Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Lib/argparse.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1469,10 +1469,8 @@ def _get_optional_kwargs(self, *args, **kwargs):

# strings starting with two prefix characters are long options
option_strings.append(option_string)
if option_string[0] in self.prefix_chars:
if len(option_string) > 1:
if option_string[1] in self.prefix_chars:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these checks are redundant.

Consider self.prefix_chars ={'-'}

The left hand side requires '--foo' whereas your updated code incorrectly allows X-foo

I do think these conditions could be combined, perhaps something like:

if ( option_string[0] inself.prefix_charsandlen(option_string) >1andoption_string[1] inself.prefix_chars ): ...

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Please check the code from line no 1463. It checks for the condition option_string[0] in self.prefix_chars.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah indeed! Maybe use elif so that's more clear at a glance?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I can do this.

if not option_string[0] in self.prefix_chars: # Some code. raise else: option_strings.append(option_string) if len(option_string) > 1 and option_string[1] in self.prefix_chars: long_option_strings.append(option_string) 

Or the current code is also fine.

Copy link
ContributorAuthor

@sp1rssp1rsAug 17, 2018

Choose a reason for hiding this comment

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

@asottile This change looks good. No need for else. This looks cleaner. Please review.

long_option_strings.append(option_string)
if len(option_string) > 1 and option_string[1] in self.prefix_chars:
long_option_strings.append(option_string)

# infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
dest = kwargs.pop('dest', None)
Expand Down