[Docpie]

Fork me on GitHub

Basic APIs


from docpie import docpie
docpie(doc, argv=None, help=True, version=None,
       stdopt=True, attachopt=True, attachvalue=True,
       auto2dashes=True, name=None, case_sensitive=False,
       optionsfirst=False, appearedonly=False, extra={})

For basic apis, docpie accepts one required argument and three optional arguments.

doc

type: str; required

doc is the description of your program which docpie can parse. It's usually the __doc__ string of your python script, but it can also be any string in correct format. Here is an example:

"""
Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]

Options:
 -h --help    show this
 -s --sorted  sorted output
 -o FILE      specify output file [default: ./test.txt]
 --quiet      print less text
 --verbose    print more text
"""

from docpie import docpie
print(docpie(__doc__))

try it >>

argv

type: sequence; default: None

argv is the command line your program accepted and it should be a list or tuple. When set to None, docpie will use sys.argv.

Note

Don't pass str directly to argv, otherwise docpie will split the str into a list by the white spaces, which can be wrong. You may use shlex instead:

>>> import shlex
>>> cmd = input()
prog --config="/path/to/my config.json"
>>> shlex.split(cmd)
['prog', '--config=/path/to/my config.json']

help

type: bool; default: True

help tells docpie to handle -h & --help automatically.

When it's set to True, -h will print "Usage" and "Option" sections you defined, then exit; --help will print the whole doc's value and exit. set help=False if you want to handle it by yourself.

See "Advanced APIs" if you want to customize the behavior.

version

type: any non-None printable object; default: None

version specifies the version of your program. When it's not None, docpie will handle -v / --version, print this value, and exit.

See "Advanced APIs" if you want to customize the behavior.


If you like this project, you can buy me a beer to help me make it better! | Flattr this

侧栏导航