Message405129
I should study previous posts in more detail, but here are some thoughts on correctly handling user namespace.
At the start of `parse_known_args`, there's a
if namespace is None:
namespace = Namespace()
We need to hang on to a copy of this namespace, e.g. call it
import copy
orig_namespace = copy.copy(namespace)
In _SubParsersAction.__call__, pass this copy to the subparser (instead of None):
subnamespace, arg_strings = parser.parse_known_args(arg_strings, orig_namespace)
for key, value in vars(subnamespace).items():
setattr(namespace, key, value)
Prior to 9351, the main namespace was passed to the subparser
namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
The trick is to get orig_namespace from the main parse_known_args to SubParsersAction.__call__ method.
in a 9351 post I explore the idea of allowing the user to specify a 'sub_namespace' for the subparser.
https://bugs.python.org/msg230056
In any case, this is a complicated issue that needs careful thought and more extensive testing. I didn't entirely like the original 9351 change, but since that's been part of argparse for many years, we need to very careful about clobbering it. |
|
Date |
User |
Action |
Args |
2021-10-27 21:41:36 | paul.j3 | set | recipients:
+ paul.j3, rhettinger, miss-islington, ALSchwalm |
2021-10-27 21:41:36 | paul.j3 | set | messageid: <1635370896.22.0.376820847545.issue45235@roundup.psfhosted.org> |
2021-10-27 21:41:36 | paul.j3 | link | issue45235 messages |
2021-10-27 21:41:36 | paul.j3 | create | |
|