Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse: ambiguous option reported with primary parser #109990

Closed
getsimonz opened this issue Sep 27, 2023 · 2 comments
Closed

argparse: ambiguous option reported with primary parser #109990

getsimonz opened this issue Sep 27, 2023 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@getsimonz
Copy link

getsimonz commented Sep 27, 2023

Bug report

Bug description:

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 required
parser = 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')
# Show
sub_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

@getsimonz getsimonz added the type-bug An unexpected behavior, bug, or error label Sep 27, 2023
@AlexWaygood AlexWaygood changed the title ambiguous option reported with primary parser argparse: ambiguous option reported with primary parser Sep 27, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Sep 27, 2023
@rruuaanng
Copy link
Contributor

Bug report

Bug description:

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 required
parser = 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')
# Show
sub_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)

It's also present in the main branch.

@serhiy-storchaka
Copy link
Member

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.

@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Doc issues
Development

No branches or pull requests

4 participants