shikhanshu shikhanshu - 1 year ago 71
Python Question

Python argparse : How can I get Namespace objects for argument groups separately?

I have some command line arguments categorized in groups as follows:

cmdParser = argparse.ArgumentParser()

groupOne = cmdParser.add_argument_group('group one')

groupTwo = cmdParser.add_argument_group('group two')

How can I parse the above, such that I end up with three different Namespace objects?

global_args - containing all the arguments not part of any group
groupOne_args - containing all the arguments in groupOne
groupTwo_args - containing all the arguments in groupTwo

Thank you!

Answer Source

Nothing in argparse is designed to do that.

For what it's worth, the parser starts off with two argument groups, one that displays as positionals and the other as optionals (I forget the exact titles). So in your example there will actually be 4 groups.

The parser only uses argument groups when formatting the help. For parsing all arguments are put in a master parser._actions list. And during parsing the parser only passes around one namespace object.

You could define separate parsers, with different sets of arguments, and call each with parse_known_args. That works better with optionals (flagged) arguments than with positionals. And it fragments your help.

I have explored in other SO questions a novel Namespace class that could nest values based on some sort of dotted dest (names like group1.optA, group2.optC, etc). I don't recall whether I had to customize the Action classes or not.

The basic point is that when saving a value to the namespace, a parser, or actually a Action (argument) object does:

setattr(namespace, dest, value)

That (and getattr/hasattr) is all that the parser expects of the namespace. The default Namespace class is simple, little more than a plain object subclass. But it could be more elaborate.