GeezerVison is showing github.com — custom prefixes for `argparse.BooleanOptionalAction` · Issue #119377 · python/cpython
custom prefixes for argparse.BooleanOptionalAction #119377DescriptionFeature or enhancement
Proposal:
At the moment, argparse.BooleanOptionalAction gives e.g. --foo vs --no-foo, whose prefix is hardcoded:
|
option_string = '--no-' + option_string[2:] |
|
setattr(namespace, self.dest, not option_string.startswith('--no-')) |
It would be beneficial if we can change the prefixes of the binary options: e.g. --enable- vs --disable- like autoconf.
usage: [-h] [--enable-foo | --disable-foo]
options:
-h, --help show this help message and exit
--enable-foo, --disable-foo
helpstring for foo
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response Activityserhiy-storchaka commented on Oct 26, 2024 This looks like reasonable request. But what API do you want?
- Two new parameters for positive and negative suffixes.
parser.add_argument('--enable-foo', action=BooleanOptionalAction, true_prefix='--enable-', false_prefix='--disable-')
- Single parameter that takes a pair of suffixes.
parser.add_argument('--enable-foo', action=BooleanOptionalAction, prefixes=('--enable-', '--disable-'))
- Simply a parameter for negative option strings.
parser.add_argument('--enable-foo', action=BooleanOptionalAction, negative_option_strings=['--disable-foo'])
mpkocher commented on Jun 29, 2025 In general, there's a lot going on in function signature to .add_argument already, I'm not sure that adding more keyword args is really going to help here.
Also, note that the currently hardcoded values have a fundamental issue with custom prefix_chars to ArgumentParser.
p = ArgumentParser(prefix_chars='++')
p.add_argument("++alpha", action=BooleanOptionalAction, help="Enable Alpha mode")
Perhaps, just documenting a recipe for this use case is reasonable.
def _add_bool_optional_group(p: ArgumentParser, dest: str, /, *, enable: str, disable: str, required:bool=True) -> ArgumentParser:
g = p.add_mutually_exclusive_group(required=required)
g.add_argument(enable, action='store_true', help=f"Enable {dest}", dest=dest, default=None)
g.add_argument(disable, action='store_false', help=f"Disable {dest} ", dest=dest, default=None)
return p
def to_parser() -> ArgumentParser:
p = ArgumentParser(description="Example Tool", prog="example.py")
return _add_bool_optional_group(p, "alpha_mode", enable="--alpha", disable="--disable-alpha", required=False)
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|