Consider the following code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-o', required=True, nargs=1)
parser.parse_args(['-h'])
Why am I getting the following output even if I said that -o
is required?
usage: test.py [-h] -o O
optional arguments:
-h, --help show this help message and exit
-o O
I would expect the help text to say that -o
is required.
This is issue #9694 on the Python bug tracker, as yet unfixed now fixed by replacing the term "optional arguments" with "options". You can read more there. Note that the usage line does correctly indicate that the switch is required (if it weren't, the usage line would read usage: test.py [-h] [-o O]
instead of usage: test.py [-h] -o O
).
For working around it in older versions of Python, you can use argument groups which can be given a title
. As the example shows at the linked page, this allows you to create groups with your choice of name instead of the default positional arguments
and optional arguments
groupings.