From f15f425bc11a69a4dc856d43d25e223dcc15f0f2 Mon Sep 17 00:00:00 2001 From: Eric L Frederich Date: Fri, 19 May 2017 09:00:18 -0400 Subject: [PATCH] Bug fix #793; use the first defined long name --- CHANGES | 1 + click/core.py | 4 ++-- tests/test_options.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 2bc5dde6e..6c49ad4c1 100644 --- a/CHANGES +++ b/CHANGES @@ -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 ----------- diff --git a/click/core.py b/click/core.py index 745645147..6b981a6c1 100644 --- a/click/core.py +++ b/click/core.py @@ -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 diff --git a/tests/test_options.py b/tests/test_options.py index d196fe279..9dd8cdff3 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -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'