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 — custom prefixes for `argparse.BooleanOptionalAction` · Issue #119377 · python/cpython

custom prefixes for argparse.BooleanOptionalAction #119377

New issue
Copy link
New issue
Copy link
Open
Open
custom prefixes for argparse.BooleanOptionalAction#119377
Copy link
Assignees
serhiy-storchaka
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

@e-kwsm
e-kwsm
opened on May 22, 2024
Issue body actions

Feature or enhancement

Proposal:

At the moment, argparse.BooleanOptionalAction gives e.g. --foo vs --no-foo, whose prefix is hardcoded:

cpython/Lib/argparse.py

Line 848 in cd516cd

option_string = '--no-' + option_string[2:]

cpython/Lib/argparse.py

Line 863 in cd516cd

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

Activity

  1. e-kwsm
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 22, 2024
  2. aisk
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 22, 2024
  3. serhiy-storchaka
    added this to Argparse issueson May 22, 2024
  4. serhiy-storchaka
    moved this to Features in Argparse issueson Oct 11, 2024
  5. serhiy-storchaka
    self-assigned this
    on Oct 23, 2024
  6. serhiy-storchaka commented on Oct 26, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 26, 2024
    Member
    More actions

    This looks like reasonable request. But what API do you want?

    1. Two new parameters for positive and negative suffixes.
      parser.add_argument('--enable-foo', action=BooleanOptionalAction, true_prefix='--enable-', false_prefix='--disable-')
    2. Single parameter that takes a pair of suffixes.
      parser.add_argument('--enable-foo', action=BooleanOptionalAction, prefixes=('--enable-', '--disable-'))
    3. Simply a parameter for negative option strings.
      parser.add_argument('--enable-foo', action=BooleanOptionalAction, negative_option_strings=['--disable-foo'])
  7. mpkocher commented on Jun 29, 2025

    @mpkocher
    mpkocher
    on Jun 29, 2025
    Contributor
    More actions

    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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

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.