Skip to content
Advertisement

Disable/Remove argument in argparse

Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How?

It is easy to add new arguments:

JavaScript

And I know you can override arguments with a new definition by specifying the “resolve” conflict handler:

JavaScript

But this still includes arg1 in the help message and results of parse_args Is there anything like

JavaScript

Or another reasonably easy way to achieve this?

Also: Would the approach be different if the argument was a positional argument?

Note: the problem with removing arg1 after parsing as suggested here is that the argument still shows in the help

Advertisement

Answer

Despite the bug issue I mention below, your use of resolve suggests a possible method. This is not for the novice or someone who needs to stick with the public API.

The parser has a list of the Action (argument) objects (as created by add_argument).

Using your 2nd parser definition, its _actions list is:

JavaScript

When you add a conflicting one with resolve, it removes the existing Action that conflicts. Look at the _handle_conflict_resolve method for details. But I can fool it into removing an action without adding a new one.

JavaScript

Look at _actions and help to verify that --arg1 is gone.

JavaScript

resolve just handles optionals, ones where the flag strings might conflict. And it removes conflicting flags first, removing the conflicting action only if no flags remain. So be extra careful when you have both short and long options.

And this does not address the case of positionals. They don’t have flags, and they may share dest parameters. (though only one will appear in the result, unless they are appending actions).

JavaScript

Playing around a bit more, it looks like I can remove one of these new positionals:

JavaScript

If I’d choosen _actions[-2] I would have removed the first foo. If I assign the value that add_argument returns to a variable, e.g. foo1, I can use that instead of looking up the value in the parser._actions list. It may be helpful to run a sample parser in an interative shell (I use IPython) and look at these objects.

Again, this seems to work on a simple example, but it needs careful testing if used with something more complex (or for production).


The topic was raised on the Python bugs/issues a couple of years ago:

http://bugs.python.org/issue19462 Add remove_argument() method to argparse.ArgumentParser

I discussed the difficulties in complete removal, and suggested some alternatives. argparse.SUPPRESS can be used to hide helps. optionals can be ignored if they aren’t required. positionals are trickier, though I suggested tweaking their attributes (nargs and default). But it’s been a while, so I need to review those posts.

=============================

I was curious about @2rs2ts problem (see comment).

I made a parser, and then used it as parent to another parser (no need to use the subparser mechanism). Then I removed an argument from one parser, and looked at changes in the other parser.

Make a parent parser with one argument:

JavaScript

Make another with parents:

JavaScript

Note that the 2nd Action is the same for both parsers (same id). parents just copied a reference to the original --foo Action, it did not make a copy.

JavaScript

Now remove ‘–foo’ from one parser, using the trick I worked out before:

JavaScript

‘–foo’ is gone from p1 list, but still present in the p list. But option_strings is now empty.

JavaScript

The resolve code removed conflicting option_strings from the --foo action, and then removed it from the p1._actions list. But changing option_strings for the p1 reference changed the p reference as well.

argparse uses a couple of ways to distinguish positionals from optionals, but the one used most often in parsing is to look at whether the option_strings attribute is empty or not. By emptying this attribute, resolve has effectively turned an optional into a positional.

Oops, my memory isn’t what it should be.:) A year ago I answered a similar question involving parents and resolve

https://stackoverflow.com/a/25821043/901925 argparse conflict resolver for options in subcommands turns keyword argument into positional argument

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement