[docs] [issue16399] argparse: append action with default list adds to list instead of overriding

paul j3 report at bugs.python.org
Thu Jun 19 18:22:50 CEST 2014


paul j3 added the comment:

It should be easy to write a subclass of Action, or append Action, that does what you want.  It just needs a different `__call__` method.  You just need a way of identifying an default that needs to be overwritten as opposed to appended to.


    def __call__(self, parser, namespace, values, option_string=None):
        current_value = getattr(namspace, self.dest)
        if 'current_value is default':
            setattr(namespace, self.dest, values)
            return
        else:
            # the normal append action
            items = _copy.copy(_ensure_value(namespace, self.dest, []))
            items.append(values)
            setattr(namespace, self.dest, items)

People on StackOverFlow might have other ideas.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16399>
_______________________________________


More information about the docs mailing list