GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `argparse.BooleanOptionalAction` silently failed when used with parser whose `prefix_chars` has no `'-'` · Issue #138525 · python/cpython

argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-' #138525

New issue
Copy link
New issue
Copy link
Closed
Closed
argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-'#138525
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@mattwang44
mattwang44
opened on Sep 5, 2025
Issue body actions

Bug report

Bug description:

argparse.BooleanOptionalAction (doc) handles the creation of the boolean flags with positive and negative options by having user only needs to add the positive argument. However, it only works with those parsers whose prefix_chars contains '-', which is the default value, and otherwise failed to create the negative option silently.

>>> from argparse import BooleanOptionalAction, ArgumentParser
>>> # normal usage
>>> p = ArgumentParser()  # prefix_chars defaults to '-'
>>> p.add_argument("--foo", action=BooleanOptionalAction)
BooleanOptionalAction(option_strings=['--foo', '--no-foo'], dest='foo', nargs=0, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> p.parse_args(['--no-foo'])
Namespace(foo=False)
>>>
>>> # parser with customized prefix_chars
>>> p = ArgumentParser(prefix_chars='+')
>>> p.add_argument("++foo", action=BooleanOptionalAction)
BooleanOptionalAction(option_strings=['++foo'], dest='foo', nargs=0, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)

Raising warning/error is expected when argparse.BooleanOptionalAction is used with parsers whose prefix_chars has no '-'.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

  • gh-138525: raise error when BooleanOptionalAction used with prefix_chars without '-' #138526
  • gh-138525: Support single-dash long options and prefix_chars in BooleanOptionalAction #138692

Activity

  1. mattwang44
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Sep 5, 2025
  2. StanFromIreland
    added this to Argparse issueson Sep 5, 2025
  3. StanFromIreland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Sep 5, 2025
  4. serhiy-storchaka commented on Sep 5, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 5, 2025
    Member
    More actions

    Alternatively, it could support alternative prefix chars. For example, ++no-foo if ++foo was specified. It is not easy, because prefix_chars is not available in the BooleanOptionalAction constructor.

    See also #119377.

    Reacted by W. H. Wang
  5. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Sep 5, 2025
  6. serhiy-storchaka
    added a commit that references this issue on Sep 9, 2025

    pythongh-138525: Support single-dash long options and prefix_chars in…

    fe05304
  7. serhiy-storchaka commented on Sep 9, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 9, 2025
    Member
    More actions

    #138692 is an alternative PR that adds support for alternate prefix_chars (and for single-dash long options, since they are common when alternate prefix_chars are used). So ++no-foo will be generated for ++foo, and /no-foo for /foo.

    There is still an issue for the case when different prefix chars are used (e.g. -+foo), but this is uncommon case, I never encountered such options in wild. There are no tests for such case, so this is not intentionally added feature. I think it should not be supported, but this is a different issue.

  8. serhiy-storchaka
    added 2 commits that reference this issue on Sep 9, 2025

    pythongh-138525: Support single-dash long options and prefix_chars in…

    bd8247f

    pythongh-138525: Support single-dash long options and prefix_chars in…

    4fa348b
  9. mattwang44
    added a commit that references this issue on Nov 20, 2025

    Merge branch 'main' into pythongh-138525

    c6221f5
  10. serhiy-storchaka
    added a commit that references this issue on Nov 22, 2025

    gh-138525: Support single-dash long options and prefix_chars in Boole…

    425fd85
  11. serhiy-storchaka
    closed this as completedon Nov 22, 2025
  12. github-project-automation
    moved this to Doc issues in Argparse issueson Nov 22, 2025
  13. serhiy-storchaka
    moved this from Doc issues to Features in Argparse issueson Nov 22, 2025
  14. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-138525: Support single-dash long options and prefix_chars in…

    076e529
  15. ashm-dev
    added a commit that references this issue on Dec 8, 2025

    pythongh-138525: Support single-dash long options and prefix_chars in…

    4edec83
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/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Argparse issues
      Status
      Features
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.