Multiple invocation of the same subcommand in a single command line - python

I'm trying to figure out how to use argparser to do the following:
$ python test.py executeBuild --name foobar1 executeBuild --name foobar2 ....
getBuild itself is a sub-command. My goal is to have the script have the capability to chain a series of sub-command (executeBuild being one of them) and execute them in order. In the example above, it would execute a build, then setup the environment, then execute build again. How can I accomplish this with argparse? I've tried the following:
main_parser = argparse.ArgumentParser(description='main commands')
subparsers = main_parser.add_subparsers(help='SubCommands', dest='command')
build_parser = subparsers.add_parser('executeBuild')
build_parser.add_argument('--name', action='store', nargs=1, dest='build_name')
check_parser = subparsers.add_parser('setupEnv')
args, extra=main_parser.parse_known_args()
However, it appears that whenever I do this, it goes into the subcommand of executeBuild and report it doesn't know what executeBuild is. I've tried parsing out the extra so I can do a repeat call / chain, however, the first view property appears to have been overwritten, so I can't even just save the extra options and iterate thru.

You are asking argparse something it was not written for : it is good at parsing one command line (but only one) and you want to parse multiple commands in one single line. IMHO, you have to do an initial splitting on your arguments array, and then use argparse on each subcommand. Following function takes a list of arguments (could be sys.argv), skips the first and split remaining in arrays beginning on each known subcommand. You can then use argparse on each sublist :
def parse(args, subcommands):
cmds = []
cmd = None
for arg in args[1:]:
if arg in (subcommands):
if cmd is not None:
cmds.append(cmd)
cmd = [arg]
else:
cmd.append(arg)
cmds.append(cmd)
return cmds
In your example :
parse(['test.py', 'executeBuild', '--name', 'foobar1', 'executeBuild', '--name', 'foobar2'],
('executeBuild',))
=>
[['executeBuild', '--name', 'foobar1'], ['executeBuild', '--name', 'foobar2']]
Limits : subcommands are used as reserved words and cannot be used as option arguments.

Splitting sys.argv before hand is a good solution. But it can also be done while parsing using an argument with nargs=argparse.REMAINDER. This type of argument gets the rest of the strings, regardless of whether they look like flags or not.
Replacing the parse_known_args with this code:
...
build_parser.add_argument('rest', nargs=argparse.REMAINDER)
check_parser.add_argument('rest', nargs=argparse.REMAINDER)
extras = 'executeBuild --name foobar1 setupEnv executeBuild --name foobar2'.split()
# or extras = sys.argv[1:]
while extras:
args = main_parser.parse_args(extras)
extras = args.rest
delattr(args,'rest')
print args
# collect args as needed
prints:
Namespace(build_name=['foobar1'], command='executeBuild')
Namespace(command='setupEnv')
Namespace(build_name=['foobar2'], command='executeBuild')
In the documentation:
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:
A problem with REMAINDER is that can be too greedy. http://bugs.python.org/issue14174. As a result build_parser and check_parser can't have other positional arguments.
A way around the greedy REMAINDER is to use argparse.PARSER. This is the nargs value that subparsers uses (undocumented). It's like REMAINDER, except that the first string must look like an 'argument' (no '-'), and is matched against choices (if given). PARSER isn't as greedy as REMAINDER, so the subparsers can have other positional arguments.
There's some extra code involving an 'exit' string and dummy parser. This is to get around the fact that the PARSER argument is 'required' (somewhat like nargs='+')
from argparse import ArgumentParser, PARSER, SUPPRESS
main_parser = ArgumentParser(prog='MAIN')
parsers = {'exit': None}
main_parser.add_argument('rest',nargs=PARSER, choices=parsers)
build_parser = ArgumentParser(prog='BUILD')
parsers['executeBuild'] = build_parser
build_parser.add_argument('cmd')
build_parser.add_argument('--name', action='store', nargs=1, dest='build_name')
build_parser.add_argument('rest',nargs=PARSER, choices=parsers, help=SUPPRESS)
check_parser = ArgumentParser(prog='CHECK')
parsers['setupEnv'] = check_parser
check_parser.add_argument('cmd')
check_parser.add_argument('foo')
check_parser.add_argument('rest',nargs=PARSER, choices=parsers, help=SUPPRESS)
argv = sys.argv[1:]
if len(argv)==0:
argv = 'executeBuild --name foobar1 setupEnv foo executeBuild --name foobar2'.split()
argv.append('exit') # extra string to properly exit the loop
parser = main_parser
while parser:
args = parser.parse_args(argv)
argv = args.rest
delattr(args,'rest')
print(parser.prog, args)
parser = parsers.get(argv[0], None)
sample output:
('MAIN', Namespace())
('BUILD', Namespace(build_name=['foobar1'], cmd='executeBuild'))
('CHECK', Namespace(cmd='setupEnv', foo='foo'))
('BUILD', Namespace(build_name=['foobar2'], cmd='executeBuild'))
Another possibility is to use '--' to separate command blocks:
'executeBuild --name foobar1 -- setupEnv -- executeBuild --name foobar2'
However there is problem when there are several '--': http://bugs.python.org/issue13922

Related

combining argsparse and sys.args in Python3

