In Python, how can I (without reinventing the argparse wheel) support a command-line option syntax à la sed -i in which one option takes an optional argument if & only if there is no whitespace between the option and its argument?
Naïvely, I'd expect argparse to support this by setting nargs='?', like so:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-i', dest='backup', nargs='?', const='')
>>> parser.add_argument('arg', nargs='*')
... but it doesn't:
>>> parser.parse_args(['-i', '~'])
Namespace(backup='~', arg=[]) # wanted: Namespace(backup='', arg=['~'])
What options are available to me? I'd prefer answers that work in both Python 2.7 and Python 3.3+.
Quick and dirty workaround here.
First, manually check the sys.argv[1:] and check if the option (with nargs='?', const='') is used with no optional argument (including -o posarg case that we are trying to resolve here).
If that so, modify that in a way to prevent it from consuming the trailing positional arguments, by appending = at the end of it.
Then explicitly give the modified args to the parser.parse_args(argv) function.
Following is the example script:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='backup', nargs='?', const='')
parser.add_argument('arg', nargs='*')
print(parser.parse_args(['-i', '~'])) # gives Namespace(arg=[], backup='~')
print(parser.parse_args(['-i=', '~'])) # gives Namespace(arg=['~'], backup='')
Related
I want to write a argparse command that needs two postional arguments when I don't set a optional argument. In my case it's like I want to call it with two necessary parameters but when I say python3 test.py -gui I want that you don't need this two arguments, because then you are using the gui.
Thx
This is what I was proposing in the comments:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--gui', action='store_true', help="use GUI")
parser.add_argument('args', nargs='*')
cmdargs = parser.parse_args()
nargs = len(cmdargs.args)
nargs_expected = 0 if cmdargs.gui else 2
if nargs != nargs_expected:
parser.error(f"{nargs_expected} arguments were expected, but got {nargs}")
I have a script which is meant to be used like this:
usage: installer.py dir [-h] [-v]
dir is a positional argument which is defined like this:
parser.add_argument('dir', default=os.getcwd())
I want the dir to be optional: when it's not specified it should just be cwd.
Unfortunately when I don't specify the dir argument, I get Error: Too few arguments.
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
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.
Short Answer
As already shown in the previous two answers, you can accept an optional positional argument with nargs='?'. You could also turn the argument directly into a Path type and/or shorten the cwd to . if you wanted to:
myfile.py
import argparse
import pathlib
parser = argparse.ArgumentParser()
parser.add_argument("dir", nargs="?", default=".", type=pathlib.Path)
parsed_args = parser.parse_args()
print("Installing to", parsed_args.dir.resolve())
$ python myfile.py
Installing to /users/myname/myfolder
$ python myfile.py /usr/bin/
Installing to /usr/bin
Longer answer
Since you also mention the flag-style True/False options -h and -v in your question, these examples may be of use:
Flags (e.g. -v)
We might refer to optional options that take no arguments as "flags". With flags, we only care about whether they are given or not. -h is a flag that argparse adds automatically (along with the longer version --help) so we shouldn't really override that. If we consider -v then,
myfile.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--version",
action="store_true")
parsed_args = parser.parse_args()
if parsed_args.version:
print("version flag given")
else:
print("version flag not given")
Note that the second argument to add_argument() is a longer name for the option. It is not mandatory but it does make your subsequent code more readable (parsed_args.version vs parsed_args.v) and makes calls to your installer more explicit.
$ python myfile.py -v
version flag given
$ python myfile.py --version
version flag given
$ python myfile.py
version flag not given
Optional arguments (e.g. --installdir /usr/bin/)
One could argue that, in your case, you would be better off with an optional argument rather than a positional one.
myfile.py
import argparse
import pathlib
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--installdir", # Optional (but recommended) long version
type=pathlib.Path,
default="/bin"
)
parsed_args = parser.parse_args()
print("Installing to", parsed_args.installdir)
$ python myfile.py -i /usr/bin/
Installing to /usr/bin
$ python myfile.py --installdir /usr/bin/
Installing to /usr/bin
$ python myfile.py
Installing to /bin
parser.add_argument also has a switch required. You can use required=False.
Here is a sample snippet with Python 2.7:
parser = argparse.ArgumentParser(description='get dir')
parser.add_argument('--dir', type=str, help='dir', default=os.getcwd(), required=False)
args = parser.parse_args()
I want to create a command parser mycommand, using argparse, with two subcommands read and write: read should have just one argument which is some path, and write should have two arguments one of which is a path and the other a value. It should be possible to execute the command in the following way:
mycommand read <path>
mycommand write <path> <value>
without using labels for the <path>, <value> arguments, i.e. without requiring --path. How can I do this?
This is pretty straight forward following the docs:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
read = subparsers.add_parser('read')
read.add_argument('path')
write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')
print(parser.parse_args(['read', 'foo']))
print(parser.parse_args(['write', 'foo', 'bar']))
Note that this doesn't tell you what parser parsed the arguments. If you want that, you can simply add a dest to the add_subparsers command:
subparsers = parser.add_subparsers(dest='subparser')
Finally, you can add a default attribute for each subparser that you can use to perform the actions specific to that subparser. This is spelled out in the docs too, but for completeness, in our example, it might look something like this1:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser')
def handle_read(args):
print('Handling read')
print(args)
read = subparsers.add_parser('read')
read.add_argument('path')
read.set_defaults(handler=handle_read)
def handle_write(args):
print('Handling write')
print(args)
write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')
write.set_defaults(handler=handle_write)
args = parser.parse_args()
args.handler(args)
1I added the dest to subparsers in this example too for illustrative purposes -- Using argparse with handler functions probably makes that attribute on args obsolete.
I am trying to make a parser using argparse than can parse the following commands:
python prog.py update <DOMAIN> <ENVIRONMENT>
python prog.py pull <DOMAIN> <ENVIRONMENT>
python prog.py release <DOMAIN> <ENVIRONMENT>
As you can see, both update, pull and release take the same arguments <DOMAIN> and <ENVIRONMENT>.
All three of them are subparsers of the main parser.
I wrote the following:
import argparse
# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG', add_help=False)
parser.add_argument('domain', type=str, help='domain help')
parser.add_argument('environment', type=str, help='environment help')
#subparsers
subparsers = parser.add_subparsers(help='sub-command help', parents=[parser])
parser_pull = subparsers.add_parser('pull', help='pull help')
parser_update = subparsers.add_parser('update', help='update help')
print parser_pull.parse_args(['pull', 'WEBAPPS', 'DEV'])
print parser.parse_args(['update', 'WEBAPPS', 'DEV'])
but it seems that domain and environment are expected BEFORE the subcommands update, pull and release, so it throws an error.
How can I make it required to accept those arguments after the subcommands, without duplicating code inside each subcommand ?
For the record, I use Python 2.7.
Go ahead and duplicate the code. A little cut and paste is not that much work.
Positional arguments have to be given in a certain order. And .add_subparsers creates one of those positionals (one that expects values like 'pull','update'. So the order of the subparse command, positionals defined for the main parser, and positionals for the subparsers matters.
There is a parents mechanism, which can save some typing. But I hesitate to recommend it because it can cause problems (previous SO questions demonstrate this). Simply biting the bullet and entering the positional arguments where they are expected is the surest approach.
Don't forget that you can create subparsers in a loop or with helper functions - saving one kind of typing for another.
For example, after creating the subparsers:
for p in parser_pull, parser_update:
p.add_argument('domain', type=str, help='domain help')
p.add_argument('environment', type=str, help='environment help')
I used the OptParse module a few years back, and it seemed so easy, but seeing the argparse module, it doesn't seem intuitive to use. Thus, I'd like some help.
I currently have (which isnt much yet):
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
I'd like to be able to issue a command like python myscript.py --char 20 . This value for char flag will always be an int.
If someone can please help, I'd greatly appreciate it! Thank you
This is how you add an argument, and retrieve it:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--char', type=int,
help='number of characters')
args = parser.parse_args()
char = args.char
You should check out the docs:
https://docs.python.org/3/library/argparse.html
And here's a tutorial:
https://docs.python.org/2/howto/argparse.html
you need to add an argument to the parser object, and optionally specify the parameter type to be int
# testargs.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--char', type=int)
args = parser.parse_args()
print(args.char)
if you execute this file with
python testargs.py --char 20
it should print 20 to the console