[New-bugs-announce] [issue20430] argparse.SUPPRESS

jzwinck report at bugs.python.org
Wed Jan 29 03:52:34 CET 2014

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:


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 <report at bugs.python.org>

More information about the New-bugs-announce mailing list