Skip to content

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

Closed
furkanonder wants to merge 3 commits into
python:mainfrom
furkanonder:issue-59330
Closed

gh-59330: argparse: Properly handle positional arguments containing '-'#104092
furkanonder wants to merge 3 commits into
python:mainfrom
furkanonder:issue-59330

Conversation

@furkanonder

@furkanonder furkanonder commented May 2, 2023

Copy link
Copy Markdown
Contributor

@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
Comment thread Lib/argparse.py
kwargs['required'] = True

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

Copy link
Copy Markdown
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.

Comment thread Lib/argparse.py
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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