Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python `argparse`: Is there a clean way to add a flag that sets multiple flags (e.g. `--all`" is equivalent to `--x --y`)

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.Actions:

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

like image 481
blelbach Avatar asked Feb 16 '18 20:02

blelbach


People also ask

How do you add arguments in Argparse?

To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required . The name is exactly what it sounds like — the name of the command line field.

What is Store_true in Python?

The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.

What is Subparser?

A “subparser” is an argument parser bound to a namespace. In other words, it works with everything after a certain positional argument. Argh implements commands by creating a subparser for every function.


1 Answers

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

like image 177
user986730 Avatar answered Oct 21 '22 07:10

user986730