argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-' #138525
Copy link
Copy link
Closed
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
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
Metadata
Metadata
Assignees
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
- StatusShow more project fieldsFeatures
Activity
serhiy-storchaka commentedon Sep 5, 2025
Alternatively, it could support alternative prefix chars. For example,
++no-fooif++foowas specified. It is not easy, becauseprefix_charsis not available in theBooleanOptionalActionconstructor.See also #119377.
pythongh-138525: Support single-dash long options and prefix_chars in…
serhiy-storchaka commentedon Sep 9, 2025
#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-foowill be generated for++foo, and/no-foofor/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.pythongh-138525: Support single-dash long options and prefix_chars in…
pythongh-138525: Support single-dash long options and prefix_chars in…
Merge branch 'main' into pythongh-138525
gh-138525: Support single-dash long options and prefix_chars in Boole…
pythongh-138525: Support single-dash long options and prefix_chars in…
pythongh-138525: Support single-dash long options and prefix_chars in…