I'm trying to write a command line tool for Python that I can run like this..
orgtoanki 'b' 'aj.org' --delimiter="~" --fields="front,back"
Here's the script:
#!/usr/bin/env python3
import sys
import argparse
from orgtoanki.api import create_package
parser = argparse.ArgumentParser()
parser.add_argument('--fields', '-f', help="fields, separated by commas", type=str, default='front,back')
parser.add_argument('--delimiter', '-d', help="delimiter", type= str, default='*')
args = parser.parse_args()
name=sys.argv[1]
org_src=sys.argv[2]
create_package(name, org_src, args.fields, agrs.delimiter)
When I run it, I get the following error:
usage: orgtoanki [-h] [--fields FIELDS] [--delimiter DELIMITER]
orgtoanki: error: unrecognized arguments: b aj.org
Why aren't 'b' and 'ab.org' being interpreted as sys.argv[1] and sys.argv[2], respectively?
And will the default work as I expect it to, if fields and delimiter aren't supplied to the command line?
The error here is caused by argparse parser which fails to apprehend the 'b' 'aj.org' part of the command, and your code never reaches the lines with sys.argv. Try adding those arguments to the argparse and avoid using both argparse and sys.argv simultaneously:
parser = argparse.ArgumentParser()
# these two lines
parser.add_argument('name', type=str)
parser.add_argument('org_src', type=str)
parser.add_argument('--fields', '-f', help="fields, separated by commas",
type=str, default='front,back')
parser.add_argument('--delimiter', '-d', help="delimiter",
type= str, default='*')
args = parser.parse_args()
You then can access their values at args.name and args.org_src respectively.
The default input to parser.parse_args is sys.argv[1:].
usage: orgtoanki [-h] [--fields FIELDS] [--delimiter DELIMITER]
orgtoanki: error: unrecognized arguments: b aj.org
The error message was printed by argparse, followed by an sys exit.
The message means that it found strings in sys.argv[1:] that it wasn't programmed to recognize. You only told it about the '--fields' and '--delimiter' flags.
You could add two positional fields as suggested by others.
Or you could use
[args, extras] = parser.parse_known_args()
name, org_src = extras
extras should then be a list ['b', 'aj.org'], the unrecognized arguments, which you could assign to your 2 variables.
Parsers don't (usually) consume and modify sys.argv. So several parsers (argparse or other) can read the same sys.argv. But for that to work they have to be forgiving about strings they don't need or recognize.

Python argparse requiring option, depending on the defined flags

I have a small python script, which uses argparse to let the user define options. It uses two flags for different modes and an argument to let the user define a file. See the simplified example below:
#!/usr/bin/python3
import argparse
from shutil import copyfile
def check_file(f):
# Mock function: checks if file exists, else "argparse.ArgumentTypeError("file not found")"
return f
def main():
aFile = "/tmp/afile.txt"
parser = argparse.ArgumentParser(description="An example",formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--file", help="A file, used with method A.", default=aFile, type=check_file)
parser.add_argument("-a", "--ay", help="Method A, requires file.", action='store_true')
parser.add_argument("-b", "--be", help="Method B, no file required.", action='store_true')
args = parser.parse_args()
f = args.file
a = args.ay
b = args.be
if a:
copyfile(f, f+".a")
elif b:
print("Method B")
if __name__ == "__main__":
main()
Method A requires the file.
Method B does not.
If I run the script with method A, I either use the default file or one that is defined with -f/--file. The script checks if the file exists and everything is fine.
Now, if I run the script with method B, it shouldn't require the file, but the default option is checked and if it doesn't exist the argparse function raises the exception and the script exits.
How can I configure argparse to make -f optional, if -b is defined and require it, if -a is defined?
edit: I just realized that it would be enough for me to make -f and -b mutually exclusive. But then, if I run -b only, the check_file is executed anyways. Is there a way to prevent that?
#!/usr/bin/python3
import argparse
from shutil import copyfile
def check_file(f):
# Mock function: checks if file exists, else "argparse.ArgumentTypeError("file not found")"
print("chk file")
return f
def main():
aFile = "/tmp/afile.txt"
parser = argparse.ArgumentParser(description="An example",formatter_class=argparse.RawTextHelpFormatter)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-f", "--file", help="A file, used with method A.", default=aFile, type=check_file)
parser.add_argument("-a", "--ay", help="Method A, requires file.", action='store_true')
group.add_argument("-b", "--be", help="Method B, no file required.", action='store_true')
args = parser.parse_args()
f = args.file
a = args.ay
b = args.be
if a:
print("File: "+str(f))
elif b:
print("Method B")
print("file: "+str(f))
if __name__ == "__main__":
main()
Output:
chk file
Method B
file: /tmp/afile.txt
You can defined subparser with ay/be as subcommand or alternatively declare a second parser instance for a. Something like:
parser = argparse.ArgumentParser(
description="An example",
formatter_class=argparse.RawTextHelpFormatter
)
# ensure either option -a or -b only
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-a", "--ay", help="Method A, requires file.",
action='store_true')
group.add_argument("-b", "--be", help="Method B, no file required.",
action='store_true')
# define a parser for option -a
parser_a = argparse.ArgumentParser()
parser_a.add_argument("-f", "--file", help="A file, used with method A.",
type=check_file, required=True)
parser_a.add_argument("-a", "--ay", help="Method A, requires file.",
action='store_true')
# first parse - get either -a/-b
args = parser.parse_known_args(sys.argv[1:])
# if -a, use the second parser to ensure -f is in argument
# note parse_known_args return tuple, the first one is the populated namespace
if args[0].ay:
args = parser_a.parse_args(sys.argv[1:])
Your problem lies with how argparse handles defaults. You'd get this behavior even if -f was the only argument. If the default is a string value, it will be 'evaluated' if the Action isn't seen.
parser.add_argument("-f", "--file", help="A file, used with method A.", default=aFile, type=check_file)
At the start of parsing defaults are put into the args namespace. During parsing it keeps track of whether Actions have been seen. At the end of parsing it checks Namespace values for Actions which haven't been seen. If they match the default (the usual case) and are strings, it passes the default through the type function.
In your -f case, the default is probably a file name, a string. So it will be 'evaluated' if the user doesn't provide an alternative. In earlier argparse versions defaults were evaluate regardless of whether they were used or not. For something like a int or float type that wasn't a problem, but for FileType it could result in unneeded file opening/creation.
Ways around this?
write check_file so it gracefully handles aFile.
make sure aFile is valid so check_file runs without error. This the usual case.
use a non-string default, e.g. an already open file.
use the default default None, and add the default value after parsing.
if args.file is None:
args.file = aFile
Combining this with -a and -b actions you have to decide whether:
if -a, is a -f value required? If -f isn't provided, what's the right default.
if -b, does it matter whether -f has a default or whether the user provides this argument? Could you just ignore it?
If -f is useful only when -a is True, why not combine them?
parser.add_argument('-a', nargs='?', default=None, const='valid_file', type=check_file)
With ?, this works in 3 ways. (docs on const)
no -a, args.a = default
bare -a, args.a = const
-a afile,args.a = afile
An even simpler example of this behavior
In [956]: p = argparse.ArgumentParser()
In [957]: p.add_argument('-f',type=int, default='astring')
...
In [958]: p.parse_args('-f 1'.split())
Out[958]: Namespace(f=1)
In [959]: p.parse_args(''.split())
usage: ipython3 [-h] [-f F]
ipython3: error: argument -f: invalid int value: 'astring'
The string default is passed through int resulting in an error. If I'd set default to something else like a list, default=[1,2,3], it would have run even though int would have choked on the default.

