from argparse import ArgumentParser
p = ArgumentParser()
subs = p.add_subparsers()
a = subs.add_parser('a')
aa = a.add_argument('aa')
b = subs.add_parser('b')
bb = b.add_argument('bb')
p.parse_args()
add_argument
returns an Action
object. These objects are also referenced in the parser._actions
list, but you can collect the references yourself, either by name or in a list (or dict). add_subparsers
is a variant on add_argument
that creates a special subclass of Action
, one which has the add_parser
method.
Try running this code in an interactive session, and look at the objects created. Most have a repr
method that shows the key (but not all) attributes. For actions this will include things like dest
, nargs
, default
, etc. You can even modify those attributes after creation. argparse
is written as a normal Python hierarchy of classes.
As for the _
and __
attributes - they aren't documented, but that doesn't mean they will be changed at a moment's notice. Changes appear in argparse
at a snails pass, with a lot thought given to backward compatibility (though sometimes not enough). It's easier to change the documentation than the code itself, especially not core things like _actions
.
But I'm not entirely sure what you are trying to do. Running unittests with argparse
is somewhat awkward. You can simulate args = parse.parse_args(argv)
where argv
is a list of strings of your choice. Or you can set sys.argv[1:]
to a new set of strings. Or you could create a new args=argparse.Namespace(...)
object. Trying to generate all possible argument combinations just based on the parser
setup sounds like a lot of work, especially when using subparsers.