Unfortunately, by default ArgumentParser does not have an extend action. But it is not so difficult to register:
import argparse class ExtendAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): items = getattr(namespace, self.dest) or [] items.extend(values) setattr(namespace, self.dest, items) parser = argparse.ArgumentParser() parser.register('action', 'extend', ExtendAction) parser.add_argument('--env', nargs='+', action='extend') args = parser.parse_args() print(args)
Demo:
$ python /tmp/args.py --env one two --env three Namespace(env=['one', 'two', 'three'])
lambda that you have in your example is somewhat outside of the intended use case of type kwarg. Therefore, I would recommend instead dividing into spaces, because it will be painful to correctly handle the case when , in fact, is in the data. If you split up into space, you get this functionality for free:
$ python /tmp/args.py --env one "hello world" two --env three Namespace(env=['one', 'hello world', 'two', 'three'])
source share