I ran into the same problem. I decided to go with the usual route of action, as suggested by mgilson.
import argparse class ExtendAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): if getattr(namespace, self.dest, None) is None: setattr(namespace, self.dest, []) getattr(namespace, self.dest).extend(values) parser = argparse.ArgumentParser() parser.add_argument("-p", nargs="+", help="Stuff", action=ExtendAction) args = parser.parse_args() print args
The result is
$ ./sample.py -px -py -pzw Namespace(p=['x', 'y', 'z', 'w'])
However, this would be much more neat if the default option was action='extend' in the library.
source share