As you have it, the argument w is expecting a value after -w on the command line. If you are just looking to flip a switch by setting a variable True or False, have a look here (specifically store_true and store_false)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-w', action='store_true')
where action='store_true' implies default=False.
Conversely, you could haveaction='store_false', which implies default=True.
As you have it, the argument w is expecting a value after -w on the command line. If you are just looking to flip a switch by setting a variable True or False, have a look here (specifically store_true and store_false)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-w', action='store_true')
where action='store_true' implies default=False.
Conversely, you could haveaction='store_false', which implies default=True.
Adding a quick snippet to have it ready to execute:
Source: myparser.py
import argparse
parser = argparse.ArgumentParser(description="Flip a switch by setting a flag")
parser.add_argument('-w', action='store_true')
args = parser.parse_args()
print args.w
Usage:
python myparser.py -w
>> True
python argparse: arg with no flag - Stack Overflow
python - Is it better practice to set default values for optional argparse arguments? - Software Engineering Stack Exchange
python - argparse optional value for argument - Stack Overflow
Argparse: How do I add a flag that doesn't require an argument, but can take an argument optionally.
Using argparse, how do you add a flag that doesn't require an argument, but can optionally take an argument.
Desired behaviour
$ keychart -c # outputs a csv to wherever PWD the script is runing from $ keychart -c /home/nick/csv # outputs a csv to specified directory
At present, my code that (almost) does what I want, but won't handle being given an argument:
# 'scratch.py'
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--csv", help="write results to a csv to disk", action='store_true')
print(args.csv)
# works as desired
$ python3 scratch.py -c
True
# doesn't work as desired
$ python3 scratch.py -c testo
usage: scratch.py [-h] [-c]
scratch.py: error: unrecognized arguments: testo
The premise behind your question is mistaken.
>>> import argparse
>>> p = argparse.ArgumentParser()
>>> p.add_argument('-a')
>>> p.parse_args()
Namespace(a=None)
The -a argument is already optional by default (you don't need required=False), and its default is already None, ensuring that args.a exists without any manual intervention.
You have to opt-in to having no default at all.
>>> p.add_argument("-b", default=argparse.SUPPRESS)
>>> p.parse_args()
Namespace(a=None)
So yes, it probably is a good idea to have some default, and argparse acknowledges that by providing one.
So some clarifications here. To answer this question for python specifically, argparse does in fact default optional argument values to None. Your problem arises from having multiple subcommands that take different arguments, but go through the same code flow. If you pass the same args from subcommands a, b, c that have different options, then you do have to handle each option that is not common across all three. In that workflow, I would recommend backfilling each option that is not guaranteed to exist.