This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Yclept.Nemo
Recipients Markus.Amalthea.Magnuson, SylvainDe, Yclept.Nemo, bethard, docs@python, paul.j3, r.david.murray
Date 2014-08-06.19:30:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1407353421.53.0.734634864601.issue16399@psf.upfronthosting.co.za>
In-reply-to
Content
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...
History
Date User Action Args
2014-08-06 19:30:21Yclept.Nemosetrecipients: + Yclept.Nemo, bethard, r.david.murray, docs@python, paul.j3, Markus.Amalthea.Magnuson, SylvainDe
2014-08-06 19:30:21Yclept.Nemosetmessageid: <1407353421.53.0.734634864601.issue16399@psf.upfronthosting.co.za>
2014-08-06 19:30:21Yclept.Nemolinkissue16399 messages
2014-08-06 19:30:21Yclept.Nemocreate