[issue44748] argparse: a bool indicating if arg was encountered
paul j3
report at bugs.python.org
Wed Jul 28 15:47:35 EDT 2021
paul j3 <ajipanca at gmail.com> added the comment:
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.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44748>
_______________________________________
More information about the Python-bugs-list
mailing list