3

I'm creating a script that takes both positional and optional arguments with argparse. I have gone through Doug's tutorial and the python Docs but can't find an answer.

parser = argparse.ArgumentParser(description='script to run')
parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'),
parser.add_argument('inputString', action='store', nargs='?') 
parser.add_argument('-option1', metavar='percent', type=float, action='store')
parser.add_argument('-option2', metavar='outFile1', type=argparse.FileType('w'),
parser.add_argument('-option3', action='store', default='<10',
args = parser.parse_args()
# rest of script.... blah blah

As you can see, I want 2 positional and 3 optional arguments. However, when I try to run it in the terminal, it doesn't check for the positionals! If I try: python script.py inputfile it will run normally and output error halfway through the script when it cannot find a value for inputString. If I try: python script.py xxx ; the output is:

usage script.py [-h] [-option1] [-option2] [-option3]

Can anyone explain why it doesn't check for the positional arguments?

jfs
417k210 gold badges1k silver badges1.7k bronze badges
asked Nov 1, 2012 at 10:18
3
  • If you want inputFile and` inputString to be required arguments, why are you setting nargs='?'? Commented Nov 1, 2012 at 10:26
  • Ok, I tried removing nargs and it worked! I was using another example and the python docs specify nargs as consuming 1 argument. I used it so that args.inputFile won't consume both the names given for inputFile and inputString. So is it correct now to say that, you only use nargs for optional arguments? Thanks a lot btw! Commented Nov 1, 2012 at 10:34
  • You can also use nargs='*' or nargs='+' for multiple arguments; see my answer below and the documentation. Commented Nov 1, 2012 at 10:45

3 Answers 3

7

Your problem is that you're specifying nargs='?'. From the documentation:

'?'. One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from default will be produced.

If you leave out the nargs='?' then the argument will be required, and argparse will display an error if it is not provided. A single argument is consumed if action='store' (the default).

You can also specify nargs=1; the difference is that this produces a list containing one item, as opposed to the item itself. See the documentation for more ways you can use nargs.

answered Nov 1, 2012 at 10:42
Sign up to request clarification or add additional context in comments.

Comments

2

Works for me.

Code:

#!/usr/bin/python
import argparse
parser=argparse.ArgumentParser(description='script to run')
parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'))
parser.add_argument('inputString', action='store', nargs='?') 
parser.add_argument('-option1', metavar='percent', type=float, action='store')
parser.add_argument('-option2', metavar='outFile1', type=argparse.FileType('w'))
parser.add_argument('-option3', action='store', default='<10')
args = parser.parse_args()

Execution:

# ./blah.py -h
usage: blah.py [-h] [-option1 percent] [-option2 outFile1] [-option3 OPTION3]
 [inputFile] [inputString]
script to run
positional arguments:
 inputFile
 inputString
optional arguments:
 -h, --help show this help message and exit
 -option1 percent
 -option2 outFile1
 -option3 OPTION3

Did you overlook the second line in the argument list?

answered Nov 1, 2012 at 10:25

3 Comments

oh right, I missed the last two positional arguments stated in the usage [-h]. Sorry, my bad. Thanks for pointing it out. But I still have problems with the script running without a 2nd positional argument.
By the way is there a way to rearrange help display so that it will show usage blah.py [inputFile] [inputString] [option1].....? Thanks!
Sorry, I saw your reply before the comment on my question. The reply by @ecatmur on removing nargs solved my problem. Basically it was that the script could run with python blah.py inputFile without inputString given.
1

It works as expected. There is no inputString if you run it as script.py inputfile (only one argument is given, but inputString is the second argument).

narg='?' means that the argument is optional (they are surrounded by [] in the help message).

answered Nov 1, 2012 at 10:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.