Skip to main content

Argument parser based on docopt

Project description

Argument parser based on docopt

install

pip install arguments

usage

Docopt is used for parsing the docstring (doc), arguments bundles the schema parser and returns a OptionParser like object with normalized attributes

For example

arguments test

Usage:
  tests.py <posarg1> <posarg2>

Options:
  -h --help                     Show this screen.
  -o --option=<option1>         An option.
  --option2=<option2>           An option [default: hello].
  -p --parameter=<parameter>    Folder to check the git repos out [default: 77].
  -v --verbose                  Folder from where to run the command [default: .].

called like

python tests.py pval1 pval2

will when called in python return

>>> myargs = Arguments()
>>> print myargs.posarg1
pyval1

>>> print myargs.posarg2
pyval2

Using schema

Assume you are using docopt with the following usage-pattern:

my_program.py [--count=N] <path> <files>

and you would like to validate that <files> are readable, and that <path> exists, and that --count is either integer from 0 to 5, or None.

this is how you validate it using schema:

>>> from arguments import *

>>> s = Schema({'<files>': [Use(open)],
...             '<path>': os.path.exists,
...             '--count': Or(None, And(Use(int), lambda n: 0 < n < 5))})

>>> args = Arguments(validateschema=s)

>>> args.files
[<open file 'LICENSE-MIT', mode 'r' at 0x...>, <open file 'setup.py', mode 'r' at 0x...>]

>>> args.path
'../'

>>> args.count
3

As you can see, it validated data successfully, opened files and converted '3' to int.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arguments-9.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file arguments-9.tar.gz.

File metadata

  • Download URL: arguments-9.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for arguments-9.tar.gz
Algorithm Hash digest
SHA256 91853a30378dbe502f9d710c388672305a0ce8ca226d57c32810edcea436bdb6
MD5 a6ec21b63586734f773518ac10208be8
BLAKE2b-256 364ffc57da587f6a531ab9944b0d357a3e80e58cc0e291d2c4f603abf0f6a3a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page