[issue20430] Make argparse.SUPPRESS work as an argument "dest"
jzwinck added the comment: Yes, I have a practical need for dest=SUPPRESS. One of the reasons is basically what you said: to implement things analogous to --help. In those cases you want to be able to invoke a function (e.g. via type=myfunc) but not store anything. Or you may need to ignore an argument completely for compatibility, but you have logic which expects all the arguments returned by parse_args() to be known and meaningful. In any case, I wrote this ticket because I ran into a concrete need that argparse didn't readily support (which almost never happens!). As for what the usage statement should show: it should behave as if dest=SUPPRESS were not present. If metavar is specified, use that, otherwise use the option name as if dest were not passed in. And we already have the behavior that help=SUPPRESS will hide it from the usage entirely, so that should still work (i.e. dest=SUPPRESS, help=SUPPRESS should hide the option both from --help and the result of parse_args()). -- ___ Python tracker <http://bugs.python.org/issue20430> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue20430] argparse.SUPPRESS
New submission from jzwinck: argparse.SUPPRESS is a special object which can be used in various parts of argparse to say "do nothing." One place where it does not seem to work is in an argument's "dest". This is despite some of the plumbing using "dest=SUPPRESS" internally. It can be made to work by patching argparse._StoreAction.__call__, like this: def __call__(self, parser, namespace, values, option_string=None): -setattr(namespace, self.dest, values) +if self.dest is not argparse.SUPPRESS: +setattr(namespace, self.dest, values) Once that's done, this works as one might expect: parser.add_argument('--foo', dest=argparse.SUPPRESS) With the patch, 'foo' will not appear if you parse args containing "--foo bar". Without the patch, args looks like this, which is not really useful: Namespace(==SUPPRESS==='bar') Note that the _SubParsersAction.__call__ code has something like the above patch already. -- components: Library (Lib) messages: 209611 nosy: jzwinck priority: normal severity: normal status: open title: argparse.SUPPRESS type: enhancement versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5 ___ Python tracker <http://bugs.python.org/issue20430> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue20430] Make argparse.SUPPRESS work as an argument "dest"
Changes by jzwinck : -- title: argparse.SUPPRESS -> Make argparse.SUPPRESS work as an argument "dest" ___ Python tracker <http://bugs.python.org/issue20430> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com