Python 3: Parse nested secondary arguments for initial set of choices - python

Subcommands like git commit and git status can be easily parsed with argparse using add_subparsers. How does one get nested settings input for choices selected from command line?
Let's say I want to play music with all custom settings:
play.py --path /path/to.file --duration 100 --filter equalizer --effect echo equalizer_settings 1 2 echo_settings 5
Here --filter equalizer and --effect echo are first level choices but I need to get settings for those as secondary arguments. Ex: echo_settings 5 and equalizer_settings 1 2.
Secondary settings could be more than one and preferably with named arguments.
Listed below is what I have so far...
import argparse
parser = argparse.ArgumentParser(description='Play music the way I want it.')
parser.add_argument('-p', '--path', type=str, required=True, help='File path')
parser.add_argument('-d', '--duration', type=int, default=50, help='Play duration')
parser.add_argument('-f', '--filter', choices=['none', 'equalizer'], default='none', help='Filter selection')
parser.add_argument('-e', '--effect', choices=['none', 'echo', 'surround'], default='none', help='Effect selection')
subparsers = parser.add_subparsers(help='Settings of optional parameters')
equalizer_parser = subparsers.add_parser("equalizer_settings")
equalizer_parser.add_argument('equalizer_min_range', type=int)
equalizer_parser.add_argument('equalizer_max_range', type=int)
echo_parser = subparsers.add_parser("echo_settings")
echo_parser.add_argument('echo_strength', type=int)
surround_parser = subparsers.add_parser("surround_settings")
surround_parser.add_argument('surround_strength', type=int)
args = parser.parse_args()
print(args)
Currently this errors in error: unrecognized arguments

