how to add multiple argument options in python using argparse?
A nargs='?'
flagged option works in 3 ways
parser.add_argument('-d', nargs='?', default='DEF', const='CONST')
commandline:
foo.py -d value # => args.d == 'value'
foo.py -d # => args.d == 'CONST'
foo.py # => args.d == 'DEF'
https://docs.python.org/3/library/argparse.html#const
Taking advantage of that, you shouldn't need anything like this erroneous -d -o
flag.
If you don't use the const
parameter, don't use '?'
parser.add_argument('--user','-u', nargs='?', const='CONST', default='default_user')
parser.add_argument('--output','-o', default='default_outfile')
parser.add_argument('--input','-i', default='default_infile')
Tara Prasad Gurung
A full time DevOps Engineer. SRE Enthusiast. On my free time you can find me hosting DevOps Kathmandu Meetups to uplift Devops community in Kathmandu. #DEVOPS #Kubernetes.
Updated on June 04, 2022Comments
-
Tara Prasad Gurung almost 2 years
My Requirement:
For now when I run my python application with this command
python main.py -d listhere/users.txt
The program will run and save the result file as predefined name say reports.txt
Now I want to add this functionality to allow users to choose what to put the filename and where to save as so
python main.py -d -o output/newfilname -i listhere/users.txt
Everything is same but I want another argument -o to be passed which will determine the filpath and name to be saved. How do I do it. What is the best way to handle or combine multiple options.
I tried this
parser = argparse.ArgumentParser(description = "CHECK-ACCESS REPORTING.") parser.add_argument('--user','-d', nargs='?') parser.add_argument('--output','-d -o', nargs='?') parser.add_argument('--input','-i', nargs='?') args = parser.parse_args(sys.argv[1:]) if args.output and args.input: #operation that involves output filename too elif args.user and not args.input: #default operation only else: #notset
I am getting this error when trying to solve the issue this way
Error:
report.py: error: unrecognized arguments: -o listhere/users.txt
-
Tara Prasad Gurung almost 7 yearsThe problem there is you have to pass some value after -d flags I just want to pass the flag -d without any value to it
-
Heiko Oberdiek almost 7 years@TaraPrasadGurung The order of the options does not matter, e.g.: -o output.txt -i input.txt -d. Option -d does not have any value on the command line. If the option is used somewhere on the command line, then it sets variable
discrepancy
to true. -
Tara Prasad Gurung almost 7 yearsThanks very descriptive I was very much unclear about default ,const
-
hpaulj almost 7 yearsAnother thought:
-d -o
is split into 2 strings by thebash
; sometimes it helps to look atsys.argv
to seek whatargparse
has to work with. -
Tara Prasad Gurung almost 7 yearscan you consider this please
python testing.py -d -i somevalue -o somevalue
I dont want to pass value after -d which result in not satisfying the conditionif args.users: if args.input and args.output
-
Tara Prasad Gurung almost 7 yearsOk did it by setting the const="some value"