Skip to content

Conversation

@furkanonder
Copy link
Contributor

@furkanonder furkanonder commented May 2, 2023

@arhadthedev arhadthedev added the stdlib Standard Library Python modules in the Lib/ directory 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
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 Standard Library Python modules in the Lib/ directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants