Python Forum
Argparse - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Argparse (/thread-5565.html)



Argparse - niru - Oct-11-2017

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?


RE: Argparse - buran - Oct-11-2017

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

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



RE: Argparse - niru - Oct-11-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


RE: Argparse - buran - Oct-11-2017

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.


RE: Argparse - wavic - Oct-11-2017

action='store_true' stores True as a constant. You can use it to create flags. For example:

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')



RE: Argparse - metulburr - Oct-11-2017

(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.