I have a python file which looks something like this :
import argparse
parser = argparse.ArgumentParser()
parser.add_argument()
etc...
args = parser.parse_args()
function_one(a=args.a, b=args.b)
function_two(c=args.c, d=args.d, e=args.e)
What I would like to have is the following
import argparse
parser = argparse.ArgumentParser()
parser_one = parser.add_parser(name1)
parser_one.add_argument('--a')
parser_two = parser.add_parser(name2)
parser_two.add_argument('--d')
args = parser.parse_args()
So that args would be something like a dictionary of dictionary and args.name1 would be the usual NameSpace.
This would enable me to divide my parser into subparser, and call the functions with :
function_one(**vars(args.name1))
function_two(**vars(args.name2))
I am aware of the function add_argument_group, but this merges the arguments into one NameSpace after calling parser_args.
And add_subparsers is also not the solution as it is exclusive between subparsers.
Related
In argparse, I want to prevent a particular combination of arguments. Lets see the sample code.
Sample:
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--firstname', dest='fn', action='store')
parser.add_argument('--lastname', dest='ln', action='store')
parser.add_argument('--fullname', dest='full', action='store')
args = parser.parse_args()
For eg: --firstname --lastname --fullname
The user can run the code in 2 days.
Way 1:
code.py --firstname myfirst --lastname mylast
Way 2:
code.py --fullname myfullname
Prevent
We should not use the combination fistname, fullname or lastname, fullname. If we use both, then it should not execute.
Can someone help me to fix this?
Not sure that is an argparse specific behavior that is possible. But as long as those items are going to their own variables in the argparse resposes its a simple set of programming to check which ones are set and issue a message and exit.
example (assuming the result of parsing is in argsvalue):
if argsvalue.fullname and (argsvalue.firstname or argsvalue.lastname):
print ("missuse of name options.....")
This assumes the argparse default for the vars is None (then if anything is set in them they will test to true with the logic above...
Like this answer proposes (on a similar question) you can do something like the following by using subparsers for both cases:
# create the top-level parser
parser = argparse.ArgumentParser(add_help=false)
subparsers = parser.add_subparsers(help='help for subcommands')
# create the parser for the "full_name" command
full_name = subparsers.add_parser('full_name', help='full_name help')
full_name.add_argument('--fullname', dest='full', action='store')
# create the parser for the "separate_names" command
separate_names = subparsers.add_parser('separate_names', help='separate_names help')
separate_names.add_argument('--firstname', dest='fn', action='store')
separate_names.add_argument('--lastname', dest='ln', action='store')
args = parser.parse_args()
You can improve it even further by requiring both the first and last name of the user as it generally makes sense.
You can split your arguments into separate commands and use subparsers, for example:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser_clean = subparsers.add_parser('clean', description='clean build folder')
parser_clean.add_argument('--all', help='clean build folder and logs', action='store_true')
parser_deploy = subparsers.add_parser('deploy')
parser_deploy.add_argument('object')
parser_deploy.add_argument('--nc', help='no cleanup', action='store_true')
args = parser.parse_args()
How can I get an argparse subparser to only parse arguments for it's own arguments? It seems that calling parse_known_args on the subparser parses all the parent's arguments.
Given this simplified version, and the inputs foo bar:
main_parser = argparse.ArgumentParser()
main_parser.add_argument("command")
args, subc_args = main_parser.parse_known_args()
print("main parser = {}".format(args))
sub_parser = main_parser.add_subparsers()
sub_command_parser = sub_parser.add_parser("sub_command")
sub_command_parser.add_argument("hostname")
sub_args = sub_command_parser.parse_known_args()
print("sub parser = {}".format(sub_args))
The first print returns what I expect:
main parser = Namespace(command='foo')
Whereas the penultimate line returns the same thing, only with the extra argument as part of the "unknown" list:
sub parser = (Namespace(hostname='foo'), ['bar'])
How can I get something like sub_command_parser.parse_known_args() to ignore the arguments that were given before it? What I need is for sub_args to only contain those arguments that were added with sub_command_parser.add_argument(). I can call them directly afterwards like this; sub_args[1], but that seems hacky and unreliable.
Background: I have a package split up into a main file as the entry point which handles the top level arguments, and the modules that do the actual work. I want to add a top level "main parser" in the main file, and sub-parsers in the modules.
Normal subcommands usage, all in one file:
main_parser = argparse.ArgumentParser()
sub_parser = main_parser.add_subparsers(dest='command')
sub_command_parser = sub_parser.add_parser("sub_command")
sub_command_parser.add_argument("hostname")
args = main_parser.parse_args()
print(args)
args.command # name of the subcommand
args.hostname # value from subcommand argument
This automatically passes the remaining strings to the subparser, the named in command. This usage should be clear in the Python documentation.
If you are going to split the parsers between files, and call them separately, skip add_subparsers bit. It doesn't do anything for you.
# Main file
#
parser = argparse.ArgumentParser()
parser.add_argument("command")
args, subc_args = parser.parse_known_args()
print("main parser = {}".format(args.command))
# Subcommand file
#
parser = argparse.ArgumentParser()
parser.add_argument("hostname")
sub_args, unknown_args = parser.parse_known_args(subc_args)
print("sub parser = {}".format(sub_args.hostname))
That helped, thanks #hpaulj. Looks like there isn't a good way to handle subcommands with argparse. The full working example for people stumbling on this is:
# Main file
#
main_parser = argparse.ArgumentParser()
main_parser.add_argument("command")
args, subc_args = main_parser.parse_known_args()
print("main parser = {}".format(args.command))
# Subcommand file
#
sub_parser = main_parser.add_subparsers()
sub_command_parser = sub_parser.add_parser("sub_command")
sub_command_parser.add_argument("hostname")
sub_args, unknown_args = sub_command_parser.parse_known_args(subc_args)
print("sub parser = {}".format(sub_args.hostname))
Note that when calling the class (or however you've implemented the subparser creation in the subcommand file), you'll need to supply the subc_args too, as optional.
EDIT: Specifying add_help=False in the main parser gets around this :)
I want to accomplish something like this:
-LoadFiles
-SourceFile "" -DestPath ""
-SourceFolder "" -DestPath ""
-GenericOperation
-SpecificOperation -Arga "" -Argb ""
-OtherOperation -Argc "" -Argb "" -Argc ""
A user should be able to run things like:
-LoadFiles -SourceFile "somePath" -DestPath "somePath"
or
-LoadFiles -SourceFolder "somePath" -DestPath "somePath"
Basically, if you have -LoadFiles, you are required to have either -SourceFile or -SourceFolder after. If you have -SourceFile, you are required to have -DestPath, etc.
Is this chain of required arguments for other arguments possible? If not, can I at least do something like, if you have -SourceFile, you MUST have -DestPath?
You can use subparsers in argparse
import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', required=True, help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "bar" command
parser_a = subparsers.add_parser('bar', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')
print(parser.parse_args())
After you call parse_args on the ArgumentParser instance you've created, it'll give you a Namespace object. Simply check that if one of the arguments is present then the other one has to be there too. Like:
args = parser.parse_args()
if ('LoadFiles' in vars(args) and
'SourceFolder' not in vars(args) and
'SourceFile' not in vars(args)):
parser.error('The -LoadFiles argument requires the -SourceFolder or -SourceFile')
There are some argparse alternatives which you can easily manage cases like what you mentioned.
packages like:
click or
docopt.
If we want to get around the manual implementation of chain arguments in argparse, check out the Commands and Groups in click for instance.
Here is a sample that in case you specify --makeDependency, forces you to specify --dependency with a value as well.
This is not done by argparse alone, but also by by the program that later on validates what the user specified.
#!/usr/bin/env python
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--makeDependency', help='create dependency on --dependency', action='store_true')
parser.add_argument('--dependency', help='dependency example')
args = parser.parse_args()
if args.makeDependency and not args.dependency:
print "error on dependency"
sys.exit(1)
print "ok!"
With python's argparse, how do I make a subcommand a required argument? I want to do this because I want argparse to error out if a subcommand is not specified. I override the error method to print help instead. I have 3-deep nested subcommands, so it's not a matter of simply handling zero arguments at the top level.
In the following example, if this is called like so, I get:
$./simple.py
$
What I want it to do instead is for argparse to complain that the required subcommand was not specified:
import argparse
class MyArgumentParser(argparse.ArgumentParser):
def error(self, message):
self.print_help(sys.stderr)
self.exit(0, '%s: error: %s\n' % (self.prog, message))
def main():
parser = MyArgumentParser(description='Simple example')
subs = parser.add_subparsers()
sub_one = subs.add_parser('one', help='does something')
sub_two = subs.add_parser('two', help='does something else')
parser.parse_args()
if __name__ == '__main__':
main()
There was a change in 3.3 in the error message for required arguments, and subcommands got lost in the dust.
http://bugs.python.org/issue9253#msg186387
There I suggest this work around, setting the required attribute after the subparsers is defined.
parser = ArgumentParser(prog='test')
subparsers = parser.add_subparsers()
subparsers.required = True
subparsers.dest = 'command'
subparser = subparsers.add_parser("foo", help="run foo")
parser.parse_args()
update
A related pull-request: https://github.com/python/cpython/pull/3027
In addition to hpaulj's answer: you can also use the required keyword argument with ArgumentParser.add_subparsers() since Python 3.7. You also need to pass dest as argument. Otherwise you will get an error: TypeError: sequence item 0: expected str instance, NoneType found.
Example file example.py:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command', required=True)
foo_parser = subparsers.add_parser("foo", help="command foo")
args = parser.parse_args()
Output of the call without an argument:
$ python example.py
usage: example.py [-h] {foo} ...
example.py: error: the following arguments are required: command
How about using required=True? More info here.
You can use the dest argument, which is documented in the last example in the documentation for add_subparsers():
# required_subparser.py
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser_name')
one = subparsers.add_parser('one')
two = subparsers.add_parser('two')
args = parser.parse_args()
Running with Python 2.7:
$python required_subparser.py
usage: required_subparser.py [-h] {one,two} ...
required_subparser.py: error: too few arguments
$python required_subparser.py one
$# no error
I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?
As indicated in the argparse docs:
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name
So you should be using args.pm_export.
Unfortunately, dash-to-underscore replacement doesn't work for positional arguments (not prefixed by --).
E.g:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs-dir',
help='Directory with .log and .log.gz files')
parser.add_argument('results-csv', type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs-dir='./', results-csv=<open file 'lool.csv', mode 'w' at 0x9020650>)
So, you should use 1'st argument to add_argument() as attribute name and metavar kwarg to set how it should look in help:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs_dir', metavar='logs-dir',
nargs=1,
help='Directory with .log and .log.gz files')
parser.add_argument('results_csv', metavar='results-csv',
nargs=1,
type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs_dir=['./'], results_csv=[<open file 'lool.csv', mode 'w' at 0xb71385f8>])
Dashes are converted to underscores:
import argparse
pa = argparse.ArgumentParser()
pa.add_argument('--foo-bar')
args = pa.parse_args(['--foo-bar', '24'])
print args # Namespace(foo_bar='24')
Concise and explicit but probably not always acceptable way would be to use vars():
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = vars(parser.parse_args())
print(args['a-b'])
getattr(args, 'positional-arg')
This is another OK workaround for positional arguments:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = parser.parse_args(['123'])
assert getattr(args, 'a-b') == '123'
Tested on Python 3.8.2.
I guess the last option is to change shorten option -a to --a
import argparse
parser = argparse.ArgumentParser(description="Help")
parser.add_argument("--a", "--argument-option", metavar="", help="") # change here
args = parser.parse_args()
option = args.a # And here
print(option)