Message398416
I've explored something similar in
https://bugs.python.org/issue11588
Add "necessarily inclusive" groups to argparse
There is a local variable in parser._parse_known_args
seen_non_default_actions
that's a set of the actions that have been seen. It is used for testing for required actions, and for mutually_exclusive groups. But making it available to users without altering user API code is awkward.
My latest idea was to add it as an attribute to the parser, or (conditionally) as attribute of the namespace
https://bugs.python.org/issue11588#msg265734
I've also thought about tweaking the interface between
parser._parse_known_args
parser.parse_known_args
to do of more of the error checking in the caller, and give the user more opportunity to do their checks. This variable would be part of _parse_known_args output.
Usually though when testing like this comes up on SO, I suggest leaving the defaults as None, and then just using a
if args.foobar is None:
# not seen
Defaults are written to the namespace at the start of parsing, and seen arguments overwrite those values (with an added type 'eval' step of remaining defaults at the end).
Keep in mind, though, that the use of subparsers could complicate any of these tweaks.
In reading my posts on https://bugs.python.org/issue26394, I remembered the IPython uses argparse (subclassed) with config. I believe it uses config inputs (default and user) to define the arguments for the parser.
So unless someone comes up with a really clever idea, this is bigger request than it first impressions suggest. |
|
Date |
User |
Action |
Args |
2021-07-28 19:47:35 | paul.j3 | set | recipients:
+ paul.j3, rhettinger, Thermi, joker |
2021-07-28 19:47:35 | paul.j3 | set | messageid: <1627501655.76.0.00294733419455.issue44748@roundup.psfhosted.org> |
2021-07-28 19:47:35 | paul.j3 | link | issue44748 messages |
2021-07-28 19:47:35 | paul.j3 | create | |
|