I am trying to sort out how to pass the boolean value False from the command line to the argparser. My origninal code looked like:
import argparse
parser = argparse.ArgumentParser(allow_abbrev=True)
parser.add_argument('-data', default=True, type=bool, help='uses the history file')
args = parser.parse_args(sys.argv[1:])
From the command line, I typed: python myscript.py -data False
Also variations with single & double quotes around False. When I examine the contents of the args namespace, args.data is always True.
So I changed the argument definition from bool to str, with a string "True" default as shown below:
parser.add_argument('-data', default="True", type=str, help='uses the history file')
I then added some massaging of the args to get the boolean value I really wanted:
if re.search("f", args.data, re.I):
args.data = False
else:
args.data = True
This workaround does work. Is there a better way to do this?
First, we need the argparse package, so we go ahead and import it on Line 2. On Line 5 we instantiate the ArgumentParser object as ap . Then on Lines 6 and 7 we add our only argument, --name . We must specify both shorthand ( -n ) and longhand versions ( --name ) where either flag could be used in the command line.
You can either use the action with store_true | store_false , or you can use an int and let implicit casting check a boolean value. Using the action , you wouldn't pass a --foo=true and --foo=false argument, you would simply include it if it was to be set to true.
With argparse, I can define the arguments, their default values, their explanations, and it can be used as a convenient container. So it's all good for passing arguments from command line. But can I also use it to pass the arguments from code, for API call? Show activity on this post.
Using add_argument () method to add the argument. This tells how to take the argument from the command line. action: action that has to be taken when the arguments are passed. default: default value produced if the arguments are absent. How to parse arguments?
Yes, certainly argparse can be used to pass arguments both from command line and from code. Show activity on this post. With vars () you can use your args like dictionaries. Result when you execute and parse some arguments look like.
The ArgumentParser object will hold all the information necessary to parse the command line into Python data types. Filling an ArgumentParser with information about program arguments is done by making calls to the add_argument () method.
You can use the store_false
or store_true
parameter to add_argument
(see the argparse documentation). For example, if you want the default to be True
then you could add an argument with action='store_false'
:
parser.add_argument('--no-data', action='store_false', help="don't use the history file")
Then args.no_data
will be False
if you run python command.py --no-data
and True
if you run python command.py
without the --no-data
argument.
def my_bool(s):
return s != 'False'
parser.add_argument('-data',default=True,type=my_bool)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With