Message 230056 - Python tracker (original) (raw)
I'm exploring modifying that patch by adding a 'subnamespace' attribute to the subparsers Action object. This would give the user, or the main parser control over the namespace that is passed to a subparser.
For example:
subnamespace = getattr(self, 'subnamespace', False)
if subnamespace is None:
pass
elif not subnamespace:
subnamespace = namespace
# if None, subparser will use a fresh one
subnamespace, arg_strings = parser.parse_known_args(arg_strings, subnamespace)
for key, value in vars(subnamespace).items():
setattr(namespace, key, value)
As written here, no value (or any False) would use the main parser namespace, as done with existing code.
A 'None' value, would use the a new empty namespace, as proposed in this patch.
Or the user could directly define the namespace, e.g.
sp = parser.add_subparsers()
sp.subnamespace = Namespace(foo=4)
That could also be convenient if the user is using a custom Namespace class.
'parse_known_args' could also set this attribute, based on whether the user has given it a namespace or not.