argparse store false if unspecified

115,107

Solution 1

The store_true option automatically creates a default value of False.

Likewise, store_false will default to True when the command-line argument is not present.

The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861

The argparse docs aren't clear on the subject, so I'll update them now: http://hg.python.org/cpython/rev/49677cc6d83a

Solution 2

With

import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-auto', action='store_true', )
args=parser.parse_args()
print(args)

running

% test.py

yields

Namespace(auto=False)

So it appears to be storing False by default.

Solution 3

Raymond Hettinger answers OP's question already.

However, my group has experienced readability issues using "store_false". Especially when new members join our group. This is because it is most intuitive way to think is that when a user specifies an argument, the value corresponding to that argument will be True or 1.

For example, if the code is -

parser.add_argument('--stop_logging', action='store_false')

The code reader may likely expect the logging statement to be off when the value in stop_logging is true. But code such as the following will lead to the opposite of the desired behavior -

if not stop_logging:
    #log

On the other hand, if the interface is defined as the following, then the "if-statement" works and is more intuitive to read -

parser.add_argument('--stop_logging', action='store_true')
if not stop_logging:
    #log
Share:
115,107
siamii
Author by

siamii

Updated on July 08, 2022

Comments

  • siamii
    siamii almost 2 years
    parser.add_argument('-auto', action='store_true')
    

    How can I store false if -auto is unspecified? I can faintly remember that this way, it stores None if unspecified

  • Faheem Mitha
    Faheem Mitha almost 11 years
    A couple of comments about this. First, it seems that if the option is -bar, then the dest is automatically set to bar, based on hg.python.org/cpython/rev/49677cc6d83a. However, I don't see where this default behavior is set in the code. I've always set the dest argument explicitly. Also, I think letting bar default to the dest for the --bar option does not really make sense if --bar is store_false. Shouldn't the dest be notbar in this case?
  • Leynos
    Leynos over 8 years
    This doesn't appear to be the case in Python 2.7 and 3.4: >>> parser.add_argument('--bar', action='store_false') _StoreFalseAction(option_strings=['--bar'], dest='bar', nargs=0, const=False, default=True, type=None, choices=None, help=None, metavar=None) >>> parser.parse_args([]) Namespace(bar=True)
  • Unix-Ninja
    Unix-Ninja over 8 years
    sorry, that's actually the default behaviour of optparse. argparse should default to the inverse of the store. i.e., 'store_false' defaults to 'True'.
  • Krassi
    Krassi almost 5 years
    You can set a destination alias, which will improve readability: parser.add_argument('--stop_logging', action='store_false', dest='use_logging').
  • brainLoop
    brainLoop almost 5 years
    I didn't understand the contrarian naming convention.
  • ady
    ady almost 5 years
    I agree, this is a bit confusing. Anyway, the 'store_false' or 'store_true' is specified as action and not a default value. Thus, when you add this argument to the program, the specified action is triggered.
  • Purushothaman Srikanth
    Purushothaman Srikanth over 3 years
    'store_true' and 'store_false' - These are special cases of 'store_const' used for storing the values True and False respectively. In addition, they create default values of False and True respectively. SOURCE: docs.python.org/3/library/argparse.html#action