Link to home
Start Free TrialLog in
Avatar of AnB Arw
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("square", type=int, help="display a square of a given number")
parser.add_argument("-v", "--verbosity",help="increase 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)
Avatar of gelonida
gelonida
Flag of France image

just print args after parsing.

Often you see immediately what's wrong.

In your case you would see:
Namespace(square=4, verbosity='1')

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:

parser.add_argument("-v", "--verbosity",type=int, help="increase verbosity output")

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)

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int, help="display a square of a given number")
parser.add_argument("-v", "--verbosity", action='count', default=0,
                    help="increase verbosity output")
args = parser.parse_args()
print(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)

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)"
should "--verbosity" be "-verbosity"?
@aikimark: You talk about line 4?
parser.add_argument("-v", "--verbosity", action='count', default=0,
                    help="increase verbosity output")

Open in new window


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/argparse.html#introducing-optional-arguments
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")

Open in new window

thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.