Python Forum
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Argparse
#1
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?
Reply
#2
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
Reply
#3
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
Reply
#4
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.
Reply
#5
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')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 614 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Not able to figure out what is wrong with argparse radioactive9 31 8,484 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  argparse --help in one line. Denial 1 1,981 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,873 Sep-11-2020, 07:42 PM
Last Post: buran
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,118 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,191 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,581 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  Why am I getting KeyError 'file' when using argparse? Mike Ru 1 3,070 Jun-09-2019, 04:48 PM
Last Post: metulburr
  How can I get some arguments using argparse? Mike Ru 0 1,873 Jun-05-2019, 12:57 PM
Last Post: Mike Ru
  argparse 2skywalkers 4 3,120 May-15-2019, 07:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020