Skip to main content

arguments

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

# coding=utf-8
"""
arguments test

Usage:
  tests.py [options] <posarg1> <posarg2>

Options:
  -h --help                     Show this screen.
  -o --option=<option1>         An option.
  --opt2=<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: .].
"""

from arguments import Arguments

def main():
    """
    main
    """
    arg = Arguments()
    print(arg)


if __name__ == "__main__":
    main()

gives

$ python main.py pval1 pval2
<arguments.Arguments object at 0x1022e0eb8>
options :
    opt2 : hello
    option : None
    parameter : 77
    help : False
    verbose : False
positional :
    posarg1 : pval1
    posarg2 : pval2

or

$ python main.py -h
arguments test

Usage:
  tests.py [options] <posarg1> <posarg2>

Options:
  -h --help                     Show this screen.
  -o --option=<option1>         An option.
  --opt2=<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: .].

$

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.

Metadata

Release files for arguments 20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for arguments 20
File Size Uploaded
arguments-20.tar.gz 11.9 kB Details

Release files / arguments-20.tar.gz

Download URL arguments-20.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5eb42d7aa7b0f4f93709981635dccc6bac5c6b6f76e02091cc430b1eb737d69a
BLAKE2b-256 checksum
How to use checksums
79bdfca5b38bb01ea64d18aabace3ef70830de9c79e517d2826940b67dd2f518
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

76

1 release file

73

1 release file

72

1 release file

71

1 release file

70

1 release file

68

1 release file

67

1 release file

59

1 release file

58

1 release file

56

1 release file

55

1 release file

54

1 release file

53

1 release file

51

1 release file

49

1 release file

48

1 release file

47

1 release file

46

1 release file

45

1 release file

44

1 release file

43

1 release file

42

1 release file

41

1 release file

40

1 release file

39

1 release file

38

1 release file

37

1 release file

36

1 release file

35

1 release file

33

1 release file

32

1 release file

31

1 release file

30

1 release file

29

1 release file

28

1 release file

27

1 release file

26

1 release file

25

1 release file

24

1 release file

23

1 release file

22

1 release file

This release

20 This release

1 release file

19

1 release file

18

1 release file

16

1 release file

15

1 release file

12

1 release file

11

1 release file

10

1 release file

9

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page