FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`argparse.BooleanOptionalAction` silently failed when used with parser whose `prefix_chars` has no `'-'` · Issue #138525 · python/cpython · GitHub

Repository navigation

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

Closed

Description

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 5, 2025
  2. serhiy-storchaka commented on Sep 5, 2025

    Member

    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.

  3. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Sep 5, 2025
  4. added a commit that references this issue on Sep 9, 2025
  5. serhiy-storchaka commented on Sep 9, 2025

    Member

    #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.

  6. added 2 commits that reference this issue on Sep 9, 2025
  7. added a commit that references this issue on Nov 22, 2025
  8. moved this from Doc issues to Features in Argparse issueson Nov 22, 2025
  9. added a commit that references this issue on Dec 6, 2025
  10. added a commit that references this issue on Dec 8, 2025
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-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL