Skip to content

Commit

Permalink
Merge pull request #794 from ericfrederich/deterministic_names
Browse files Browse the repository at this point in the history
Bug fix #793; use deterministic option name; can't rely on list.sort
  • Loading branch information
untitaker authored May 21, 2017
2 parents 2ab5f7e + f15f425 commit ebcbef9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Version 6.8
- Fix bug in test runner when calling ``sys.exit`` with ``None``. See #739.
- Fix crash on Windows console, see #744.
- Fix bashcompletion on chained commands. See #754.
- Fix option naming routine to match documentation. See #793

Version 6.7
-----------
Expand Down
4 changes: 2 additions & 2 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,8 @@ def _parse_decls(self, decls, expose_value):
opts.append(decl)

if name is None and possible_names:
possible_names.sort(key=lambda x: len(x[0]))
name = possible_names[-1][1].replace('-', '_').lower()
possible_names.sort(key=lambda x: -len(x[0])) # group long options first
name = possible_names[0][1].replace('-', '_').lower()
if not isidentifier(name):
name = None

Expand Down
22 changes: 22 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,25 @@ def cli_alt(warnings):
assert result.output == 'False\n'
result = runner.invoke(cli_alt, ['-w'])
assert result.output == 'True\n'

@pytest.mark.parametrize('option_args,expected', [
(['--aggressive', '--all', '-a'], 'aggressive'),
(['--first', '--second', '--third', '-a', '-b', '-c'], 'first'),
(['--apple', '--banana', '--cantaloupe', '-a', '-b', '-c'], 'apple'),
(['--cantaloupe', '--banana', '--apple', '-c', '-b', '-a'], 'cantaloupe'),
(['-a', '-b', '-c'], 'a'),
(['-c', '-b', '-a'], 'c'),
(['-a', '--apple', '-b', '--banana', '-c', '--cantaloupe'], 'apple'),
(['-c', '-a', '--cantaloupe', '-b', '--banana', '--apple'], 'cantaloupe'),
])
def test_multiple_long_options(runner, option_args, expected):
@click.command()
@click.option(*option_args, is_flag=True)
def cmd(**kwargs):
click.echo(str(kwargs[expected]))

assert cmd.params[0].name == expected

for form in option_args:
result = runner.invoke(cmd, [form])
assert result.output == 'True\n'

0 comments on commit ebcbef9

Please sign in to comment.