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

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.

Related

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 3: Parse nested secondary arguments for initial set of choices

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.

How to conditionally make python's argparse module require additional arguments

Basic intended usage:
my_framework create Project_title /path/to/project
OR
my_framework create Project_title (ie. use current working directory)
OR
my_framework update (ie. update my_framework rather than creating a new project)
I know I can make name optional by providing it with a default, but, in reality name is not optional provided the user has entered create as the first argument.
Best solution I've come up with is to use a default value for name and then, if the argument name equals its default value, throw an error. But if there's a way to make argparse do this work for me I'd rather learn to do it.
Writing two scripts, my_framework_create and my_framework_update doesn't appeal to me aesthetically.
#!/usr/bin/env python
import argparse
import os
import shutil
from subprocess import call
template_path = "/usr/local/klibs/template"
parser = argparse.ArgumentParser("MY_FRAMEWORK CLI", description='Creates a new MY_FRAMEWORK project or updates MY_FRAMEWORK')
parser.add_argument('action', choices=['create', 'update'], type=str, help='<help text>')
parser.add_argument('name', type=str, help='<help text>')
parser.add_argument('path', default=os.getcwd(), nargs="?", type=str, help='<help text>')
args = parser.parse_args()
if args.action == "create":
# do the create stuff
if args.action == "update":
# do the update stuff
The best way to do this is with a subparser
An example from the docs:
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title='subcommands',
... description='valid subcommands',
... help='additional help')
>>> subparsers.add_parser('foo')
>>> subparsers.add_parser('bar')
>>> parser.parse_args(['-h'])
usage: [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
valid subcommands
{foo,bar} additional help
In your case you would have create and update as separate subparsers.
Example:
def create(args):
# do the create stuff
print(args)
def update(args):
# do the update stuff
print(args)
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title='subcommands',
description='valid subcommands',
help='additional help')
create_parser = subparsers.add_parser('create')
create_parser.add_argument('name', type=str)
create_parser.set_defaults(func=create)
update_parser = subparsers.add_parser('update')
update_parser.set_defaults(func=update)

Arguments that are dependent on other arguments with Argparse

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!"

Categories