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 — bpo-8538: Add support for boolean actions to argparse by remilapeyre · Pull Request #11478 · python/cpython

Conversation

@remilapeyre

@remilapeyre remilapeyre commented Jan 9, 2019 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue8538

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

guludo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Rémi Lapeyre
Add support for boolean actions to argparse
d5d92a1
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jan 9, 2019
@bedevere-bot bedevere-bot added the awaiting review label Jan 9, 2019
kenahoo
kenahoo reviewed Jan 9, 2019
View reviewed changes
Comment thread Doc/library/argparse.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@kenahoo remilapeyre
Fix doc typo in Doc/library/argparse.rst
3567f32
Co-Authored-By: remilapeyre <[email protected]>
@the-knights-who-say-ni

the-knights-who-say-ni commented Jan 10, 2019

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added CLA not signed and removed CLA signed labels Jan 10, 2019
@remilapeyre

remilapeyre commented Jan 10, 2019

Copy link
Copy Markdown
Contributor Author

@kenahoo Can you sign the CLA to make @the-knights-who-say-ni happy?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kenahoo

kenahoo commented Jan 10, 2019

Copy link
Copy Markdown

Thanks @remilapeyre , I've signed it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added CLA signed and removed CLA not signed labels Jan 11, 2019
@guludo

guludo commented Jan 14, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Hi!

I was about to write a patch to add the same feature. Good thing that I decided to look if someone was already working on it, thank you :-)

I'd like to contribute with the following comments:

1. Register action rather than exposing the class

I don't think BooleanOptionalAction should be exposed to the module user. Rather, I think it should be "private" (i.e., _BooleanOptionalAction) and it should be registered like the other ones (look for calls like self.register('action', ...) in the source file). My suggestion is:

self.register('action', 'optional_boolean', _BooleanOptionalAction)

2. Be aware of short options

Your current implementation does not ignore short options when adding the prefix '--no-'. They should be ignored IMO.

3. Ordering of parameters names

I think the --no-<option_name> variants should be next to --<option_name> in the list. Consider the following case where the user decided to have 2 names for the same option:

parser.add_argument('--foo', '--bar', action='optional_boolean', ...)

Then, in the help message, --foo and --bar would appear before --no-foo and --no-bar.

4. Documentation

In the case of registering the action 'optional_boolean', it would be necessary to update the documentation on the list of supported values for the action keyword of add_argument.

5. My custom action class

About (2) and (3), I have a custom implementation in one project of mine that does that:

class BoolAction(argparse.Action):
    def __init__(self, option_strings, dest, **kw):
        self.original_option_strings = option_strings

        kw['nargs'] = 0

        option_strings = []
        for s in self.original_option_strings:
            option_strings.append(s)

            if s.startswith('--'):
                s = '--no-' + s[2:]
                option_strings.append(s)

        super(BoolAction, self).__init__(option_strings, dest, **kw)

    def __call__(self, parser, namespace, values, option_string):
        value = option_string in self.original_option_strings
        setattr(namespace, self.dest, value)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kenahoo

kenahoo commented Jan 23, 2019

Copy link
Copy Markdown

All of @guludo's comments seem reasonable to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Rémi Lapeyre
Fix option ordering in help and short options for BooleanOptionalAction
b0652c9
@remilapeyre

remilapeyre commented Jan 28, 2019

Copy link
Copy Markdown
Contributor Author

Hi @guludo, about 1. this has been discussed on b.p.o (https://bugs.python.org/issue8538#msg105620) and Steven Bethard seems to agree (https://bugs.python.org/issue8538#msg166165) so I think it is better to keep it that way before a core reviewers either confirm this API or asks for a registered action. I think it makes 4. unneeded.

Nice catch for 2. and 3. I used something similar than you did for short options. The latest commit fix the ordering too.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matrixise matrixise self-assigned this Sep 12, 2019
@matrixise matrixise closed this Sep 13, 2019
@matrixise matrixise reopened this Sep 13, 2019
@matrixise

matrixise commented Sep 13, 2019

Copy link
Copy Markdown
Member

I would like to see the result of Travis...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matrixise
Remove useless maxDiff in test_argparse.py
9d226ec
matrixise
matrixise approved these changes Sep 13, 2019
View reviewed changes

@matrixise matrixise left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution, I like the feature.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Sep 13, 2019
@matrixise
matrixise merged commit 6a517c6 into python:master Sep 13, 2019
@bedevere-bot bedevere-bot removed the awaiting merge label Sep 13, 2019
@matrixise

matrixise commented Sep 13, 2019

Copy link
Copy Markdown
Member

Thank you for your contribution and this feature, really appreciated.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@remilapeyre

remilapeyre commented Sep 13, 2019

Copy link
Copy Markdown
Contributor Author

Thanks @matrixise !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matrixise

matrixise commented Sep 16, 2019

Copy link
Copy Markdown
Member

@remilapeyre with pleasure.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hauntsaninja
hauntsaninja reviewed Jun 3, 2020
View reviewed changes
Comment thread Lib/argparse.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hauntsaninja hauntsaninja mentioned this pull request Jun 3, 2020
argparse: fix error __init__, add BooleanOptionalAction for py39 python/typeshed#4144
Merged
hauntsaninja pushed a commit to hauntsaninja/typeshed that referenced this pull request Jun 26, 2020
hauntsaninja
argparse: fix for latest py39
b165e85
python#4144 and
python/cpython#11478 (review)
resulted in the issue being fixed upstream.

A fix in time saves a branch in typeshed :-)
@hauntsaninja hauntsaninja mentioned this pull request Jun 26, 2020
argparse: fix for latest py39 python/typeshed#4267
Merged
srittau pushed a commit to python/typeshed that referenced this pull request Jun 26, 2020
@hauntsaninja
argparse: fix for latest py39 (#4267)
9a16b02
#4144 and
python/cpython#11478 (review)
resulted in the issue being fixed upstream.


Co-authored-by: hauntsaninja <>
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
@hauntsaninja Vishal Kuo
argparse: fix for latest py39 (python#4267)
1bb27cd
python#4144 and
python/cpython#11478 (review)
resulted in the issue being fixed upstream.


Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hauntsaninja hauntsaninja hauntsaninja left review comments
+2 more reviewers
@kenahoo kenahoo kenahoo left review comments
@matrixise matrixise matrixise approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@matrixise matrixise

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@remilapeyre @the-knights-who-say-ni @kenahoo @guludo @matrixise @hauntsaninja @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.