165
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

oHo
  • 51,447
  • 27
  • 165
  • 200
siamii
  • 23,374
  • 28
  • 93
  • 143

5 Answers5

284

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

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 2
    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 http://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? – Faheem Mitha Jun 20 '13 at 10:48
  • 45
    I didn't understand the contrarian naming convention. – brainLoop Jul 16 '19 at 09:04
  • 6
    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. – ady Aug 11 '19 at 15:45
  • 1
    '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: https://docs.python.org/3/library/argparse.html#action – Purushothaman Srikanth Feb 06 '21 at 06:48
  • 2
    @brainLoop I think 'store_true' and 'store_false' answers the question - "What boolean value to store when this key/argument is passed (without a value, ofcourse)?". Passing a boolean value to that key leads to an error message in the lines of "ignored explicit argument". For example, let's say the key is 'do_something' which can store a boolean value of True or False. If action='store_true' and the cmdline looks like `./my_python_script --do_something` then do_something will be set to 'True'. Conversely, if action='store_false' for that same cmdline, then do_something will be set to 'False'. – ZeZNiQ Aug 29 '22 at 01:49
  • 1
    "The `store_true` option automatically creates a default value of *False*" should also clarify, that is, when arg option is not present. Summary: `store_true` assigns True if arg option is found; `store_false` assigns False if arg option is found. If the arg option is not found, then the **default value** is assigned (check first two lines of answer), what folks refer to "contrarian convention", not confusing IMO after some clarification. – milpita Nov 08 '22 at 20:41
30

With

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-flag', action='store_true')
args = parser.parse_args()

print(args.flag)

running yields

False

So it appears to be storing False by default.

AS Mackay
  • 2,831
  • 9
  • 19
  • 25
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
14

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
MonsieurBeilto
  • 878
  • 1
  • 11
  • 18
  • 25
    You can set a destination alias, which will improve readability: `parser.add_argument('--stop_logging', action='store_false', dest='use_logging')`. – Krassi Jun 18 '19 at 12:05
1

I've found the default, when unspecified, to vary between OSX and Linux.

With the following line of code,

parser.add_argument('-auto', action='store_true')

and then omitting -auto from the command line a Mac results in auto being assigned a value of False, as expected, whereas on Ubuntu Linux auto is assigned True by default.

-6

store_false will actually default to 0 by default (you can test to verify). To change what it defaults to, just add default=True to your declaration.

So in this case: parser.add_argument('-auto', action='store_true', default=True)

Unix-Ninja
  • 124
  • 2
  • 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)` – Leynos Sep 16 '15 at 12:50
  • 2
    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'. – Unix-Ninja Sep 17 '15 at 20:34