My CLI program uses a --debug flag to decide whether or not to print debug messages. When --debug is specified, it should print debug messages; otherwise, it should not print debug messages.
My current approach is:
parser.add_argument('--debug', help='print debug messages to stderr', nargs='?')
However, the --help message suggests that this approach does not achieve my goal:
optional arguments:
-h, --help show this help message and exit
--debug [DEBUG] print debug messages to stderr
As you can see, it wants a value after the flag; however, --debug is an on/off argument.
What should I do instead?
Use the
store_trueaction instead:nargs='?'should only be used for options that take one or more arguments (with a fallback to a default value).