Skip to content
Myr

Merge pull request #1455 from Na-Ne/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

use difflib for name suggestions in error messages

authored by David Lord · committed by GitHub · parents 7147f41, 27ec938 · Browse files · 3 files+21 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+21 added−1 removed

  • tests:+16 added−0 removed
  • other:+5 added−1 removed

3 files+21 added−1 removed

modified:src/click/parser.py+3 added-1 removed
327 unmodified lines
328 # not a very interesting subset!329330 def _match_long_opt(self, opt, explicit_value, state):331 if opt not in self._long_opt:332 possibilities = [word for word in self._long_opt if word.startswith(opt)]332 from difflib import get_close_matches333334 possibilities = get_close_matches(opt, self._long_opt)335 raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx)336337 option = self._long_opt[opt]338 if option.takes_value:
99 unmodified lines
modified:tests/test_options.py+16 added
84 unmodified lines
85 assert result.exception86 assert f"no such option: {unknown_flag}" in result.output878889@pytest.mark.parametrize(90 ("value", "expect"),91 [92 ("--cat", "Did you mean --count?"),93 ("--bounds", "(Possible options: --bound, --count)"),94 ("--bount", "(Possible options: --bound, --count)"),95 ],96)97def test_suggest_possible_options(runner, value, expect):98 cli = click.Command(99 "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]100 )101 result = runner.invoke(cli, [value])102 assert expect in result.output103104105def test_multiple_required(runner):106 @click.command()107 @click.option("-m", "--message", multiple=True, required=True)108 def cli(message):
513 unmodified lines
modified:CHANGES.rst+2 added
82 unmodified lines
83 ``name`` and ``mode`` attributes. This lets ``File`` options with84 the ``-`` value match non-testing behavior. :issue:`1064`85- When creating a ``Group``, allow passing a list of commands instead86 of a dict. :issue:`1339`87- When a long option name isn't valid, use ``difflib`` to make better88 suggestions for possible corrections. :issue:`1446`899091Version 7.1.292-------------
635 unmodified lines