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.
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.
#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.
Activity
serhiy-storchaka commented on Sep 5, 2025
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.
pythongh-138525: Support single-dash long options and prefix_chars in…
serhiy-storchaka commented on 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-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.
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…