Posts: 3
Threads: 2
Joined: Sep 2017
Oct-11-2017, 08:12 AM
(This post was last modified: Oct-11-2017, 08:12 AM by niru.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import argparse
parser = argparse.ArgumentParser()
parser.add_argument( "-a" , action = "store_true" , help = "get anna's age" )
parser.add_argument( "-b" , action = "store_true" , help = "get bob's age" )
parser.add_argument( "-c" , action = "store_true" , help = "get carol's age" )
parser.add_argument( "-d" , action = "store_true" , help = "get daniel's age" )
args = parser.parse_args()
if args.a:
print "17"
elif args.b:
print "14"
elif args.c:
print "51"
elif args.d:
print "8"
elif len (sys.argv) = = 1 :
parser.print_help()
parser.exit()
|
Hi, if i do python test.py -a, it will print "17".
if i do python test.py -b, it will print "14".
Now what i want is that with : python test.py -a -text,
it would output "17" followed by a text i will choose, like:
Output: 17 is anna's age
or something like that,
what is the easiest way of doing this?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Oct-11-2017, 08:50 AM
(This post was last modified: Oct-11-2017, 08:50 AM by buran.)
Your approach is wrong - you should have only one argument - name and eventually second one if you want two versions of the output. The way you are doing it you will need to add more and more arguments for every person
1 2 3 4 5 6 7 8 9 10 11 12 |
import argparse
parser = argparse.ArgumentParser()
parser.add_argument( '-n' , '--name' , required = True , action = 'store' , dest = 'name' , help = 'Name of the person for which to return age' )
args = parser.parse_args()
persons = { 'anna' : 17 , 'bob' : 14 , 'carol' : 51 , 'daniel' : 8 }
age = persons.get(args.name.lower(), None )
if age:
print ( "{} is {} years old." . format (args.name.capitalize(), age))
else :
print ( "I don't know this person" )
|
Output: C:\cmder>python age.py -n anna
Anna is 17 years old.
C:\cmder>python age.py --name Daniel
Daniel is 8 years old.
C:\cmder>python age.py --name Thomas
I don't know this person
C:\cmder>python age.py -h
usage: age.py [-h] -n NAME
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME Name of the person for which to return age
C:\cmder>python age.py
usage: age.py [-h] -n NAME
age.py: error: argument -n/--name is required
Posts: 3
Threads: 2
Joined: Sep 2017
hello buran, thanks for your answer,
I used this approach because i still would like being able to just print any age using one arg like:
'python name.py -a' -> 17
or 'python name.py -b' -> 14
etc
and so if i add a second argument -text (python -a -text) it would print age with a text
Posts: 8,160
Threads: 160
Joined: Sep 2016
OK, try to add second argument to my example. it will be boolean, and if true - print long text, if false - print only the age. Or the other way around.
Posts: 2,953
Threads: 48
Joined: Sep 2016
action='store_true' stores True as a constant. You can use it to create flags. For example:
1 2 3 4 5 6 7 8 |
import argparse
parser = argparse.ArgumentParser()
parser.add_argument( '-v' , '--verbose' , action = 'store_true' , help = 'Verbose output' )
args = parser.parse_args()
if args.verbose:
print ( 'Long text' )
|
Posts: 5,151
Threads: 396
Joined: Sep 2016
(Oct-11-2017, 09:07 AM)niru Wrote: hello buran, thanks for your answer,
I used this approach because i still would like being able to just print any age using one arg like:
'python name.py -a' -> 17
or 'python name.py -b' -> 14
etc
and so if i add a second argument -text (python -a -text) it would print age with a text
The reason buran's is better is because you could dynamically add to the dictionary, whereas you would have to change the code everytime a new entry is added with yours. It also much more simple and easier to read using a dict as shown. Thats the whole idea of programming is that the code adapts to different situations, and you wouldnt have to hold its hand upon a change.
Recommended Tutorials:
|