argparse store false if unspecified
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.