Reputation: 96
Is there a way to produce the equivalent of const
(that we can use with nargs='?'
, see reference question here for an example), but for nargs='*'
. Meaning that I would want:
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument('--option', nargs='*', const=[1, 2, 3])
print(argparser.parse_args())
And then during usage:
my_script.py # Namespace(option=None)
my_script.py --option # Namespace(option=[1, 2, 3])
my_script.py --option 4 5 # Namespace(option=[4, 5])
Currently I get ValueError: nargs must be '?' to supply const
Upvotes: 3
Views: 443
Reputation: 2337
Simplification of @YaakovBressler's code:
class ConstWithMultiArgs (argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
obj = values if values else self.const if option_string else self.default
setattr(namespace, self.dest, obj)
Upvotes: 1
Reputation: 12018
You can use a custom action
to achieve this outcome:
import argparse
p = argparse.ArgumentParser()
class customAction(argparse.Action):
"""
Customized argparse action, will set the
value in the following way:
1) If no option_string is supplied: set to None
2) If option_string is supplied:
2A) If values are supplied:
set to list of values
2B) If no values are supplied:
set to default value (`self.const`)
NOTES:
If `const` is not set, default value (2A) will be None
"""
def __call__(self, parser, namespace, values, option_string=None):
if option_string:
setattr(namespace, self.dest, self.const)
elif not values:
setattr(namespace, self.dest, None)
else:
setattr(namespace, self.dest, values)
p.add_argument('--option',
nargs='*',
action=customAction,
dest='option',
type=int,
const=[1, 2, 3]
)
Upvotes: 1