You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse: ambiguous option reported with primary parser #109990
When we have sub_parser argument which is substring of main parser, getting error as ambiguous option
python sample.py show -foo "data"
usage: PROG [-h] [-foobar FOOBAR] [-foobar1 FOOBAR1] {show} ...
PROG: error: ambiguous option: -foo could match -foobar, -foobar1
Observing with python 3.11.3. Earlier it works fine with python 3.5
# Add a code block here, if requiredparser=argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
parser.add_argument('-foobar', action='store')
parser.add_argument('-foobar1', action='store')
sub_parser=parser.add_subparsers(dest='command')
# Showsub_parser_show=sub_parser.add_parser('show', help='Print Client ID for a specific environment',
description='Print Client ID for a specific environment')
sub_parser_show.add_argument('-foo', dest='env_name', action='store',
help='Environment name Eg: "development"')
args=parser.parse_args()
print (args)
When we have sub_parser argument which is substring of main parser, getting error as ambiguous option
python sample.py show -foo "data" usage: PROG [-h] [-foobar FOOBAR] [-foobar1 FOOBAR1] {show} ... PROG: error: ambiguous option: -foo could match -foobar, -foobar1
Observing with python 3.11.3. Earlier it works fine with python 3.5
# Add a code block here, if requiredparser=argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
parser.add_argument('-foobar', action='store')
parser.add_argument('-foobar1', action='store')
sub_parser=parser.add_subparsers(dest='command')
# Showsub_parser_show=sub_parser.add_parser('show', help='Print Client ID for a specific environment',
description='Print Client ID for a specific environment')
sub_parser_show.add_argument('-foo', dest='env_name', action='store',
help='Environment name Eg: "development"')
args=parser.parse_args()
print (args)
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
* [gh-109990: Fixed argparser parsing optional arguments with same prefix #124000](https://github.com/python/cpython/pull/124000)
This is caused by combination of two bugs: #58573 and #104860.
#124631 fixes the first issue in different way than #124000. It delays the check for ambiguous options until the option been consumed. The advantage is that it allows to use abbreviated long options (e.g. -fo for this example) in subparsers.
Activity
[-]ambiguous option reported with primary parser[/-][+]argparse: ambiguous option reported with primary parser[/+]rruuaanng commentedon Sep 16, 2024
It's also present in the main branch.
serhiy-storchaka commentedon Sep 26, 2024
This is caused by combination of two bugs: #58573 and #104860.
#124631 fixes the first issue in different way than #124000. It delays the check for ambiguous options until the option been consumed. The advantage is that it allows to use abbreviated long options (e.g.
-fofor this example) in subparsers.