Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-59330: argparse: Properly handle positional arguments containing '-' #104092

Closed
wants to merge 3 commits into from

Conversation

furkanonder
Copy link
Contributor

@furkanonder furkanonder commented May 2, 2023

@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label May 3, 2023
@furkanonder furkanonder changed the title GH-59330: Properly handle positional arguments containing '-' in the argparse gh-59330: Properly handle positional arguments containing '-' in the argparse Aug 6, 2023
@@ -1577,6 +1587,9 @@ def _get_positional_kwargs(self, dest, **kwargs):
if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
kwargs['required'] = True

# make dest attribute-accessible, 'foo-bar' -> 'foo_bar'
dest = dest.replace('-', '_')
Copy link
Member

Choose a reason for hiding this comment

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

It has a side effect of changing the default metavar.

return key in self.__dict__ or key.replace('-', '_') in self.__dict__

def __getattr__(self, name):
# Compatibility for people doing getattr(args, 'foo-bar') instead of
Copy link
Member

Choose a reason for hiding this comment

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

But what about setattr(args, 'foo-bar') and delattr(args, 'foo-bar')?

@@ -0,0 +1 @@
Hyphens in positional arguments in argparse module are automatically replaced with underscores. Patch by Simon Law and Furkan Onder.
Copy link
Member

Choose a reason for hiding this comment

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

It should be in the Library section.

@AlexWaygood AlexWaygood changed the title gh-59330: Properly handle positional arguments containing '-' in the argparse gh-59330: argparse: Properly handle positional arguments containing '-' Jan 17, 2024
@serhiy-storchaka
Copy link
Member

Close this PR as it is too complex and may still break user code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants