Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda