问题
I've got some Python argparse
command-line processing code that initially looked like this:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("--x", help = "Set `x`.", action = "store_true", default = False)
ap.add_argument("--y", help = "Set `y`.", action = "store_true", default = False)
ap.add_argument(
"--all", help = "Equivalent to `--x --y`.",
action = "store_true", default = False
)
args = ap.parse_args()
if args.all:
args.x = True
args.y = True
print "args.x", args.x
print "args.y", args.y
The basic idea: I have some boolean flags that toggle on a particular setting (--x
, --y
, etc), and I want to add a convenience option that toggles multiple settings on - e.g. --all
is equivalent to --x --y
.
I wanted to avoid having any command-line processing logic that was not contained within the ArgumentParser
and done in parse_args
, so I came up with this solution using custom argparse.Action
s:
import argparse
def store_const_multiple(const, *destinations):
"""Returns an `argparse.Action` class that sets multiple argument
destinations (`destinations`) to `const`."""
class store_const_multiple_action(argparse.Action):
def __init__(self, *args, **kwargs):
super(store_const_multiple_action, self).__init__(
metavar = None, nargs = 0, const = const, *args, **kwargs
)
def __call__(self, parser, namespace, values, option_string = None):
for destination in destinations:
setattr(namespace, destination, const)
return store_const_multiple_action
def store_true_multiple(*destinations):
"""Returns an `argparse.Action` class that sets multiple argument
destinations (`destinations`) to `True`."""
return store_const_multiple(True, *destinations)
ap = argparse.ArgumentParser()
ap.add_argument("--x", help = "Set `x`.", action = "store_true", default = False)
ap.add_argument("--y", help = "Set `y`.", action = "store_true", default = False)
ap.add_argument(
"--all", help = "Equivalent to `--x --y`.",
action = store_true_multiple("x", "y")
)
args = ap.parse_args()
print "args.x", args.x
print "args.y", args.y
Is there any clean way of achieving what I want with argparse
without either (0) doing some processing after parse_args()
(first example) or (2) writing a custom argparse.Action
(second example)?
回答1:
This is late, and not exactly what you wanted, but here is something you could try:
import argparse
myflags = ['x', 'y', 'z']
parser = argparse.ArgumentParser()
parser.add_argument('--flags', nargs="+", choices=myflags)
parser.add_argument('--all-flags', action='store_const', const=myflags, dest='flags')
args = parser.parse_args()
print(args)
Then calling python myscript.py --flags x y
outputs this
Namespace(flags=['x', 'y'])
And calling python myscript.py --all-flags
outputs this
Namespace(flags=['x', 'y', 'z'])
But you'll have to check your flags via 'x' in args.flags
instead of simply args.x
来源:https://stackoverflow.com/questions/48834678/python-argparse-is-there-a-clean-way-to-add-a-flag-that-sets-multiple-flags