Skip to content

Argparse: add method for retrieving the parsers of actions #144932

@dogversioning

Description

@dogversioning

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions