METADATA 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. Metadata-Version: 2.1
  2. Name: autocommand
  3. Version: 2.2.2
  4. Summary: A library to create a command-line program from a function
  5. Home-page: https://github.com/Lucretiel/autocommand
  6. Author: Nathan West
  7. License: LGPLv3
  8. Project-URL: Homepage, https://github.com/Lucretiel/autocommand
  9. Project-URL: Bug Tracker, https://github.com/Lucretiel/autocommand/issues
  10. Platform: any
  11. Classifier: Development Status :: 6 - Mature
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 3
  16. Classifier: Programming Language :: Python :: 3 :: Only
  17. Classifier: Topic :: Software Development
  18. Classifier: Topic :: Software Development :: Libraries
  19. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  20. Requires-Python: >=3.7
  21. Description-Content-Type: text/markdown
  22. License-File: LICENSE
  23. [![PyPI version](https://badge.fury.io/py/autocommand.svg)](https://badge.fury.io/py/autocommand)
  24. # autocommand
  25. A library to automatically generate and run simple argparse parsers from function signatures.
  26. ## Installation
  27. Autocommand is installed via pip:
  28. ```
  29. $ pip install autocommand
  30. ```
  31. ## Usage
  32. Autocommand turns a function into a command-line program. It converts the function's parameter signature into command-line arguments, and automatically runs the function if the module was called as `__main__`. In effect, it lets your create a smart main function.
  33. ```python
  34. from autocommand import autocommand
  35. # This program takes exactly one argument and echos it.
  36. @autocommand(__name__)
  37. def echo(thing):
  38. print(thing)
  39. ```
  40. ```
  41. $ python echo.py hello
  42. hello
  43. $ python echo.py -h
  44. usage: echo [-h] thing
  45. positional arguments:
  46. thing
  47. optional arguments:
  48. -h, --help show this help message and exit
  49. $ python echo.py hello world # too many arguments
  50. usage: echo.py [-h] thing
  51. echo.py: error: unrecognized arguments: world
  52. ```
  53. As you can see, autocommand converts the signature of the function into an argument spec. When you run the file as a program, autocommand collects the command-line arguments and turns them into function arguments. The function is executed with these arguments, and then the program exits with the return value of the function, via `sys.exit`. Autocommand also automatically creates a usage message, which can be invoked with `-h` or `--help`, and automatically prints an error message when provided with invalid arguments.
  54. ### Types
  55. You can use a type annotation to give an argument a type. Any type (or in fact any callable) that returns an object when given a string argument can be used, though there are a few special cases that are described later.
  56. ```python
  57. @autocommand(__name__)
  58. def net_client(host, port: int):
  59. ...
  60. ```
  61. Autocommand will catch `TypeErrors` raised by the type during argument parsing, so you can supply a callable and do some basic argument validation as well.
  62. ### Trailing Arguments
  63. You can add a `*args` parameter to your function to give it trailing arguments. The command will collect 0 or more trailing arguments and supply them to `args` as a tuple. If a type annotation is supplied, the type is applied to each argument.
  64. ```python
  65. # Write the contents of each file, one by one
  66. @autocommand(__name__)
  67. def cat(*files):
  68. for filename in files:
  69. with open(filename) as file:
  70. for line in file:
  71. print(line.rstrip())
  72. ```
  73. ```
  74. $ python cat.py -h
  75. usage: ipython [-h] [file [file ...]]
  76. positional arguments:
  77. file
  78. optional arguments:
  79. -h, --help show this help message and exit
  80. ```
  81. ### Options
  82. To create `--option` switches, just assign a default. Autocommand will automatically create `--long` and `-s`hort switches.
  83. ```python
  84. @autocommand(__name__)
  85. def do_with_config(argument, config='~/foo.conf'):
  86. pass
  87. ```
  88. ```
  89. $ python example.py -h
  90. usage: example.py [-h] [-c CONFIG] argument
  91. positional arguments:
  92. argument
  93. optional arguments:
  94. -h, --help show this help message and exit
  95. -c CONFIG, --config CONFIG
  96. ```
  97. The option's type is automatically deduced from the default, unless one is explicitly given in an annotation:
  98. ```python
  99. @autocommand(__name__)
  100. def http_connect(host, port=80):
  101. print('{}:{}'.format(host, port))
  102. ```
  103. ```
  104. $ python http.py -h
  105. usage: http.py [-h] [-p PORT] host
  106. positional arguments:
  107. host
  108. optional arguments:
  109. -h, --help show this help message and exit
  110. -p PORT, --port PORT
  111. $ python http.py localhost
  112. localhost:80
  113. $ python http.py localhost -p 8080
  114. localhost:8080
  115. $ python http.py localhost -p blah
  116. usage: http.py [-h] [-p PORT] host
  117. http.py: error: argument -p/--port: invalid int value: 'blah'
  118. ```
  119. #### None
  120. If an option is given a default value of `None`, it reads in a value as normal, but supplies `None` if the option isn't provided.
  121. #### Switches
  122. If an argument is given a default value of `True` or `False`, or
  123. given an explicit `bool` type, it becomes an option switch.
  124. ```python
  125. @autocommand(__name__)
  126. def example(verbose=False, quiet=False):
  127. pass
  128. ```
  129. ```
  130. $ python example.py -h
  131. usage: example.py [-h] [-v] [-q]
  132. optional arguments:
  133. -h, --help show this help message and exit
  134. -v, --verbose
  135. -q, --quiet
  136. ```
  137. Autocommand attempts to do the "correct thing" in these cases- if the default is `True`, then supplying the switch makes the argument `False`; if the type is `bool` and the default is some other `True` value, then supplying the switch makes the argument `False`, while not supplying the switch makes the argument the default value.
  138. Autocommand also supports the creation of switch inverters. Pass `add_nos=True` to `autocommand` to enable this.
  139. ```
  140. @autocommand(__name__, add_nos=True)
  141. def example(verbose=False):
  142. pass
  143. ```
  144. ```
  145. $ python example.py -h
  146. usage: ipython [-h] [-v] [--no-verbose]
  147. optional arguments:
  148. -h, --help show this help message and exit
  149. -v, --verbose
  150. --no-verbose
  151. ```
  152. Using the `--no-` version of a switch will pass the opposite value in as a function argument. If multiple switches are present, the last one takes precedence.
  153. #### Files
  154. If the default value is a file object, such as `sys.stdout`, then autocommand just looks for a string, for a file path. It doesn't do any special checking on the string, though (such as checking if the file exists); it's better to let the client decide how to handle errors in this case. Instead, it provides a special context manager called `smart_open`, which behaves exactly like `open` if a filename or other openable type is provided, but also lets you use already open files:
  155. ```python
  156. from autocommand import autocommand, smart_open
  157. import sys
  158. # Write the contents of stdin, or a file, to stdout
  159. @autocommand(__name__)
  160. def write_out(infile=sys.stdin):
  161. with smart_open(infile) as f:
  162. for line in f:
  163. print(line.rstrip())
  164. # If a file was opened, it is closed here. If it was just stdin, it is untouched.
  165. ```
  166. ```
  167. $ echo "Hello World!" | python write_out.py | tee hello.txt
  168. Hello World!
  169. $ python write_out.py --infile hello.txt
  170. Hello World!
  171. ```
  172. ### Descriptions and docstrings
  173. The `autocommand` decorator accepts `description` and `epilog` kwargs, corresponding to the `description <https://docs.python.org/3/library/argparse.html#description>`_ and `epilog <https://docs.python.org/3/library/argparse.html#epilog>`_ of the `ArgumentParser`. If no description is given, but the decorated function has a docstring, then it is taken as the `description` for the `ArgumentParser`. You can also provide both the description and epilog in the docstring by splitting it into two sections with 4 or more - characters.
  174. ```python
  175. @autocommand(__name__)
  176. def copy(infile=sys.stdin, outfile=sys.stdout):
  177. '''
  178. Copy an the contents of a file (or stdin) to another file (or stdout)
  179. ----------
  180. Some extra documentation in the epilog
  181. '''
  182. with smart_open(infile) as istr:
  183. with smart_open(outfile, 'w') as ostr:
  184. for line in istr:
  185. ostr.write(line)
  186. ```
  187. ```
  188. $ python copy.py -h
  189. usage: copy.py [-h] [-i INFILE] [-o OUTFILE]
  190. Copy an the contents of a file (or stdin) to another file (or stdout)
  191. optional arguments:
  192. -h, --help show this help message and exit
  193. -i INFILE, --infile INFILE
  194. -o OUTFILE, --outfile OUTFILE
  195. Some extra documentation in the epilog
  196. $ echo "Hello World" | python copy.py --outfile hello.txt
  197. $ python copy.py --infile hello.txt --outfile hello2.txt
  198. $ python copy.py --infile hello2.txt
  199. Hello World
  200. ```
  201. ### Parameter descriptions
  202. You can also attach description text to individual parameters in the annotation. To attach both a type and a description, supply them both in any order in a tuple
  203. ```python
  204. @autocommand(__name__)
  205. def copy_net(
  206. infile: 'The name of the file to send',
  207. host: 'The host to send the file to',
  208. port: (int, 'The port to connect to')):
  209. '''
  210. Copy a file over raw TCP to a remote destination.
  211. '''
  212. # Left as an exercise to the reader
  213. ```
  214. ### Decorators and wrappers
  215. Autocommand automatically follows wrapper chains created by `@functools.wraps`. This means that you can apply other wrapping decorators to your main function, and autocommand will still correctly detect the signature.
  216. ```python
  217. from functools import wraps
  218. from autocommand import autocommand
  219. def print_yielded(func):
  220. '''
  221. Convert a generator into a function that prints all yielded elements
  222. '''
  223. @wraps(func)
  224. def wrapper(*args, **kwargs):
  225. for thing in func(*args, **kwargs):
  226. print(thing)
  227. return wrapper
  228. @autocommand(__name__,
  229. description= 'Print all the values from START to STOP, inclusive, in steps of STEP',
  230. epilog= 'STOP and STEP default to 1')
  231. @print_yielded
  232. def seq(stop, start=1, step=1):
  233. for i in range(start, stop + 1, step):
  234. yield i
  235. ```
  236. ```
  237. $ seq.py -h
  238. usage: seq.py [-h] [-s START] [-S STEP] stop
  239. Print all the values from START to STOP, inclusive, in steps of STEP
  240. positional arguments:
  241. stop
  242. optional arguments:
  243. -h, --help show this help message and exit
  244. -s START, --start START
  245. -S STEP, --step STEP
  246. STOP and STEP default to 1
  247. ```
  248. Even though autocommand is being applied to the `wrapper` returned by `print_yielded`, it still retreives the signature of the underlying `seq` function to create the argument parsing.
  249. ### Custom Parser
  250. While autocommand's automatic parser generator is a powerful convenience, it doesn't cover all of the different features that argparse provides. If you need these features, you can provide your own parser as a kwarg to `autocommand`:
  251. ```python
  252. from argparse import ArgumentParser
  253. from autocommand import autocommand
  254. parser = ArgumentParser()
  255. # autocommand can't do optional positonal parameters
  256. parser.add_argument('arg', nargs='?')
  257. # or mutually exclusive options
  258. group = parser.add_mutually_exclusive_group()
  259. group.add_argument('-v', '--verbose', action='store_true')
  260. group.add_argument('-q', '--quiet', action='store_true')
  261. @autocommand(__name__, parser=parser)
  262. def main(arg, verbose, quiet):
  263. print(arg, verbose, quiet)
  264. ```
  265. ```
  266. $ python parser.py -h
  267. usage: write_file.py [-h] [-v | -q] [arg]
  268. positional arguments:
  269. arg
  270. optional arguments:
  271. -h, --help show this help message and exit
  272. -v, --verbose
  273. -q, --quiet
  274. $ python parser.py
  275. None False False
  276. $ python parser.py hello
  277. hello False False
  278. $ python parser.py -v
  279. None True False
  280. $ python parser.py -q
  281. None False True
  282. $ python parser.py -vq
  283. usage: parser.py [-h] [-v | -q] [arg]
  284. parser.py: error: argument -q/--quiet: not allowed with argument -v/--verbose
  285. ```
  286. Any parser should work fine, so long as each of the parser's arguments has a corresponding parameter in the decorated main function. The order of parameters doesn't matter, as long as they are all present. Note that when using a custom parser, autocommand doesn't modify the parser or the retrieved arguments. This means that no description/epilog will be added, and the function's type annotations and defaults (if present) will be ignored.
  287. ## Testing and Library use
  288. The decorated function is only called and exited from if the first argument to `autocommand` is `'__main__'` or `True`. If it is neither of these values, or no argument is given, then a new main function is created by the decorator. This function has the signature `main(argv=None)`, and is intended to be called with arguments as if via `main(sys.argv[1:])`. The function has the attributes `parser` and `main`, which are the generated `ArgumentParser` and the original main function that was decorated. This is to facilitate testing and library use of your main. Calling the function triggers a `parse_args()` with the supplied arguments, and returns the result of the main function. Note that, while it returns instead of calling `sys.exit`, the `parse_args()` function will raise a `SystemExit` in the event of a parsing error or `-h/--help` argument.
  289. ```python
  290. @autocommand()
  291. def test_prog(arg1, arg2: int, quiet=False, verbose=False):
  292. if not quiet:
  293. print(arg1, arg2)
  294. if verbose:
  295. print("LOUD NOISES")
  296. return 0
  297. print(test_prog(['-v', 'hello', '80']))
  298. ```
  299. ```
  300. $ python test_prog.py
  301. hello 80
  302. LOUD NOISES
  303. 0
  304. ```
  305. If the function is called with no arguments, `sys.argv[1:]` is used. This is to allow the autocommand function to be used as a setuptools entry point.
  306. ## Exceptions and limitations
  307. - There are a few possible exceptions that `autocommand` can raise. All of them derive from `autocommand.AutocommandError`.
  308. - If an invalid annotation is given (that is, it isn't a `type`, `str`, `(type, str)`, or `(str, type)`, an `AnnotationError` is raised. The `type` may be any callable, as described in the `Types`_ section.
  309. - If the function has a `**kwargs` parameter, a `KWargError` is raised.
  310. - If, somehow, the function has a positional-only parameter, a `PositionalArgError` is raised. This means that the argument doesn't have a name, which is currently not possible with a plain `def` or `lambda`, though many built-in functions have this kind of parameter.
  311. - There are a few argparse features that are not supported by autocommand.
  312. - It isn't possible to have an optional positional argument (as opposed to a `--option`). POSIX thinks this is bad form anyway.
  313. - It isn't possible to have mutually exclusive arguments or options
  314. - It isn't possible to have subcommands or subparsers, though I'm working on a few solutions involving classes or nested function definitions to allow this.
  315. ## Development
  316. Autocommand cannot be important from the project root; this is to enforce separation of concerns and prevent accidental importing of `setup.py` or tests. To develop, install the project in editable mode:
  317. ```
  318. $ python setup.py develop
  319. ```
  320. This will create a link to the source files in the deployment directory, so that any source changes are reflected when it is imported.