You can only use one subparser at a time, i.e. one of those 3 settings. There are some advanced ways of using several subparsers in sequence, but I don't think we want to go there (see previous SO argparse questions).
But do you really need to use subparsers? Why not just another set of optionals (flagged) arguments, something like:
parser.add_argument("--equalizer_settings", nargs=2, type=int)
parser.add_argument("--echo_settings", type=int)
parser.add_argument("--surround_strength", type=int
You are just adding some parameters, not invoking some sort of action command.

Related

Extending Help in Argparse to Subcommands [duplicate]

My argparse has only 3 flags (store_true) on the top level, everything else is handled through subparsers. When I run myprog.py --help, the output shows a list of all subcommands like normal, {sub1, sub2, sub3, sub4, ...}. So, the default is working great...
I usually can't remember the exact subcommand name I need, and all of its options. So I end up doing 2 help lookups:
myprog.py --help
myprog.py sub1 --help
I do this so often, I decided to cram this into one step. I would rather have my toplevel help output a huge summary, and then I scroll through the list manually. I find it is much faster (for me at least).
I was using a RawDescriptionHelpFormatter, and typing the long help output by hand. But now I have lots of subcommands, and its becoming a pain to manage.
Is there a way to get a verbose help output with just one program call?
If not, how can I iterate the subparsers of my argparse instance, and then retrieve the help output individually from each one (which I will then later glue together)?
Here is a quick outline of my argparse setup. I cleaned/stripped the code a fair bit, so this may not run without a bit of help.
parser = argparse.ArgumentParser(
prog='myprog.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(""" You can manually type Help here """) )
parser.add_argument('--debuglog', action='store_true', help='Verbose logging for debug purposes.')
parser.add_argument('--ipyonexit', action='store_true', help='Drop into an embeded Ipython session instead of exiting command.')
subparser = parser.add_subparsers()
### --- Subparser B
parser_b = subparser.add_parser('pdfreport', description="Used to output reports in PDF format.")
parser_b.add_argument('type', type=str, choices=['flatlist', 'nested', 'custom'],
help="The type of PDF report to generate.")
parser_b.add_argument('--of', type=str, default='',
help="Override the path/name of the output file.")
parser_b.add_argument('--pagesize', type=str, choices=['letter', '3x5', '5x7'], default='letter',
help="Override page size in output PDF.")
parser_b.set_defaults(func=cmd_pdf_report)
### ---- Subparser C
parser_c = subparser.add_parser('dbtables', description="Used to perform direct DB import/export using XLS files.")
parser_c.add_argument('action', type=str, choices=['push', 'pull', 'append', 'update'],
help="The action to perform on the Database Tables.")
parser_c.add_argument('tablename', nargs="+",
help="The name(s) of the DB-Table to operate on.")
parser_c.set_defaults(func=cmd_db_tables)
args = parser.parse_args()
args.func(args)
This is a bit tricky, as argparse does not expose a list of defined sub-parsers directly. But it can be done:
import argparse
# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')
# print main help
print(parser.format_help())
# retrieve subparsers from parser
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)]
# there will probably only be one subparser_action,
# but better safe than sorry
for subparsers_action in subparsers_actions:
# get all subparsers and print help
for choice, subparser in subparsers_action.choices.items():
print("Subparser '{}'".format(choice))
print(subparser.format_help())
This example should work for python 2.7 and python 3. The example parser is from Python 2.7 documentation on argparse sub-commands.
The only thing left to do is adding a new argument for the complete help, or replacing the built in -h/--help.
Here is complete soulution with custom help handler (almost all code from #Adaephon answer):
import argparse
class _HelpAction(argparse._HelpAction):
def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
# retrieve subparsers from parser
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)]
# there will probably only be one subparser_action,
# but better save than sorry
for subparsers_action in subparsers_actions:
# get all subparsers and print help
for choice, subparser in subparsers_action.choices.items():
print("Subparser '{}'".format(choice))
print(subparser.format_help())
parser.exit()
# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG', add_help=False) # here we turn off default help action
parser.add_argument('--help', action=_HelpAction, help='help for help if you need some help') # add custom help
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')
parsed_args = parser.parse_args()
Perhaps an easier approach is to use parser.epilog:
def define_parser():
import argparse
parser = argparse.ArgumentParser(
prog='main',
formatter_class=argparse.RawDescriptionHelpFormatter,
)
commands = parser.add_subparsers(
title="required commands",
help='Select one of:',
)
command_list = commands.add_parser(
'list',
help='List included services',
)
command_ensure = commands.add_parser(
'ensure',
help='Provision included service',
)
command_ensure.add_argument(
"service",
help='Service name',
)
import textwrap
parser.epilog = textwrap.dedent(
f"""\
commands usage:\n
{command_list.format_usage()}
{command_ensure.format_usage()}
"""
)
return parser
parser = define_parser()
parser.print_help()
which results in the following output:
usage: main [-h] {list,ensure} ...
optional arguments:
-h, --help show this help message and exit
required commands:
{list,ensure} Select one of:
list List included services
ensure Provision included service
commands usage:
usage: main list [-h]
usage: main ensure [-h] service
A simpler way to iterate over the subparsers in Adaephon's example is
for subparser in [parser_a, parser_b]:
subparser.format_help()
Python does allow you to access hidden attributes like parser._actions, but that's not encouraged. It is just as easy to build your own list while defining the parser. Same goes for doing special things with the arguments. add_argument and add_subparser return their respective Action and Parser objects for a reason.
If I were making a subclass of ArgumentParser I would feel free to use _actions. But for a one off application, building my own list would be clearer.
An example:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('mainpos')
parser.add_argument('--mainopt')
sp = parser.add_subparsers()
splist = [] # list to collect subparsers
sp1 = sp.add_parser('cmd1')
splist.append(sp1)
sp1.add_argument('--sp1opt')
sp2 = sp.add_parser('cmd2')
splist.append(sp2)
sp2.add_argument('--sp2opt')
# collect and display for helps
helps = []
helps.append(parser.format_help())
for p in splist:
helps.append(p.format_help())
print('\n'.join(helps))
# or to show just the usage
helps = []
helps.append(parser.format_usage())
for p in splist:
helps.append(p.format_usage())
print(''.join(helps))
The combined 'usage' display is:
usage: stack32607706.py [-h] [--mainopt MAINOPT] mainpos {cmd1,cmd2} ...
usage: stack32607706.py mainpos cmd1 [-h] [--sp1opt SP1OPT]
usage: stack32607706.py mainpos cmd2 [-h] [--sp2opt SP2OPT]
The display of the combined helps is long and redundant. It could be edited in various ways, either after formatting, or with special help formatters. But who is going make such choices?
I was also able to print a short help for commands using _choices_actions.
def print_help(parser):
print(parser.description)
print('\ncommands:\n')
# retrieve subparsers from parser
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)]
# there will probably only be one subparser_action,
# but better save than sorry
for subparsers_action in subparsers_actions:
# get all subparsers and print help
for choice in subparsers_action._choices_actions:
print(' {:<19} {}'.format(choice.dest, choice.help))
add_subparsers().add_parser() accepts not only a description, which shows up in the help of the subcommand, but also a help= which is used as one-line description in the top-level parsers' help.
The docs have this hidden in the formulation
(A help message for each subparser command, however, can be given by supplying the help= argument to add_parser() as above.)
and even in the sample code around that sentence:
>>> # create the parser for the "b" command
>>> parser_b = subparsers.add_parser('b', help='b help')
>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')
[...]
usage: PROG [-h] [--foo] {a,b} ...
positional arguments:
{a,b} sub-command help
a a help
b b help
Yes, this is not the full help for everthing, but IMHO covers the basic use case very well and is not easily discoverable.
if __name__ == '__main__':
parser = argparse.ArgumentParser("TOML FILE OVERWRITE SCRIPT")
subparsers = parser.add_subparsers()
parser_set = subparsers.add_parser('set', help='Set Toml')
parser_set.add_argument('set', help='TOMl file edit set action', action='store_true')
parser_set.add_argument('-n', '--name', type=str, help='Service Name', required=True)
parser_set.add_argument('-s', '--section', type=str, help='Toml Section Name', required=True)
parser_set.add_argument('-k', '--key', type=str, help='Toml Key of Section', required=True)
parser_set.add_argument('-v', '--value', help='New Value', required=True)
args = parser.parse_args()
if args.set:
setter = ConfigurationSetter(args.name, args.section, args.key, args.value)
setter.execute()
else:
print("Ops! Something is wrong, type --help or -h")
You can check my code maybe inspires you!

