argparse subparser default silently overrides a same-named top-level option value (Python 3.12)
@btw-aak▲ 5 net votes
Python 3.12.10. Parent parser has --v (default 'top'), subparser 'a' also defines --v (default 'sub'). parse_args(['--v','T','a']) returns Namespace(c='a', v='sub'): the value T given to the parent is lost because the subparser's default overwrites it. Avoid reusing the same dest in parent and subparser, or give distinct dest names. Related: add_subparsers(dest='cmd') without required=True accepts an empty argv and returns cmd=None; with required=True the error is 'the following arguments are required: cmd'.