Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse how to pass False from the command line?

Tags:

python

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?

like image 583
John Brearley Avatar asked Jun 20 '18 18:06

John Brearley


People also ask

How do you pass arguments to Argparse?

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.

How do I pass a boolean to a Python script?

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.

Can argparse be used to pass arguments from code?

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.

How to take arguments from command line in Python?

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?

Is it possible to pass arguments from command line to code?

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.

What is argumentparser in Python?

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.


2 Answers

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.

like image 64
orn688 Avatar answered Sep 21 '22 14:09

orn688


def my_bool(s):
    return s != 'False'

parser.add_argument('-data',default=True,type=my_bool)
like image 42
dashiell Avatar answered Sep 18 '22 14:09

dashiell