How to have multiple args for a single option in argparse

I would like to allow a user to enter in multiple ids as an argument. For example, something like:
import argparse
parser = argparse.ArgumentParser(description='Dedupe assets based on group_id.')
parser.add_argument('--ids', nargs='?', default=None, type=int, help='Enter your ids')
parser.parse_args()
Yet when I enter in something like:
$ python test.py --ids 1 2 3 4
I get the following error:
test.py: error: unrecognized arguments: 2 3 4
What would be the proper way to allow/enter in multiple arguments for a single option?
you can use '+' rather than '?'.
import argparse
parser = argparse.ArgumentParser(description='Dedupe assets based on group_id.')
parser.add_argument('--ids', nargs='+', default=None, type=int, help='Enter your ids')
args = parser.parse_args()
print(args.ids)

python argparse - How to prevent by using a combination of arguments

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()

Python argparse how to add related group of options that are optional

I'm trying to add a group of options related to each other to existing script that has optional arguments and positional arguments to chose command to run.
import argparse
parser = argparse.ArgumentParser(prog='test')
parser.add_argument("-opt1", default="A", type=str)
parser.add_argument("-opt2", default="B", type=str)
parser.add_argument("-opt3", default="C", type=str)
subparsers = parser.add_subparsers()
cmd1 = subparsers.add_parser("cmd1", help="run cmd1")
cmd1.add_argument("-cmd1-opt1", default="cmd1-A")
cmd1.add_argument("-cmd1-opt2", default="cmd1-B")
cmd2 = subparsers.add_parser("cmd2", help="run cmd2")
cmd2.add_argument("-cmd2-opt", type=int)
args = parser.parse_args()
print(args)
For example I would like to add group of new optional options opt4, opt5, opt6, opt7 which are related.
Something like opt5 is valid option only when opt4 has some value.
I could add all the options individually as optional arguments but I'm looking for any better way to do that being the options I want to add are related to each other.

Python Argparse "radio" flags with default?

For example:
example.py
parser = argparse.ArgumentParser(description="Will take arguments... or none")
parser.add_argument("-a", action="store_true")
parser.add_argument("-b", action="store_true")
parser.add_argument("-c", action="store_true")
parser.add_argument("-d", action="store_true")
args = parser.parse_args()
print args
I want example.py to set a to True, but only if either:
The -a flag is used
No flags are used
I tried messing around with
parser.set_defaults(a=True, b=False)
and
parser.add_argument("-a", action="store_true", default=True)
but they will set a to True even if I decide to use the b flag.
yes using the default values will set a to True even other arguments are specified. This will violate your second requirement, following is a simple fix with a naive condition checking.
parser = argparse.ArgumentParser(description="Will take arguments... or none")
parser.add_argument("-a", action="store_true")
parser.add_argument("-b", action="store_true")
parser.add_argument("-c", action="store_true")
parser.add_argument("-d", action="store_true")
args = parser.parse_args()
if not (args.b or args.c or args.d):
args.a=True
print args
Sounds like you want a 'radio button' effect - choosing just one of several alternatives. An alternative to a set of flags would be an argument with choices.
parser.add_argument('flag', choices=['a','b','c','d'], default='a', nargs='?')
You can check the result in args.flag, which will be one of 4 strings.
Obviously the positional argument couple replaced by a flag, e.g. -f.
I went for the following solution:
parser = argparse.ArgumentParser(description="Will take arguments... or none")
lettergroup = parser.add_mutually_exclusive_group()
lettergroup.add_argument("-a", action="store_const", dest="letter", const="a", default="a")
lettergroup.add_argument("-b", action="store_const", dest="letter", const="b")
lettergroup.add_argument("-c", action="store_const", dest="letter", const="c")
lettergroup.add_argument("-d", action="store_const", dest="letter", const="d")
args = parser.parse_args()
Now, the value is stored in args.letter. If no flag is called, args.letter will have the value a. If two flags are called at the same time, the parser will throw an error.
Just another way to solve this problem.

Categories