Message224964
Well that won't work. Example:
import argparse
class TestAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print("default: {}({})\tdest: {}({})".format(self.default, type(self.default), getattr(namespace, self.dest), type(getattr(namespace, self.dest))))
if getattr(namespace, self.dest) is self.default:
print("Replacing with: ", values)
setattr(namespace, self.dest, values)
# extra logical code not necessary for testcase
parser = argparse.ArgumentParser()
parser.add_argument\
( "-o", "--output"
, type=int
, action=TestAction
, default=42
)
args = parser.parse_args()
$ ./argparse_test -o 42 -o 100
default: 42(<class 'int'>) dest: 42(<class 'int'>)
Replacing with: 42
default: 42(<class 'int'>) dest: 42(<class 'int'>)
Replacing with: 100
100
Use this approach:
class ExtendAction(argparse.Action):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not isinstance(self.default, collections.abc.Iterable):
self.default = [self.default]
self.reset_dest = False
def __call__(self, parser, namespace, values, option_string=None):
if not self.reset_dest:
setattr(namespace, self.dest, [])
self.reset_dest = True
getattr(namespace, self.dest).extend(values)
Anyway, this should be properly documented... |
|
Date |
User |
Action |
Args |
2014-08-06 19:30:21 | Yclept.Nemo | set | recipients:
+ Yclept.Nemo, bethard, r.david.murray, docs@python, paul.j3, Markus.Amalthea.Magnuson, SylvainDe |
2014-08-06 19:30:21 | Yclept.Nemo | set | messageid: <1407353421.53.0.734634864601.issue16399@psf.upfronthosting.co.za> |
2014-08-06 19:30:21 | Yclept.Nemo | link | issue16399 messages |
2014-08-06 19:30:21 | Yclept.Nemo | create | |
|