Argparse optional positional arguments?
Solution 1
Use nargs='?' (or nargs='*' if you need more than one dir)
parser.add_argument('dir', nargs='?', default=os.getcwd())
extended example:
>>> import os, argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-v', action='store_true')
_StoreTrueAction(option_strings=['-v'], dest='v', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None)
>>> parser.add_argument('dir', nargs='?', default=os.getcwd())
_StoreAction(option_strings=[], dest='dir', nargs='?', const=None, default='/home/vinay', type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args('somedir -v'.split())
Namespace(dir='somedir', v=True)
>>> parser.parse_args('-v'.split())
Namespace(dir='/home/vinay', v=True)
>>> parser.parse_args(''.split())
Namespace(dir='/home/vinay', v=False)
>>> parser.parse_args(['somedir'])
Namespace(dir='somedir', v=False)
>>> parser.parse_args('somedir -h -v'.split())
usage: [-h] [-v] [dir]
positional arguments:
dir
optional arguments:
-h, --help show this help message and exit
-v
Solution 2
As an extension to @VinaySajip answer. There are additional nargs worth mentioning.
-
parser.add_argument('dir', nargs=1, default=os.getcwd())
N (an integer). N arguments from the command line will be gathered together into a list
-
parser.add_argument('dir', nargs='*', default=os.getcwd())
'*'. All command-line arguments present are gathered into a list. Note that it generally doesn't make much sense to have more than one positional argument with nargs='*', but multiple optional arguments with nargs='*' is possible.
parser.add_argument('dir', nargs='+', default=os.getcwd())
'+'. Just like '*', all command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn’t at least one command-line argument present.
parser.add_argument('dir', nargs=argparse.REMAINDER, default=os.getcwd())
argparse.REMAINDER. All the remaining command-line arguments are gathered into a list. This is commonly useful for command line utilities that dispatch to other command line utilities
If the nargs keyword argument is not provided, the number of arguments consumed is determined by the action. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced.
Edit (copied from a comment by @Acumenus) nargs='?' The docs say: '?'. 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.
Waldo Bronchart
Updated on November 05, 2020Comments
-
Waldo Bronchart about 2 yearsI have a script which is meant to be used like this:
usage: installer.py dir [-h] [-v]diris a positional argument which is defined like this:parser.add_argument('dir', default=os.getcwd())I want the
dirto be optional: when it's not specified it should just becwd.Unfortunately when I don't specify the
dirargument, I getError: Too few arguments. -
Dolan Antenucci almost 10 yearsDo the?and*mean the same thing they mean in regular expressions (i.e.?requires 0 or 1, and*requiring 0 or more)? If so, does+work as well? -
Vinay Sajip almost 10 years@dolan: Yes,+works, too. See docs.python.org/2/library/argparse.html#nargs for the details. -
scagnetti about 8 yearsis there some way to get dir to show up in optional arguments? or it seems that positional arguments should have a preceeding 'optional' qualifier. is it possible to register (as far as help is concerned) it as such? -
Vinay Sajip about 8 years@ant From the above, you can see that dir is optional (that it appears in square brackets in argparse output indicates this). -
Asclepius about 6 yearsIt should be noted however thatnargs='?'does not produce a list. -
Matas Vaitkevicius about 6 years@A-B-B Last line of the answerGenerally this means a single command-line argument will be consumed and a single item (not a list) will be produced.Hope this helps... -
Asclepius about 6 yearsThe quoted line refers to the case of not definingnargs, butnargs='?'is defining it. The docs say: '?'. 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. -
Matas Vaitkevicius about 6 years@A-B-B Just edit the answer if you feel that something is missing. Thanks. -
SoloPilot almost 6 yearsOP was asking about positional params, not '--dir'. 'required' is an invalid argument for positionals. And 'false' was a typo, she meant 'False'. +1 for newbie, -1 for sloppiness. -
ptim almost 5 yearsTx! Access dir fromoptions.dir, notargs.dir, as I was trying! -
Admin over 4 yearsWhat is the difference betweennargs=argparse.REMAINDERandnargs='*', as it seems to me, they are identical in their effect (tested in Python 2.7.10 and Python 3.6.1)? -
Gabriel Staples over 3 yearsHere's the updated (Python 3) documentation--a careful reading of it explains it all: docs.python.org/3/library/argparse.html#nargs. For anyone new to theargparsemodule, start with the tutorial: docs.python.org/3/howto/argparse.html -
Joonho Park almost 3 years@dolan and Vinay, '+' does not work because it requires at least one argument at the command line.
-
Karthik Sunil over 2 yearsWe can not userequiredfor positional argument. -
violet 12 monthsnote also that
defaultis required to make this work. without specifying a default value, executing the parsing command without a value in that position will complain that a value is required for that argument.