Argparse: add method for retrieving the parsers of actions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
I have a CLI interface with an argparse.ArgumentParser composed of a bunch of different actions. I was trying to validate if an argument had been set to a default value, and with just the main parser available, I was having some issues using get_default() working from the main parser, rather than the parsers for the various actions.
I ended up going with the following comparison, in order to reduce the amount of refactoring I would need to do, where args is the var holding the object returned from parse_args():
args.my_param == parser._actions[1].choices[args.action]].get_default(args.my_param)
I could also have solved this by adding a wrapper class holding all the subparsers in an object of some kind.
But - I think it would be nice to have a public method for accessing these action parsers, mostly for enabling this kind of use case.
Has this already been discussed elsewhere?
Not that I could find
Links to previous discussion of this feature:
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with argparse.ArgumentParser, the parser's _actions collection, subparser choices, and get_default(). Determine the public method's intended scope and behavior for retrieving action parsers, including how it should support the described default-value validation use case. Done means the API and its behavior are clearly defined and the feature is covered by appropriate tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100