Using GNU-style long options in argparse (not confusing optional argument with positional)

For example with GNU ls you can control coloring by using the --color[=WHEN] option. Now in this case the equal sign is crucial since ls have to distinguish between an optional argument to --color and positional arguments (which is the files to list). That is ls --color lists file with colors, which is the same as ls --color=always, but ls --color always will list the file always (and with colors).
Now from what I've seen argparse seem to accept arguments to long options using the --longopt <argument> syntax as well which will lead to not being able to make the argument optional. That is if I try to implement myls with the same behavior as GNU ls (that's just an example) I would run into problems as now myls --color always means the same as myls --color=always (and not as required --color without argument and always as a positional argument).
I know that I can circumvent this by using myls --color -- always, but isn't there a way to make this work without that workaround? That is to tell argparse that the argument to --color has to be supplied with the --color[=WHEN] syntax.
Note that I don't want to rely on the fact that the --color option has finite number of valid arguments. Here's an example what I've tried that didn't work properly:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--foo",
action="store",
nargs="?")
parser.add_argument("frob",
action="store",
nargs=argparse.REMAINDER)
print(parser.parse_args(["alpha", "beta"]))
print(parser.parse_args(["--foo", "alpha", "beta"]))
print(parser.parse_args(["--foo=bar", "alpha", "beta"]))
With the output:
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo='alpha', frob=['beta'])
Namespace(foo='bar', frob=['alpha', 'beta'])
note the second where alpha was interpreted as argument to --foo. I wanted:
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo=None, frob=['alpha', 'beta'])
Namespace(foo='bar', frob=['alpha', 'beta'])
You've probably already tried the ? optional followed by required positional:
p=argparse.ArgumentParser()
p.add_argument('--foo', nargs='?',default='one', const='two')
p.add_argument('bar')
which fails with
In [7]: p.parse_args('--foo 1'.split())
usage: ipython3 [-h] [--foo [FOO]] bar
ipython3: error: the following arguments are required: bar
--foo consumes the 1, leaving nothing for bar.
http://bugs.python.org/issue9338 discusses this issue. The nargs='?' is greedy, consuming an argument, even though the following positional requires one. But the suggested patch is complicated, so I can't quickly apply it to a parser and test your case.
The idea of defining an Action that would work with --foo==value, but not consume value in --foo value, is interesting, but I have no idea of what it would take to implement. Certainly it doesn't work with the current parser. I'd have to review how it handles that explicit =.
============================
By changing a deeply nested function in parse_args,
def consume_optional(....):
....
# error if a double-dash option did not use the
# explicit argument
else:
msg = _('ignored explicit argument %r')
#raise ArgumentError(action, msg % explicit_arg)
# change for stack40989413
print('Warn ',msg)
stop = start_index + 1
args = [explicit_arg]
action.nargs=None
action_tuples.append((action, args, option_string))
break
and adding a custom Action class:
class MyAction(myparse._StoreConstAction):
# requies change in consume_optional
def __call__(self, parser, namespace, values, option_string=None):
if values:
setattr(namespace, self.dest, values)
else:
setattr(namespace, self.dest, self.const)
I can get the desired behavior from:
p = myparse.ArgumentParser()
p.add_argument('--foo', action=MyAction, const='C', default='D')
p.add_argument('bar')
Basically I'm modifying store_const to save the =explicit_arg if present.
I don't plan on proposing this as a formal patch, but I'd welcome feedback if it is useful. Use at your own risk. :)
Apparently this is not possible. This behaviour is supported by GNU getopt() (man getopt, man 3 getopt). man getopt says:
If the [long] option has an optional argument, it must be written directly after the long option name, separated by '=', if present
The Python getopt module, however, is clear that it doesn't support this:
Optional arguments [in long options] are not supported.
For argparse I don't find any specific reference in the manual, but I would be surprised if it supported it. In fact, I'm surprised GNU getopt supports it and that ls works the way you described. User interfaces should be simple, and this behaviour is far from simple.
Here's a workaround:
#!/usr/bin/python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="*", help="List of files", type=str)
parser.add_argument('--color', dest='color', action='store_true')
parser.add_argument('--color=ALWAYS', dest='color_always', action='store_true')
args = parser.parse_args()
print args
Results:
[~]$ ./test.py xyz --color
Namespace(color=True, color_always=False, files=['xyz'])
[~]$ ./test.py xyz --color=ALWAYS
Namespace(color=False, color_always=True, files=['xyz'])
Problemo solved!
It's a bit (a lot) hacky, but here you go.
The solution revolves around a class that inherits _StoreConstAction and tweaks it a little, but mainly tricks the help formatter when it tries to get its attributes.
I tested this in python3 under windows and linux.
import argparse
import inspect
class GnuStyleLongOption(argparse._StoreConstAction):
def __init__(self, **kw):
self._real_option_strings = kw['option_strings']
opts = []
for option_string in self._real_option_strings:
opts.append(option_string)
for choice in kw['choices']:
opts.append(f'{option_string}={choice}')
kw['option_strings'] = opts
self.choices = kw.pop('choices')
help_choices = [f"'{choice}'" for choice in self.choices]
kw['help'] += f"; {kw['metavar']} is {', or '.join([', '.join(help_choices[:-1]), help_choices[-1]])}"
super(GnuStyleLongOption, self).__init__(**kw)
def __getattribute__(self, attr):
caller_is_argparse_help = False
for frame in inspect.stack():
if frame.function == 'format_help' and frame.filename.endswith('argparse.py'):
caller_is_argparse_help = True
break
if caller_is_argparse_help:
if attr == 'option_strings':
return [f'{i}[=WHEN]' for i in self._real_option_strings]
if attr == 'nargs':
return 0
if attr == 'metavar':
return None
return super(GnuStyleLongOption, self).__getattribute__(attr)
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, self.const if '=' not in option_string else option_string[option_string.find('=') + 1:])
p = argparse.ArgumentParser()
p.add_argument('--color', '--colour', action=GnuStyleLongOption, choices=['always', 'never', 'auto'], const='always', default='auto', help='use markers to highlight whatever we want', metavar='WHEN')
p.add_argument('filenames', metavar='filename', nargs='*', help='file to process')
args = p.parse_args()
print(f'color = {args.color}, filenames = {args.filenames}')
Results:
~ $ ./gnu_argparse.py --help
usage: gnu_argparse.py [-h] [--color[=WHEN]] [filename [filename ...]]
positional arguments:
filename file to process
optional arguments:
-h, --help show this help message and exit
--color[=WHEN], --colour[=WHEN]
use markers to highlight whatever we want; WHEN is
'always', 'never', or 'auto'
~ $ ./gnu_argparse.py
color = auto, filenames = []
~ $ ./gnu_argparse.py file
color = auto, filenames = ['file']
~ $ ./gnu_argparse.py --color file
color = always, filenames = ['file']
~ $ ./gnu_argparse.py --color never file
color = always, filenames = ['never', 'file']
~ $ ./gnu_argparse.py --color=never file
color = never, filenames = ['file']
~ $ ./gnu_argparse.py --colour=always file
color = always, filenames = ['file']
maybe nargs would be help.
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--color', nargs='?', const='c', default='d')
>>> parser.parse_args(['XX', '--color', 'always'])
Namespace(bar='XX', color='always')
>>> parser.parse_args(['XX', '--color'])
Namespace(bar='XX', color='c')
>>> parser.parse_args([])
Namespace(bar='d', color='d')
with nargs, you get different args and you will know what the input type is.
by the way, I think --color option could use action='store_true'.
parser.add_argument('--color', action='store_true')

