[issue46101] argparse: using parents & subcommands, options can be ignored
Lucas Cimon
report at bugs.python.org
Thu Dec 16 10:29:06 EST 2021
New submission from Lucas Cimon <lucas.cimon at gmail.com>:
Hi!
Here is some minimal code reproducing the issue:
import argparse
common_opts_parser = argparse.ArgumentParser(add_help=False)
common_opts_parser.add_argument("--endpoint", choices=("prod", "dev"), default="dev")
parser = argparse.ArgumentParser(parents=[common_opts_parser])
subparsers = parser.add_subparsers(required=True)
subcmd_cmd = subparsers.add_parser("subcmd", parents=[common_opts_parser])
subcmd_cmd.add_argument("--debug", action="store_true")
print(parser.parse_args())
Everything works fine / as expected when specifying the common optional arg last:
$ ./bug_repro.py subcmd --endpoint=dev
Namespace(endpoint='dev', debug=False)
However when specifying the --endpoint between the program and the subcommand, the value provided is ignored:
$ ./bug_repro.py --endpoint=dev subcmd
Namespace(endpoint=None, debug=False)
I have a PR ready to fix that.
----------
components: Library (Lib)
messages: 408711
nosy: Lucas Cimon
priority: normal
severity: normal
status: open
title: argparse: using parents & subcommands, options can be ignored
type: behavior
versions: Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46101>
_______________________________________
More information about the Python-bugs-list
mailing list