$ python3 prog.py --help
usage: prog.py [-h]
- optional arguments:
+ options:
-h, --help show this help message and exit
$ python3 prog.py --verbose
usage: prog.py [-h]
positional arguments:
echo
- optional arguments:
+ options:
-h, --help show this help message and exit
$ python3 prog.py foo
foo
positional arguments:
echo echo the string you use here
- optional arguments:
+ options:
-h, --help show this help message and exit
Now, how about doing something even more useful::
$ python3 prog.py --help
usage: prog.py [-h] [--verbosity VERBOSITY]
- optional arguments:
+ options:
-h, --help show this help message and exit
--verbosity VERBOSITY
increase output verbosity
$ python3 prog.py --help
usage: prog.py [-h] [--verbose]
- optional arguments:
+ options:
-h, --help show this help message and exit
--verbose increase output verbosity
$ python3 prog.py --help
usage: prog.py [-h] [-v]
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --verbose increase output verbosity
positional arguments:
square display a square of a given number
- optional arguments:
+ options:
-h, --help show this help message and exit
-v {0,1,2}, --verbosity {0,1,2}
increase output verbosity
print(answer)
We have introduced another action, "count",
-to count the number of occurrences of a specific optional arguments:
+to count the number of occurrences of specific options.
+
.. code-block:: shell-session
positional arguments:
square display a square of a given number
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --verbosity increase output verbosity
$ python3 prog.py 4 -vvv
x the base
y the exponent
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --verbosity
$ python3 prog.py 4 2 -v
x the base
y the exponent
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --verbose
-q, --quiet
positional arguments:
N an integer for the accumulator
- optional arguments:
+ options:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
>>> parser.print_help()
usage: myprogram [-h]
- optional arguments:
+ options:
-h, --help show this help message and exit
Note that the program name, whether determined from ``sys.argv[0]`` or from the
>>> parser.print_help()
usage: myprogram [-h] [--foo FOO]
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo of the myprogram program
positional arguments:
bar bar help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo [FOO] foo help
positional arguments:
bar bar help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo [FOO] foo help
A foo that bars
- optional arguments:
+ options:
-h, --help show this help message and exit
By default, the description will be line-wrapped so that it fits within the
A foo that bars
- optional arguments:
+ options:
-h, --help show this help message and exit
And that's how you'd foo a bar
this description was indented weird but that is okay
- optional arguments:
+ options:
-h, --help show this help message and exit
likewise for this epilog whose whitespace will be cleaned up and whose words
exactly the way
I want it
- optional arguments:
+ options:
-h, --help show this help message and exit
:class:`RawTextHelpFormatter` maintains whitespace for all sorts of help text,
positional arguments:
bar BAR! (default: [1, 2, 3])
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO FOO! (default: 42)
positional arguments:
float
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo int
>>> parser.print_help()
usage: PROG [-h] [-f FOO] [--foo FOO]
- optional arguments:
+ options:
-h, --help show this help message and exit
-f FOO old foo help
--foo FOO new foo help
$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
>>> parser.print_help()
usage: PROG [--foo FOO]
- optional arguments:
+ options:
--foo FOO foo help
The help option is typically ``-h/--help``. The exception to this is
>>> parser.print_help()
usage: PROG [+h]
- optional arguments:
+ options:
+h, ++help show this help message and exit
positional arguments:
bar one of the bars to be frobbled
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo the bars before frobbling
positional arguments:
bar the bar to frobble (default: 42)
- optional arguments:
+ options:
-h, --help show this help message and exit
As the help string supports %-formatting, if you want a literal ``%`` to appear
>>> parser.print_help()
usage: frobble [-h]
- optional arguments:
+ options:
-h, --help show this help message and exit
positional arguments:
bar
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO
positional arguments:
XXX
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo YYY
>>> parser.print_help()
usage: PROG [-h] [-x X X] [--foo bar baz]
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X X
--foo bar baz
a a help
b b help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo help
positional arguments:
bar bar help
- optional arguments:
+ options:
-h, --help show this help message and exit
>>> parser.parse_args(['b', '--help'])
usage: PROG b [-h] [--baz {X,Y,Z}]
- optional arguments:
+ options:
-h, --help show this help message and exit
--baz {X,Y,Z} baz help
>>> parser.parse_args(['-h'])
usage: [-h] {foo,bar} ...
- optional arguments:
+ options:
-h, --help show this help message and exit
subcommands:
bar bar help
{1,2,3} command help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo help
'''))
bar bar help
{1,2,3} command help
- optional arguments:
+ options:
-h, --help show this help message and exit
++foo foo help
'''))
main description
- optional arguments:
+ options:
-h, --help show this help message and exit
--non-breaking help message containing non-breaking spaces shall not
wrap\N{NO-BREAK SPACE}at non-breaking spaces
bar bar help
{1,2,3} command help
- optional arguments:
+ options:
+h, ++help show this help message and exit
++foo foo help
'''))
2 2 help
3 3 help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo help
'''))
positional arguments:
bar bar help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo help
positional arguments:
{a,b,c} x help
- optional arguments:
+ options:
-h, --help show this help message and exit
-w W w help
'''))
positional arguments:
z z help
- optional arguments:
+ options:
-h, --help show this help message and exit
-y {1,2,3} y help
'''))
positional arguments:
bar bar help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo foo help
a
z
- optional arguments:
+ options:
-h, --help show this help message and exit
-b B
--w W
self.assertEqual(parser_help, textwrap.dedent('''\
usage: {}{}[-h] [-w W] [-x X] [-y Y | -z Z]
- optional arguments:
+ options:
-h, --help show this help message and exit
-y Y
-z Z
expected = '''\
usage: PROG [-h] [--foo | --bar] [--soup | --nuts]
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo
--bar
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
--bar BAR bar help
--baz [BAZ] baz help
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
--abcde ABCDE abcde help
--fghij FGHIJ fghij help
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
-y y help
'''
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
'''
positional arguments:
badger BADGER
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO
--spam SPAM SPAM
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
-x x help
-a a help
'''
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
Titled group:
x x help
a a help
- optional arguments:
+ options:
-h, --help show this help message and exit
-y y help
-b b help
help = '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
-a A
-b B
foo FOO HELP
bar BAR HELP
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-x X HELP
bar
BAR HELP
- optional arguments:
+ options:
-h, --help
show this
help
foo FOO HELP
bar BAR HELP
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-x X HELP
ekiekiekifekang EKI HELP
bar BAR HELP
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X HELP
--y Y Y HELP
positional arguments:
yyy normal y help
- optional arguments:
+ options:
-h, --help show this help message and exit
-x XX oddly formatted -x help
YHYH YHYH
YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH
- optional arguments:
+ options:
-h, --help show this help message and exit
-x XX XHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH \
HXXHH HXXHH
YHYH YHYH
YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-x XXXXXXXXXXXXXXXXXXXXXXXXX
b b
c c
- optional arguments:
+ options:
-h, --help show this help message and exit
-w W [W ...] w
-x [X ...] x
a
b
- optional arguments:
+ options:
-h, --help show this help message and exit
-w W
-x X
a
b
- optional arguments:
+ options:
-h, --help show this help message and exit
-w WWWWWWWWWWWWWWWWWWWWWWWWW
-x XXXXXXXXXXXXXXXXXXXXXXXXX
b
c
- optional arguments:
+ options:
-h, --help show this help message and exit
-w WWWWWWWWWWWWWWWWWWWWWWWWW
-x XXXXXXXXXXXXXXXXXXXXXXXXX
bbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccc
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X
-y Y
bbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccc
- optional arguments:
+ options:
-h, --help show this help message and exit
-x XXXXXXXXXXXXXXXXXXXXXXXXX
-y YYYYYYYYYYYYYYYYYYYYYYYYY
'''
help = usage + '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
-x XXXXXXXXXXXXXXXXXXXXXXXXX
-y YYYYYYYYYYYYYYYYYYYYYYYYY
spam spam PROG None
badger badger PROG 0.5
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X x PROG None int %
-y y PROG 42 XXX
''')
help = usage + '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
'''
version = ''
positional arguments:
spam spam help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
'''
positional arguments:
spam spam help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
'''
'''
help = usage + '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
'''
'''
help = usage + '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
'''
'''
help = usage + '''\
- optional arguments:
+ options:
^^foo foo help
;b BAR, ;;bar BAR bar help
'''
positional arguments:
spam spam help
- optional arguments:
+ options:
--foo FOO foo help
'''
version = ''
positional arguments:
spam
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO
'''
'''
help = usage + '''\
- optional arguments:
+ options:
-h, --help show this help message and exit
-w W1 [W2 ...] w
-x [X1 [X2 ...]] x
positional arguments:
spam spam help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help should also
appear as given here
positional arguments:
spam spam help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help should not retain this odd formatting
spam spam help
badger badger help (default: wooden)
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, None
--bar bar help (default: False)
description
- optional arguments:
+ options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
'''
positional arguments:
spam spam help
- optional arguments:
+ options:
-h, --help show this help message and exit
--foo FOO foo help
'''
positional arguments:
{a,b,c,d,e}
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
'''
d d subcommand help
e e subcommand help
- optional arguments:
+ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
'''
positional arguments:
int
- optional arguments:
+ options:
-h, --help show this help message and exit
-b custom_type
-c SOME FLOAT
self.assertEqual(parser.format_help(), textwrap.dedent('''\
usage: PROG [-h] [-x X]
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X NEW X
'''))
self.assertEqual(parser.format_help(), textwrap.dedent('''\
usage: PROG [-h] [-x X] [--spam NEW_SPAM]
- optional arguments:
+ options:
-h, --help show this help message and exit
-x X NEW X
--spam NEW_SPAM
usage: this_is_spammy_prog_with_a_long_name_sorry_about_the_name
[-h] [--proxy <http[s]://example:1234>]
- optional arguments:
+ options:
-h, --help show this help message and exit
--proxy <http[s]://example:1234>
'''))