Simple argparse example wanted: 1 argument, 3 results

The documentation for the argparse python module, while excellent I'm sure, is too much for my tiny beginner brain to grasp right now. I don't need to do math on the command line or meddle with formatting lines on the screen or change option characters. All I want to do is "If arg is A, do this, if B do that, if none of the above show help and quit".
Here's the way I do it with argparse (with multiple args):
parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f','--foo', help='Description for foo argument', required=True)
parser.add_argument('-b','--bar', help='Description for bar argument', required=True)
args = vars(parser.parse_args())
args will be a dictionary containing the arguments:
if args['foo'] == 'Hello':
# code here
if args['bar'] == 'World':
# code here
In your case simply add only one argument.
My understanding of the original question is two-fold. First, in terms of the simplest possible argparse example, I'm surprised that I haven't seen it here. Of course, to be dead-simple, it's also all overhead with little power, but it might get you started.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("a")
args = parser.parse_args()
if args.a == 'magic.name':
print 'You nailed it!'
But this positional argument is now required. If you leave it out when invoking this program, you'll get an error about missing arguments. This leads me to the second part of the original question. Matt Wilkie seems to want a single optional argument without a named label (the --option labels). My suggestion would be to modify the code above as follows:
...
parser.add_argument("a", nargs='?', default="check_string_for_empty")
...
if args.a == 'check_string_for_empty':
print 'I can tell that no argument was given and I can deal with that here.'
elif args.a == 'magic.name':
print 'You nailed it!'
else:
print args.a
There may well be a more elegant solution, but this works and is minimalist.
The argparse documentation is reasonably good but leaves out a few useful details which might not be obvious. (#Diego Navarro already mentioned some of this but I'll try to expand on his answer slightly.) Basic usage is as follows:
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--my-foo', default='foobar')
parser.add_argument('-b', '--bar-value', default=3.14)
args = parser.parse_args()
The object you get back from parse_args() is a 'Namespace' object: An object whose member variables are named after your command-line arguments. The Namespace object is how you access your arguments and the values associated with them:
args = parser.parse_args()
print (args.my_foo)
print (args.bar_value)
(Note that argparse replaces '-' in your argument names with underscores when naming the variables.)
In many situations you may wish to use arguments simply as flags which take no value. You can add those in argparse like this:
parser.add_argument('--foo', action='store_true')
parser.add_argument('--no-foo', action='store_false')
The above will create variables named 'foo' with value True, and 'no_foo' with value False, respectively:
if (args.foo):
print ("foo is true")
if (args.no_foo is False):
print ("nofoo is false")
Note also that you can use the "required" option when adding an argument:
parser.add_argument('-o', '--output', required=True)
That way if you omit this argument at the command line argparse will tell you it's missing and stop execution of your script.
Finally, note that it's possible to create a dict structure of your arguments using the vars function, if that makes life easier for you.
args = parser.parse_args()
argsdict = vars(args)
print (argsdict['my_foo'])
print (argsdict['bar_value'])
As you can see, vars returns a dict with your argument names as keys and their values as, er, values.
There are lots of other options and things you can do, but this should cover the most essential, common usage scenarios.
Matt is asking about positional parameters in argparse, and I agree that the Python documentation is lacking on this aspect. There's not a single, complete example in the ~20 odd pages that shows both parsing and using positional parameters.
None of the other answers here show a complete example of positional parameters, either, so here's a complete example:
# tested with python 2.7.1
import argparse
parser = argparse.ArgumentParser(description="An argparse example")
parser.add_argument('action', help='The action to take (e.g. install, remove, etc.)')
parser.add_argument('foo-bar', help='Hyphens are cumbersome in positional arguments')
args = parser.parse_args()
if args.action == "install":
print("You asked for installation")
else:
print("You asked for something other than installation")
# The following do not work:
# print(args.foo-bar)
# print(args.foo_bar)
# But this works:
print(getattr(args, 'foo-bar'))
The thing that threw me off is that argparse will convert the named argument "--foo-bar" into "foo_bar", but a positional parameter named "foo-bar" stays as "foo-bar", making it less obvious how to use it in your program.
Notice the two lines near the end of my example -- neither of those will work to get the value of the foo-bar positional param. The first one is obviously wrong (it's an arithmetic expression args.foo minus bar), but the second one doesn't work either:
AttributeError: 'Namespace' object has no attribute 'foo_bar'
If you want to use the foo-bar attribute, you must use getattr, as seen in the last line of my example. What's crazy is that if you tried to use dest=foo_bar to change the property name to something that's easier to access, you'd get a really bizarre error message:
ValueError: dest supplied twice for positional argument
Here's how the example above runs:
$ python test.py
usage: test.py [-h] action foo-bar
test.py: error: too few arguments
$ python test.py -h
usage: test.py [-h] action foo-bar
An argparse example
positional arguments:
action The action to take (e.g. install, remove, etc.)
foo-bar Hyphens are cumbersome in positional arguments
optional arguments:
-h, --help show this help message and exit
$ python test.py install foo
You asked for installation
foo
Yet another summary introduction, inspired by this post.
import argparse
# define functions, classes, etc.
# executes when your script is called from the command-line
if __name__ == "__main__":
parser = argparse.ArgumentParser()
#
# define each option with: parser.add_argument
#
args = parser.parse_args() # automatically looks at sys.argv
#
# access results with: args.argumentName
#
Arguments are defined with combinations of the following:
parser.add_argument( 'name', options... ) # positional argument
parser.add_argument( '-x', options... ) # single-char flag
parser.add_argument( '-x', '--long-name', options... ) # flag with long name
Common options are:
help: description for this arg when --help is used.
default: default value if the arg is omitted.
type: if you expect a float or int (otherwise is str).
dest: give a different name to a flag (e.g. '-x', '--long-name', dest='longName'). Note: by default --long-name is accessed with args.long_name
action: for special handling of certain arguments
store_true, store_false: for boolean args '--foo', action='store_true' => args.foo == True
store_const: to be used with option const '--foo', action='store_const', const=42 => args.foo == 42
count: for repeated options, as in ./myscript.py -vv '-v', action='count' => args.v == 2
append: for repeated options, as in ./myscript.py --foo 1 --foo 2 '--foo', action='append' => args.foo == ['1', '2']
required: if a flag is required, or a positional argument is not.
nargs: for a flag to capture N args ./myscript.py --foo a b => args.foo = ['a', 'b']
choices: to restrict possible inputs (specify as list of strings, or ints if type=int).
Note the Argparse Tutorial in Python HOWTOs. It starts from most basic examples, like this one:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
help="display a square of a given number")
args = parser.parse_args()
print(args.square**2)
and progresses to less basic ones.
There is an example with predefined choice for an option, like what is asked:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
print("{}^2 == {}".format(args.square, answer))
else:
print(answer)
Here's what I came up with in my learning project thanks mainly to #DMH...
Demo code:
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--flag', action='store_true', default=False) # can 'store_false' for no-xxx flags
parser.add_argument('-r', '--reqd', required=True)
parser.add_argument('-o', '--opt', default='fallback')
parser.add_argument('arg', nargs='*') # use '+' for 1 or more args (instead of 0 or more)
parsed = parser.parse_args()
# NOTE: args with '-' have it replaced with '_'
print('Result:', vars(parsed))
print('parsed.reqd:', parsed.reqd)
if __name__ == "__main__":
main()
This may have evolved and is available online: command-line.py
Script to give this code a workout: command-line-demo.sh
code file: argparseDemo.py
Simple: common case
name(abbr, full), with help
import argparse
argParser = argparse.ArgumentParser()
argParser.add_argument("-n", "--name", help="your name")
args = argParser.parse_args()
print("args=%s" % args)
print("args.name=%s" % args.name)
call
python argparseDemo.py -n Crifan
python argparseDemo.py --name Crifan
output: args=Namespace(name='Crifan') and args.name=Crifan
type
argParser.add_argument("-a", "--age", type=int, help="your current age")
print("type(args.age)=%s" % type(args.age))
call: python argparseDemo.py --age 30
output: type(args.age)=<class 'int'> and args.age=30
required
argParser.add_argument("-a", "--age", required=True, type=int, help="your current age")
call: python argparseDemo.py
output: error argparseDemo.py: error: the following arguments are required: -a/--age
default
argParser.add_argument("-a", "--age", type=int, default=20, help="your current age. Default is 20")
call: python argparseDemo.py
output: args.age=20
choices
argParser.add_argument("-f", "--love-fruit", choices=['apple', 'orange', 'banana'], help="your love fruits")
call: python argparseDemo.py -f apple
output: args=Namespace(love_fruit='apple') and args.love_fruit=apple
multi args
argParser.add_argument("-f", "--love-fruit", nargs=2, help="your love fruits")
call: python argparseDemo.py -f apple orange
output: args.love_fruit=['apple', 'orange']
Detail
most simple: -x
code:
import argparse
argParser = argparse.ArgumentParser()
argParser.add_argument("-a") # most simple -> got args.a, type is `str`
args = argParser.parse_args()
print("args.a=%s" % args.a)
usage = run in command line
python argparseDemo.py -a 30
or: ./argparseDemo.py -a 30
makesure argparseDemo.py is executable
if not, add it: chmod +x argparseDemo.py
output
args.a=30
Note
default type is str
argParser.add_argument("-a") == argParser.add_argument("-a", type=str)
print("type(args.a)=%s" % type(args.a)) -> type(args.a)=<class 'str'>
args type is Namespace
print("type(args)=%s" % type(args)) -> type(args)=<class 'argparse.Namespace'>
args value is Namespace(a='30')
print("args=%s" % args) -> args=Namespace(a='30')
so we can call/use args.a
parameter name
full parameter name: --xxx
code
argParser.add_argument("-a", "--age")
usage
python argparseDemo.py -a 30
or: python argparseDemo.py --age 30
get parsed value: args.age
Note: NOT args.a, and NOT exist args.a
full parameter name with multiple words: --xxx-yyy
code
argParser.add_argument("-a", "--current-age")
get parsed value: args.current_age
add help description: help
code
argParser.add_argument("-a", help="your age") # with help
output
use --help can see description
 python argparseDemo.py --help
usage: argparseDemo.py [-h] [-a A]
optional arguments:
-h, --help show this help message and exit
-a A your age
designate parameter type: type
code
argParser.add_argument("-a", type=int) # parsed arg is `int`, not default `str`
output
print("type(args.a)=%s" % type(args.a)) -> type(args.a)=<class 'int'>
print("args=%s" % args) -> args=Namespace(a=30)
add default value: default
code
argParser.add_argument("-a", type=int, default=20) # if not pass a, a use default value: 20
effect
usage: python argparseDemo.py
output: print("args.age=%s" % args.age) -> args=Namespace(a=20)
You could also use plac (a wrapper around argparse).
As a bonus it generates neat help instructions - see below.
Example script:
#!/usr/bin/env python3
def main(
arg: ('Argument with two possible values', 'positional', None, None, ['A', 'B'])
):
"""General help for application"""
if arg == 'A':
print("Argument has value A")
elif arg == 'B':
print("Argument has value B")
if __name__ == '__main__':
import plac
plac.call(main)
Example output:
No arguments supplied - example.py:
usage: example.py [-h] {A,B}
example.py: error: the following arguments are required: arg
Unexpected argument supplied - example.py C:
usage: example.py [-h] {A,B}
example.py: error: argument arg: invalid choice: 'C' (choose from 'A', 'B')
Correct argument supplied - example.py A :
Argument has value A
Full help menu (generated automatically) - example.py -h:
usage: example.py [-h] {A,B}
General help for application
positional arguments:
{A,B} Argument with two possible values
optional arguments:
-h, --help show this help message and exit
Short explanation:
The name of the argument usually equals the parameter name (arg).
The tuple annotation after arg parameter has the following meaning:
Description (Argument with two possible values)
Type of argument - one of 'flag', 'option' or 'positional' (positional)
Abbreviation (None)
Type of argument value - eg. float, string (None)
Restricted set of choices (['A', 'B'])
Documentation:
To learn more about using plac check out its great documentation:
Plac: Parsing the Command Line the Easy Way
To add to what others have stated:
I usually like to use the 'dest' parameter to specify a variable name and then use 'globals().update()' to put those variables in the global namespace.
Usage:
$ python script.py -i "Hello, World!"
Code:
...
parser.add_argument('-i', '--input', ..., dest='inputted_variable',...)
globals().update(vars(parser.parse_args()))
...
print(inputted_variable) # Prints "Hello, World!"
New to this, but combining Python with Powershell and using this template, being inspired by an in-depth and great Python Command Line Arguments – Real Python
There is a lot you can do within the init_argparse() and I am covering just the most simple scenario here.
import argparse
use if __name__ == "__main__": main() pattern to execute from terminal
parse arguments within the main() function that has no parameters as all
define a init_argparse() function
create a parser object by calling argparse.ArgumentParser()
declare one or more argumnent with parser.add_argument("--<long_param_name>")
return parser
parse args by creating an args object by calling parser.parse_args()
define a function proper with param1, param2, ...
call function_proper with params being assigned as attributes of an args object
e.g. function_proper(param1=args.param1, param2=args.param2)
within a shell call the module with named arguments:
e.g. python foobar.py --param1="foo" --param2=="bar"
#file: foobar.py
import argparse
def function_proper(param1, param2):
#CODE...
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("--param1")
parser.add_argument("--param2")
return parser
def main() -> None:
parser = init_argparse()
args = parser.parse_args()
function_proper(param1=args.param1, param2=args.param2)
if __name__ == "__main__":
main()
>>> python .\foobar.py --param1="foo" --param2=="bar"
I went through all the examples and answers and in a way or another they didn't address my need. So I will list her a scenario that I need more help and I hope this can explain the idea more.
Initial Problem
I need to develop a tool which is getting a file to process it and it needs some optional configuration file to be used to configure the tool.
so what I need is something like the following
mytool.py file.text -config config-file.json
The solution
Here is the solution code
import argparse
def main():
parser = argparse.ArgumentParser(description='This example for a tool to process a file and configure the tool using a config file.')
parser.add_argument('filename', help="Input file either text, image or video")
# parser.add_argument('config_file', help="a JSON file to load the initial configuration ")
# parser.add_argument('-c', '--config_file', help="a JSON file to load the initial configuration ", default='configFile.json', required=False)
parser.add_argument('-c', '--config', default='configFile.json', dest='config_file', help="a JSON file to load the initial configuration " )
parser.add_argument('-d', '--debug', action="store_true", help="Enable the debug mode for logging debug statements." )
args = parser.parse_args()
filename = args.filename
configfile = args.config_file
print("The file to be processed is", filename)
print("The config file is", configfile)
if args.debug:
print("Debug mode enabled")
else:
print("Debug mode disabled")
print("and all arguments are: ", args)
if __name__ == '__main__':
main()
I will show the solution in multiple enhancements to show the idea
First Round: List the arguments
List all input as mandatory inputs so second argument will be
parser.add_argument('config_file', help="a JSON file to load the initial configuration ")
When we get the help command for this tool we find the following outcome
(base) > python .\argparser_example.py -h
usage: argparser_example.py [-h] filename config_file
This example for a tool to process a file and configure the tool using a config file.
positional arguments:
filename Input file either text, image or video
config_file a JSON file to load the initial configuration
optional arguments:
-h, --help show this help message and exit
and when I execute it as the following
(base) > python .\argparser_example.py filename.txt configfile.json
the outcome will be
The file to be processed is filename.txt
The config file is configfile.json
and all arguments are: Namespace(config_file='configfile.json', filename='filename.txt')
But the config file should be optional, I removed it from the arguments
(base) > python .\argparser_example.py filename.txt
The outcome will be is:
usage: argparser_example.py [-h] filename config_file
argparser_example.py: error: the following arguments are required: c
Which means we have a problem in the tool
Second Round : Make it optimal
So to make it optional I modified the program as follows
parser.add_argument('-c', '--config', help="a JSON file to load the initial configuration ", default='configFile.json', required=False)
The help outcome should be
usage: argparser_example.py [-h] [-c CONFIG] filename
This example for a tool to process a file and configure the tool using a config file.
positional arguments:
filename Input file either text, image or video
optional arguments:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
a JSON file to load the initial configuration
so when I execute the program
(base) > python .\argparser_example.py filename.txt
the outcome will be
The file to be processed is filename.txt
The config file is configFile.json
and all arguments are: Namespace(config_file='configFile.json', filename='filename.txt')
with arguments like
(base) > python .\argparser_example.py filename.txt --config_file anotherConfig.json
The outcome will be
The file to be processed is filename.txt
The config file is anotherConfig.json
and all arguments are: Namespace(config_file='anotherConfig.json', filename='filename.txt')
Round 3: Enhancements
to change the flag name from --config_file to --config while we keep the variable name as is we modify the code to include dest='config_file' as the following:
parser.add_argument('-c', '--config', help="a JSON file to load the initial configuration ", default='configFile.json', dest='config_file')
and the command will be
(base) > python .\argparser_example.py filename.txt --config anotherConfig.json
To add the support for having a debug mode flag, we need to add a flag in the arguments to support a boolean debug flag. To implement it i added the following:
parser.add_argument('-d', '--debug', action="store_true", help="Enable the debug mode for logging debug statements." )
the tool command will be:
(carnd-term1-38) > python .\argparser_example.py image.jpg -c imageConfig,json --debug
the outcome will be
The file to be processed is image.jpg
The config file is imageConfig,json
Debug mode enabled
and all arguments are: Namespace(config_file='imageConfig,json', debug=True, filename='image.jpg')
A really simple way to use argparse and amend the '-h'/ '--help' switches to display your own personal code help instructions is to set the default help to False, you can also add as many additional .add_arguments as you like:
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-h', '--help', action='help',
help='To run this script please provide two arguments')
parser.parse_args()
Run: python test.py -h
Output:
usage: test.py [-h]
optional arguments:
-h, --help To run this script please provide two arguments
As an addition to existing answers, if you are lazy enough, it is possible to use code generation tool called protoargs. It generates arguments parser from the configuration. For python it uses argparse.
Configuration with optional A and B:
syntax = "proto2";
message protoargs
{
optional string A = 1; // A param description
optional string B = 2; // B param description
}//protoargs
Configuration with required A and B:
syntax = "proto2";
message protoargs
{
required string A = 1; // A param description
required string B = 2; // B param description
}//protoargs
Configuration with positional A and B:
syntax = "proto2";
message protoargs
{
required string A = 1; // A param description
required string B = 2; // B param description
}//protoargs
message protoargs_links
{
}//protoargs_links
Now all you should run is:
python ./protoargs.py -i test.proto -o . --py
And use it (it is possible to take other examples here):
import sys
import test_pa
class ArgsParser:
program = "test"
description = "Simple A and B parser test."
def parse(self, argv):
self.config = test_pa.parse(self.program, self.description, argv)
def usage(self):
return test_pa.usage(self.program, self.description)
if __name__ == "__main__":
parser = ArgsParser()
if len(sys.argv) == 1:
print(parser.usage())
else:
parser.parse(sys.argv[1:])
if parser.config.A:
print(parser.config.A)
if parser.config.B:
print(parser.config.B)
If you want more - change configuration, regenerate parser, use an updated parser.config.
UPD: As mentioned in rules, I must specify that this is my own project
code:
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-A', default=False, action='store_true')
parser.add_argument('-B', default=False, action='store_true')
args=parser.parse_args()
if args.A:
print('do this')
elif args.B:
print('do that')
else:
print('help')
running result:
$ python3 test.py
help
$ python3 test.py -A
do this
$ python3 test.py -B
do that
$ python3 test.py -C
usage: test.py [-h] [-A] [-B]
test.py: error: unrecognized arguments: -C
As for the original request (if A ....), I would use argv to solve it, not using argparse at all:
import sys
if len(sys.argv)==2:
if sys.argv[1] == 'A':
print('do this')
elif sys.argv[1] == 'B':
print('do that')
else:
print('help')
else:
print('help')
Since you have not clarified wheather the arguments 'A' and 'B' are positional or optional, I'll make a mix of both.
Positional arguments are required by default. If not giving one will throw 'Few arguments given' which is not the case for the optional arguments going by their name. This program will take a number and return its square by default, if the cube option is used it shall return its cube.
import argparse
parser = argparse.ArgumentParser('number-game')
parser.add_argument(
"number",
type=int,
help="enter a number"
)
parser.add_argument(
"-c", "--choice",
choices=['square','cube'],
help="choose what you need to do with the number"
)
# all the results will be parsed by the parser and stored in args
args = parser.parse_args()
# if square is selected return the square, same for cube
if args.c == 'square':
print("{} is the result".format(args.number**2))
elif args.c == 'cube':
print("{} is the result".format(args.number**3))
else:
print("{} is not changed".format(args.number))
usage
$python3 script.py 4 -c square
16
Here the optional arguments are taking value, if you just wanted to use it like a flag you can too. So by using -s for square and -c for cube we change the behaviour, by adding action = "store_true". It is changed to true only when used.
parser.add_argument(
"-s", "--square",
help="returns the square of number",
action="store_true"
)
parser.add_argument(
"-c", "--cube",
help="returns the cube of number",
action="store_true"
)
so the conditional block can be changed to,
if args.s:
print("{} is the result".format(args.number**2))
elif args.c:
print("{} is the result".format(args.number**3))
else:
print("{} is not changed".format(args.number))
usage
$python3 script.py 4 -c
64
The simplest answer!
P.S. the one who wrote the document of argparse is foolish
python code:
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--o_dct_fname',type=str)
parser.add_argument('--tp',type=str)
parser.add_argument('--new_res_set',type=int)
args = parser.parse_args()
o_dct_fname = args.o_dct_fname
tp = args.tp
new_res_set = args.new_res_set
running code
python produce_result.py --o_dct_fname o_dct --tp father_child --new_res_set 1

