Issue 27153: Default value shown by argparse.ArgumentDefaultsHelpFormatter is backwards for action='store_false' (original) (raw)

Small example:

import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--no-foo', dest='foo', action='store_false', help="Suppress foo")
args = parser.parse_args()
print('foo = {}'.format(args.foo))

Output with "-h":

optional arguments: -h, --help show this help message and exit --no-foo Suppress foo (default: True)

A reasonable person reading that would think that we suppress foo by default. But actually, foo is True by default - "--no-foo" is off by default. I would suggest that if action='store_false', the default value reported by the formatter should be flipped.

The message is correct. The default value of foo is True. It would be just as misleading to say default: False, since that would imply the default value of foo was false. It also makes no sense to think it would mean the default value of 'no-foo' is False.

Instead, if you want to use ArgumentDefaultsHelpFormatter instead of controlling when defaults are displayed, change your help string to read something like: "Set foo to False".