AnB Arw
asked on
python argument parser
When I try to run the below script as python3.6 prog5.py 4 -v 1, I just get the value of the answer but not the output of print ("{}^2 == {}".format (args.square, answer)). Same thing with a value of 2
#!/usr/local/bin/python3.6
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("squar e", type=int, help="display a square of a given number")
parser.add_argument("-v", "--verbosity",help="increa se verbosity output")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
print ("{}^2 == {}".format (args.square, answer))
else:
print(answer)
#!/usr/local/bin/python3.6
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("squar
parser.add_argument("-v", "--verbosity",help="increa
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
print ("{}^2 == {}".format (args.square, answer))
else:
print(answer)
should "--verbosity" be "-verbosity"?
@aikimark: You talk about line 4?
This looks correct:
'-' for the one letter option
the
'--' is for the long command line options
Following example is from https://docs.python.org/2/ howto/argp arse.html# introducin g-optional -arguments
parser.add_argument("-v", "--verbosity", action='count', default=0,
help="increase verbosity output")
This looks correct:
'-' for the one letter option
the
'--' is for the long command line options
Following example is from https://docs.python.org/2/
parser.add_argument("-v", "--verbose", help="increase output verbosity",
action="store_true")
thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Often you see immediately what's wrong.
In your case you would see:
Open in new window
You see, that (as you didn't specify any explicit data type) verbosity is of type string (the default type)
comparing '1' with 1 is false
So the simplest fix would be:
Open in new window
I would however change the helptext and or your code slightly, as the helptext is misleading:
calling with -v 0 will not at all increase the verbosity nor would -v 3
I would change the code such, that -v will increase the log level to 1
-vv to 2 and so forth and also change the logic of the if statement to compare whether the log level is greater (and not whether it is equal)
Open in new window
Now you could call your program as
python3.6 prog5.py 4
python3.6 prog5.py 4 -v
python3.6 prog5.py 4 -vv
python3.6 prog5.py 4 -vvv
You can of course stay with an explicit log level, but you should then still compare with >= and not with ==
and you might change the help text to
"set the log level (higher level means more logging)"