-
-
Notifications
You must be signed in to change notification settings - Fork 34k
bpo-29626: spacing issue when using help in argparse #1835
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
|
@csabella, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benjaminp, @bethard and @bitdancer to be potential reviewers. |
|
Please add comments to the issue about your proposed solution. |
Lib/argparse.py
Outdated
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.
(option_string) is not a 1-element tuple.
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.
I've fixed it. I haven't worked with the older version of string formatting, so I wasn't aware I did a hybrid of the two methods. It seems that a 1-element tuple is preferred, so I've changed it to be that.
|
@bitdancer Thank you. I've added a comment to the issue tracker. |
Lib/argparse.py
Outdated
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.
If option_string is a string, '%s' % (option_string,) can be replaced with just option_string. And the loop can be replaced with the single extend() call as at line 548.
But I'm not convinced that supporting empty names is a good idea.
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.
Thanks @serhiy-storchaka . I've been looking at this trying to figure out what you meant by using a single extend() call.
The original code is:
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
for option_string in action.option_strings:
parts.append('%s %s' % (option_string, args_string))
So, are you saying that this change would be:
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
if not args_string:
parts.extend(action.option_strings)
else:
for option_string in action.option_strings:
parts.append('%s %s' % (option_string, args_string))
or is there a way to incorporate the existing value string append parts.append('%s %s' % (option_string, args_string)) into the extend() as well?
Sorry if this is a basic question.
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.
Hi @csabella, I'm looking at old PRs and this one seems still relevant as thee bug is still present on master. I think parts.extend(action.option_strings) was what Serhiy Stochaka meant by replacing the loop with extend().
A metavar value of an empty string was causing an extra space to be printed in argparse help.
For example, the created test case was printing:
-f , --foo (extra space before the comma)
instead of
-f, --foo