New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remove_argument() method to argparse.ArgumentParser #63661
Comments
In order to migrate from optparse to argparse we need to have an ability to substitute anguments, e.g. remove and then create. In our framework we use the command line utility base class and then inherit the particular tools from it. The parser in base class uses add_argument() to populate the general argument list but for some tools it is needed to modify the inherited arguments set and make some arguments to have the modified meaning. With optparse we've just used remove_option() and then added the modified one with add_option() but argparse currently does not have this functionality. For the purpose above we just need to have remove_argument() or modify_argument() methods in orgparse |
Does conflict_handler='resolve' address your use case? It sounds like it should. |
We need argparse to raise an error for conflicting options and that's why
|
Explicitly substitute, excuse me
|
When you add an argument, argparse creates an For example: parser = ArgumentParser()
a = parser.add_argument('--foo')
print a produces: _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
The constant In http://bugs.python.org/issue14191 I explored a couple of ways of temporarily 'deactivating' certain groups of arguments, so as to parse the optionals and positionals separately. It's an advance issue, but might still give some ideas. Another possibility is to use 'parent' parsers to define clusters of arguments. Your base class could create a parser with one set of parents, and the subclass could use a different set. |
Paul, |
Just hang on the Action object that the a = parser.add_argument('--foo', help='initial help')
....
a.help = 'new help' If using a custom parser class and subclass, I'd do something like: self.changeablearg = self.parser.add_argument... in the class, and self.changeablearg.help = 'new help' in the subclass You can test the help message with
|
What is the way to 'hide' the argument from being parsed? E.g. we have self.parser.add_argument('foo') in parent class, |
|
It does the trick with optionals but not the positionals. |
f.nargs = '?' may be best set of tweaks to a positional Action But if there is a string in the input that could match this positional, it will be use. f.nargs = 0 is another option. This puts a I should stress, though, that fiddling with |
I realized while answering a Stackoverflow question that the resolve method has the pieces for removing an Action from the parser. It removes an existing Action so that the new, conflicting one, can replace it. It's meant to be used with flagged arguments. If
If it is a positional, this call seems to be sufficient: arg1.container._remove_action(arg1) http://stackoverflow.com/a/32809642/901925 Beyond answering this question I haven't tested the idea. If there's more interest I could explore it more. |
@paul j3 i.e.: |
It seems to me that the original use case is obsolete. Paul, do you think this issue should be closed? |
I think it can be closed. |
I just needed this function and used this implementation: My use case is I have ~ten subcommands, nine of which need the same argument, one that doesn't. Easier to add it once to a common sub-parser, then remove it from the exception. Rather than repeat the add() nine times. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: