|  | 
| 1 |  | -argscript | 
| 2 |  | -========= | 
|  | 1 | +[argscript][] is a port of [werkzeug.script][ws] to [argparse][]. Argscript generates | 
|  | 2 | +a an argparse command line parser from the function definitions of a script. | 
|  | 3 | + | 
|  | 4 | +> An action is a function in the same module starting with `action_` which takes | 
|  | 5 | +a number of arguments where every argument has a default. The type of the | 
|  | 6 | +default value specifies the type of the argument. Arguments can then be passed by using `--name=value` from the shell. [werkzeug.script][ws] | 
| 3 | 7 | 
 | 
| 4 |  | -Port of [werkzeug.script][ws] to [argparse][]. | 
| 5 | 8 | 
 | 
| 6 |  | -Since werkzeug.script is being deprecated, I've ported it to argparse. | 
| 7 | 9 | 
 | 
| 8 | 10 | Basic Usage is the same as werkzeug.script: | 
| 9 | 11 | 
 | 
| 10 | 12 |     #!/usr/bin/env python | 
| 11 | 13 |     # -*- coding: utf-8 -*- | 
| 12 |  | -    """Docstring for script""" | 
|  | 14 | +    """Docstring for test script""" | 
| 13 | 15 | 
 | 
| 14 | 16 |     import argscript | 
| 15 | 17 | 
 | 
| 16 | 18 |     # actions go here | 
| 17 |  | -    def action_foo(arg='test'): | 
|  | 19 | +    def action_foo(arg1='test'): | 
| 18 | 20 |         """doc for action foo | 
| 19 | 21 |          | 
| 20 |  | -        arg -- first argument | 
|  | 22 | +        arg1 -- first argument | 
| 21 | 23 |         """ | 
| 22 |  | -        pass | 
|  | 24 | +        print 'action_foo called with argument arg1: %s' % arg1  | 
| 23 | 25 | 
 | 
| 24 |  | -    def action_lee(bar=4): | 
|  | 26 | +    def action_bar(msg='', times=1): | 
| 25 | 27 |         """doc for action bar | 
| 26 | 28 | 
 | 
| 27 |  | -        bar -- run with bar | 
|  | 29 | +        msg -- a message | 
|  | 30 | +        times -- number  | 
| 28 | 31 |         """ | 
| 29 |  | -        pass | 
|  | 32 | +        print 'action_lee called with arguments msg: %s, times: %s' %(msg, times) | 
|  | 33 | +         | 
| 30 | 34 | 
 | 
| 31 | 35 |     if __name__ == '__main__': | 
| 32 | 36 |         argscript.run() | 
| 33 | 37 | 
 | 
|  | 38 | +Now you can run the script with the `-h` parameter to show a basic help. The  | 
|  | 39 | +docstring from the `test.py` script is reused in the help message. | 
|  | 40 | + | 
|  | 41 | +    $ ./test.py -h | 
|  | 42 | +    usage: test.py [-h] {foo,bar} ... | 
|  | 43 | + | 
|  | 44 | +    Docstring for test script | 
|  | 45 | + | 
|  | 46 | +    positional arguments: | 
|  | 47 | +      {foo,bar} | 
|  | 48 | + | 
|  | 49 | +    optional arguments: | 
|  | 50 | +      -h, --help  show this help message and exit | 
|  | 51 | + | 
|  | 52 | +To get the help message for a particular action call the script with the action | 
|  | 53 | +name and the `-h` parameter: | 
|  | 54 | + | 
|  | 55 | +    $ ./test.py bar -h | 
|  | 56 | +    usage: test.py bar [-h] [--msg MSG] [--times TIMES] | 
|  | 57 | + | 
|  | 58 | +    doc for action bar | 
|  | 59 | + | 
|  | 60 | +    optional arguments: | 
|  | 61 | +      -h, --help     show this help message and exit | 
|  | 62 | +      --msg MSG      a message | 
|  | 63 | +      --times TIMES  number | 
|  | 64 | + | 
|  | 65 | +Again the function docstring is reuesed in the help message. If you specify | 
|  | 66 | +a description for the arguments in the doc string (`variable -- doc`), they are | 
|  | 67 | +reuesed too. | 
|  | 68 | + | 
|  | 69 | +Now you can run the script with the action name and the parameters, and the | 
|  | 70 | +action function will be called with them. If you obmit a parameter, the default | 
|  | 71 | +value will be used to call the function. | 
|  | 72 | + | 
|  | 73 | +    $ ./test.py bar --msg='Hello World' --times=4 | 
|  | 74 | +     | 
|  | 75 | +    action_lee called with arguments msg: Hello World, times: 4 | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +## Installation | 
|  | 79 | + | 
|  | 80 | +You can either install the development version from [github][argscript]: | 
|  | 81 | + | 
|  | 82 | +    git clone [email protected]:hoffmann/argscript.git | 
|  | 83 | +    cd argscript | 
|  | 84 | +    sudo python setup.py install | 
|  | 85 | + | 
|  | 86 | + | 
|  | 87 | +or install it from [pypi.python.org][pypi]: | 
|  | 88 | + | 
|  | 89 | +    pip install argscript | 
|  | 90 | + | 
|  | 91 | + | 
|  | 92 | + | 
| 34 | 93 | 
 | 
| 35 | 94 | [ws]: http://werkzeug.pocoo.org/docs/script/ | 
| 36 | 95 | [argparse]: http://docs.python.org/library/argparse.html | 
|  | 96 | +[argscript]: https://github.com/hoffmann/argscript | 
|  | 97 | +[pypi]: http://pypi.python.org/pypi/argscript/ | 
0 commit comments