Skip to content

argparse: ambiguous option reported with primary parser #109990

Closed as not planned
Closed as not planned

Description

@getsimonz

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

Activity

  1. changed the title [-]ambiguous option reported with primary parser[/-] [+]argparse: ambiguous option reported with primary parser[/+] on Sep 27, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 27, 2023
  3. rruuaanng commented on Sep 16, 2024

    @rruuaanng
    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.

  4. serhiy-storchaka commented on Sep 26, 2024

    @serhiy-storchaka
    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions