I am trying to use the argparse module to make my Python program accept flexible command-line arguments. I want to pass a simple boolean flag, and saying True
or False
to execute the appropriate branch in my code.
Consider the following.
import argparse
parser = argparse.ArgumentParser(prog='test.py',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-boolflag', type=bool, default=True)
parser.add_argument('-intflag' , type=int, default=3)
args = parser.parse_args()
boolflag = args.boolflag
intflag = args.intflag
print ("Bool Flag is ", boolflag)
print ("Int Flag is ", intflag)
When I tried to execute it with python scrap.py -boolflag False -intflag 314
I got the result
Bool Flag is True
Int Flag is 314
Why is this?!! The intflag seems to be parsed correctly, yet the boolean flag is invariably parsed as True
even if I mention explicitly in the command-line arguments that I want it to be False
.
Where am I going wrong?