Don't parse options after the last positional argument

I'm writing a wrapper around the ssh command line client. After the first positional argument that's part of command, all further options should also be treated as positional arguments.
Under optparse, I believe this would be done with disable_interspersed_args.
Presently I have something like this:
parser = argparse.ArgumentParser()
parser.add_argument('--parallel', default=False, action='store_true')
# maybe allow no command? this would ssh interactively into each machine...
parser.add_argument('command', nargs='+')
args = parser.parse_args()
But if options are passed as part of the command (such as my_wrapper ls -l), they're instead interpreted by ArgumentParser as unknown options. error: unrecognized arguments: -l
If I use parse_known_args(), the options may be taken out of order.
p = argparse.ArgumentParser()
p.add_argument('-a', action='store_true')
p.add_argument('command', nargs='+')
print(p.parse_known_args())
$ python3 bah.py -b ls -l -a
(Namespace(a=True, command=['ls']), ['-b', '-l'])
Here you can see that -b's position before ls has been lost, and -a has been parsed out from the command, which is not desired.
How can I:
Prevent arguments from being parsed after a certain point?
Disable parsing of interspersed arguments?
Allow arguments with a prefix to be consumed as positional arguments?
I had the same problem. I found the solution on the argparse bug tracker: http://code.google.com/p/argparse/issues/detail?id=52
The solution is simple: replace nargs='+' (or '*') with nargs=argparse.REMAINDER. This special value is not documented, but it does what you want.
I think your best bet to start solving these issues is to try out -- after all your optional args. -- is a pseudo-arg that tells ArgumentParser that everything after is a positional argument. Docs are here
As for prevent arguments from being parsed after a certain point, you can pass part of argv to parse_args. That combined with some introspection can be used to limit what is parsed.
What #dcolish suggested is the universal approach. Here is a sample implementation which also supports the standard -- separator, but its usage is not required for correct parsing.
Result:
# ./parse-pos.py -h
usage: parse-pos.py [-h] [-qa] [-qb] COMMAND [ARGS...]
# ./parse-pos.py -qa ls -q -h aa /bb
try_argv = ['-qa', 'ls']
cmd_rest_argv = ['-q', '-h', 'aa', '/bb']
parsed_args = Namespace(command='ls', qa=True, qb=False)
The code:
#!/usr/bin/python3
import argparse
import sys
from pprint import pprint
class CustomParserError(Exception):
pass
class CustomArgumentParser(argparse.ArgumentParser):
def error(self, message):
raise CustomParserError(message)
def original_error(self, message):
super().error(message)
def parse_argv():
parser = CustomArgumentParser(description='Example')
parser.add_argument('command', metavar='COMMAND [ARGS...]', help='the command to be executed')
parser.add_argument('-qa', action='store_true') # "ambiguous option" if you specify just "-q"
parser.add_argument('-qb', action='store_true') # "ambiguous option" if you specify just "-q"
def parse_until_positional(parser, _sys_argv = None):
if _sys_argv is None:
_sys_argv = sys.argv[1:] # skip the program name
for i in range(0, len(_sys_argv) + 1):
try_argv = _sys_argv[0:i]
try:
parsed_args = parser.parse_args(try_argv)
except CustomParserError as ex:
if len(try_argv) == len(_sys_argv):
# this is our last try and we still couldn't parse anything
parser.original_error(str(ex)) # sys.exit()
continue
# if we are here, we parsed our known optional & dash-prefixed parameters and the COMMAND
cmd_rest_argv = _sys_argv[i:]
break
return (parsed_args, cmd_rest_argv, try_argv)
(parsed_args, cmd_rest_argv, try_argv) = parse_until_positional(parser)
# debug
pprint(try_argv)
pprint(cmd_rest_argv)
pprint(parsed_args)
return (parsed_args, cmd_rest_argv)
def main():
parse_argv()
main()
Another option is to use parse_known_args, which stops parsing when an unknown argument is encountered.

Categories

Resources