paul j3 added the comment:

In the attached file I tried another approach - a custom Action class.  It 
gives you the custom behavior now, instead of 2-3 releases in the future.

    class Action2(Action):
        # custom action
        def __init__(self, *args, **kwargs):
            super(Action2, self).__init__(*args, **kwargs)
            self.default = SUPPRESS 
        def __call__(self, parser, namespace, values, option_string=None):
           print('Action2', argparse._get_action_name(self), values)

'self.default=SUPPRESS' keeps the dest out of the namespace without affecting 
help display.  And as with '_HelpAction', the __call__ can do its own thing 
without modifying the namespace.

One thing that this custom Action class does not do well is let you modify the 
handling of the argument after it is created.  For example if an argument is 
created by an imported parent parser, attributes like 'dest' and 'default' can 
be changed after the fact, but the argument class can't.

In   http://bugs.python.org/issue14191  I wrestled with the issue of 
temporarily disabling subsets of the arguments, first the positionals, and then 
the optionals, so I could run 'parse_known_args' separately on the two groups.

For optionals it was enough to ensure that 'required=False'.  For positionals I 
first used 'nargs=0', and latter enabled a 'nargs=SUPPRESS' option to suppress 
them.

----------
Added file: http://bugs.python.org/file35389/issue20430.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue20430>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to