Skip to main content

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.

Download files

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

Source Distribution

arguments-19.tar.gz (11.8 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for arguments-19.tar.gz
Algorithm Hash digest
SHA256 eb21d93206d300897a93984f71213b2bc5cccc899b81c25911aa2cab33e865df
MD5 a73dbedacf934ce25d624c7771abcd71
BLAKE2b-256 1b4feb456c106ea78d0d941eb4de8d185ae9af446f75420598a0fc2cbec969ec

See more details on using hashes here.

Release history Release notifications | RSS feed

76

1 file

73

1 file

72

1 file

71

1 file

70

1 file

68

1 file

67

1 file

59

1 file

58

1 file

56

1 file

55

1 file

54

1 file

53

1 file

51

1 file

49

1 file

48

1 file

47

1 file

46

1 file

45

1 file

44

1 file

43

1 file

42

1 file

41

1 file

40

1 file

39

1 file

38

1 file

37

1 file

36

1 file

35

1 file

33

1 file

32

1 file

31

1 file

30

1 file

29

1 file

28

1 file

27

1 file

26

1 file

25

1 file

24

1 file

23

1 file

22

1 file

20

1 file

This release

19 This release

1 file

18

1 file

16

1 file

15

1 file

12

1 file

11

1 file

10

1 file

9

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page