-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-59330: argparse: Properly handle positional arguments containing '-' #104092
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
Conversation
@@ -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('-', '_') |
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.
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 |
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.
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. |
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.
It should be in the Library section.
Close this PR as it is too complex and may still break user code. |
Co-authored by: Simon Law