Merge pull request #930 from stopthatcow/feature/925
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Overhaul bash completion to mirror invoke logic
authored by David Lord · committed by GitHub · parents 31d19ba, fbd18ec · Browse files · 4 files·+138 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+138 added−47 removed
- tests:+100 added−24 removed
- other:+38 added−23 removed
Provenance
- Commit
be28b6c- Tree
7472fa7- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
31d19ba- Parent 2
fbd18ec
4 files changed+138 added−47 removed
modified:click/_bashcomplete.py+24 added-16 removed
71 unmodified lines72 :param args: full list of args73 :return: the final context/command parsed74 """75 ctx = cli.make_context(prog_name, args, resilient_parsing=True)76- args_remaining = ctx.protected_args + ctx.args77- while ctx is not None and args_remaining:76+ args = ctx.protected_args + ctx.args77+ while args:78 if isinstance(ctx.command, MultiCommand):79+ if not ctx.command.chain:79- cmd = ctx.command.get_command(ctx, args_remaining[0])80- if cmd is None:81- return None82- ctx = cmd.make_context(83- args_remaining[0], args_remaining[1:], parent=ctx, resilient_parsing=True)84- args_remaining = ctx.protected_args + ctx.args80+ cmd_name, cmd, args = ctx.command.resolve_command(ctx, args)81+ if cmd is None:82+ return ctx83+ ctx = cmd.make_context(cmd_name, args, parent=ctx,84+ resilient_parsing=True)85+ args = ctx.protected_args + ctx.args86+ else:87+ # Walk chained subcommand contexts saving the last one.88+ while args:89+ cmd_name, cmd, args = ctx.command.resolve_command(ctx, args)90+ if cmd is None:91+ return ctx92+ sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,93+ allow_extra_args=True,94+ allow_interspersed_args=False,95+ resilient_parsing=True)96+ args = sub_ctx.args97+ ctx = sub_ctx98+ args = sub_ctx.protected_args + sub_ctx.args99 else:86- ctx = ctx.parent87-100+ break101 return ctx102 103 104 def start_of_option(param_str):123 unmodified lines228 return get_user_autocompletions(ctx, all_args, incomplete, param)229 # completion for argument values from user supplied values230 for param in ctx.command.params:231 if is_incomplete_argument(ctx.params, param):219- completions.extend(get_user_autocompletions(220- ctx, all_args, incomplete, param))221- # Stop looking for other completions only if this argument is required.222- if param.required:223- return completions224- break232+ return get_user_autocompletions(ctx, all_args, incomplete, param)233 234 add_subcommand_completions(ctx, incomplete, completions)235 return completions236 27 unmodified linesmodified:click/core.py+6 added-4 removed
180 unmodified lines181 default. In addition to that, formatters might182 add some safety mapping on the right.183 :param resilient_parsing: if this flag is enabled then Click will184 parse without any interactivity or callback185- invocation. This is useful for implementing185+ invocation. Default values will also be186+ ignored. This is useful for implementing187 things such as completion support.188 :param allow_extra_args: if this is set to `True` then extra arguments189 at the end will not raise an error and will be190 kept on the context. The default is to inherit121 unmodified lines312 #: options, choices, commands etc.313 self.token_normalize_func = token_normalize_func314 315 #: Indicates if resilient parsing is enabled. In that case Click315- #: will do its best to not cause any failures.316+ #: will do its best to not cause any failures and default values317+ #: will be ignored. Useful for completion.318 self.resilient_parsing = resilient_parsing319 320 # If there is no envvar prefix yet, but the parent has one and321 # the command on this level has a name, we can expand the envvar856 unmodified lines1178 # something else we should do: if the first argument looks like1179 # an option we want to kick off parsing again for arguments to1180 # resolve things like --help which now should go to the main1181 # place.1180- if cmd is None:1182+ if cmd is None and not ctx.resilient_parsing:1183 if split_opt(cmd_name)[0]:1184 self.parse_args(ctx, ctx.args)1185 ctx.fail('No such command "%s".' % original_cmd_name)1186 246 unmodified lines1433 1434 def full_process_value(self, ctx, value):1435 value = self.process_value(ctx, value)1436 1435- if value is None:1437+ if value is None and not ctx.resilient_parsing:1438 value = self.get_default(ctx)1439 1440 if self.required and self.value_is_missing(value):1441 raise MissingParameter(ctx=ctx, param=self)412 unmodified linesmodified:tests/test_bashcomplete.py+100 added-24 removed
130 unmodified lines131 132 def test_chaining():133 @click.group('cli', chain=True)134 @click.option('--cli-opt')135+ @click.argument('arg', type=click.Choice(['cliarg1', 'cliarg2']))135- def cli(cli_opt):136+ def cli(cli_opt, arg):137 pass138 139 @cli.command()140 @click.option('--asub-opt')1 unmodified line142 pass143 144 @cli.command(help='bsub help')145 @click.option('--bsub-opt')145- @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)146+ @click.argument('arg', type=click.Choice(['arg1', 'arg2']))147 def bsub(bsub_opt, arg):148 pass149 150 @cli.command()151 @click.option('--csub-opt')151- @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)152+ @click.argument('arg', type=click.Choice(['carg1', 'carg2']), default='carg1')153 def csub(csub_opt, arg):154 pass155 156 assert choices_without_help(cli, [], '-') == ['--cli-opt']156- assert choices_without_help(cli, [], '') == ['asub', 'bsub', 'csub']157- assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']158- assert choices_without_help(cli, ['asub'], '') == ['bsub', 'csub']159- assert choices_without_help(cli, ['bsub'], '') == ['arg1', 'arg2']160- assert choices_without_help(cli, ['asub', '--asub-opt'], '') == []161- assert choices_without_help(cli, ['asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']162- assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']163- assert choices_with_help(cli, ['asub'], 'b') == [('bsub', 'bsub help')]164- assert choices_without_help(cli, ['asub', 'csub'], '-') == ['--csub-opt']157+ assert choices_without_help(cli, [], '') == ['cliarg1', 'cliarg2']158+ assert choices_without_help(cli, ['cliarg1', 'asub'], '-') == ['--asub-opt']159+ assert choices_without_help(cli, ['cliarg1', 'asub'], '') == ['bsub', 'csub']160+ assert choices_without_help(cli, ['cliarg1', 'bsub'], '') == ['arg1', 'arg2']161+ assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt'], '') == []162+ assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']163+ assert choices_without_help(cli, ['cliarg1', 'asub', 'bsub'], '-') == ['--bsub-opt']164+ assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '') == ['carg1', 'carg2']165+ assert choices_without_help(cli, ['cliarg1', 'bsub', 'arg1', 'csub'], '') == ['carg1', 'carg2']166+ assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '-') == ['--csub-opt']167+ assert choices_with_help(cli, ['cliarg1', 'asub'], 'b') == [('bsub', 'bsub help')]168 169 170 def test_argument_choice():171 @click.command()169- @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))170- @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))171- @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172+ @click.argument('arg1', required=True, type=click.Choice(['arg11', 'arg12']))173+ @click.argument('arg2', type=click.Choice(['arg21', 'arg22']), default='arg21')174+ @click.argument('arg3', type=click.Choice(['arg', 'argument']), default='arg')175 def cli():176 pass177 178 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']5 unmodified lines184 185 def test_option_choice():186 @click.command()187 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')185- @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))188+ @click.option('--opt2', type=click.Choice(['opt21', 'opt22']), default='opt21')189 @click.option('--opt3', type=click.Choice(['opt', 'option']))190 def cli():191 pass192 27 unmodified lines220 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']221 assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']222 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']223 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']224+ assert choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2']225+ assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']226 227 228 def test_boolean_flag_choice():229 @click.command()32 unmodified lines262 263 264 def test_variadic_argument_choice():265 @click.command()266+ @click.option('--opt', type=click.Choice(['opt1', 'opt2']))267 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))268 def cli(local_opt):269 pass270 271 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']272+ assert choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt']273+ assert choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2']274+ assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']275 276 277+def test_variadic_argument_complete():278+279+ def _complete(ctx, args, incomplete):280+ return ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']281+282+ @click.group()283+ def entrypoint():284+ pass285+286+ @click.command()287+ @click.option('--opt', autocompletion=_complete)288+ @click.argument('arg', nargs=-1)289+ def subcommand(opt, arg):290+ pass291+292+ entrypoint.add_command(subcommand)293+294+ assert choices_without_help(entrypoint, ['subcommand', '--opt'], '') == _complete(0,0,0)295+ assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt'], '') == _complete(0,0,0)296+ assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt', 'abc'], '') == []297+298+299 def test_long_chain_choice():300 @click.group()301 def cli():302 pass303 304 @cli.group()305 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))306 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))276- def sub(sub_opt):307+ def sub(sub_opt, sub_arg):308 pass309 310 @sub.command(short_help='bsub help')311 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))1 unmodified line313 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))314 def bsub(bsub_opt):315 pass316 286- assert choices_with_help(cli, ['sub'], '') == [('subarg1', None), ('subarg2', None), ('bsub', 'bsub help')]317+ @sub.group('csub')318+ def csub():319+ pass320+321+ @csub.command()322+ def dsub():323+ pass324+325+ assert choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')]326+ assert choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2']327 assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']328 assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \289- ['subarg1', 'subarg2', 'bsub']329+ ['subarg1', 'subarg2']330 assert choices_without_help(cli,291- ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']331+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']332 assert choices_without_help(cli,293- ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \294- ['bsubopt1', 'bsubopt2']333+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2']334 assert choices_without_help(cli,335+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \336+ ['bsubopt1', 'bsubopt2']337+ assert choices_without_help(cli,296- ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],297- '') == ['bbsubarg1', 'bbsubarg2']338+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],339+ '') == ['bbsubarg1', 'bbsubarg2']340+ assert choices_without_help(cli,341+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'],342+ '') == ['dsub']343+344+345+def test_chained_multi():346+ @click.group()347+ def cli():348+ pass349+350+ @cli.group()351+ def sub():352+ pass353+354+ @sub.group()355+ def bsub():356+ pass357+358+ @sub.group(chain=True)359+ def csub():360+ pass361+362+ @csub.command()363+ def dsub():364+ pass365+366+ @csub.command()367+ def esub():368+ pass369+370+ assert choices_without_help(cli, ['sub'], '') == ['bsub', 'csub']371+ assert choices_without_help(cli, ['sub'], 'c') == ['csub']372+ assert choices_without_help(cli, ['sub', 'csub'], '') == ['dsub', 'esub']373+ assert choices_without_help(cli, ['sub', 'csub', 'dsub'], '') == ['esub']modified:CHANGES.rst+8 added-3 removed
38 unmodified lines39 - Disable ``sys._getframes()`` on Python interpreters that don't support it. (`#728`_)40 - Fix bug in test runner when calling ``sys.exit`` with ``None``. (`#739`_)41 - Clarify documentation on command line options. (`#741`_, `#1003`_)42 - Fix crash on Windows console. (`#744`_)43-- Fix bug that caused bash completion to give improper completions on chained commands when a required option/argument was being completed. (`#754`_, `#774`_, `#790`_, `#806`_)43+- Fix bug that caused bash completion to give improper completions on chained commands. (`#754`_, `#774`_)44 - Added support for dynamic bash completion from a user-supplied callback. (`#755`_)45 - Added support for bash completions containing spaces. (`#773`_)46-- Fix option naming routine to match documentation. (`#793`_)47-- Use deterministic option name; can't rely on list sort. (`#793`_, `#794`_)46+- Allow autocompletion function to determine whether or not to return completions that start with the incomplete argument. (`#790`_, `#806`_)47+- Fix option naming routine to match documentation and be deterministic. (`#793`_, `#794`_)48 - Fix path validation bug. (`#795`_, `#1020`_)49 - Add test and documentation for ``Option`` naming: functionality. (`#799`_)50 - Update doc to match arg name for ``path_type``. (`#801`_)51 - Raw strings added so correct escaping occurs. (`#807`_)7 unmodified lines59 - Fix ``ResourceWarning`` that occurs during some tests. (`#878`_)60 - When detecting a misconfigured locale, don't fail if the ``locale`` command fails. (`#880`_)61 - Add ``case_sensitive=False`` as an option to ``Choice`` types. (`#887`_)62 - Force stdout/stderr writable. This works around issues with badly patched standard streams like those from Jupyter. (`#918`_)63+- Fix completion of subcommand options after last argument (`#919`_, `#930`_)64 - ``_AtomicFile`` now uses the ``realpath`` of the original filename so that chaning the working directory does not affect it. (`#920`_)65+- Fix incorrect completions when defaults are present (`#925`_, `#930`_)66 - Add copy option attrs so that custom classes can be re-used. (`#926`_, `#994`_)67 - "x" and "a" file modes now use stdout when file is ``"-"``. (`#929`_)68 - Fix missing comma in ``__all__`` list. (`#935`_)69 - Clarify how parameters are named. (`#949`_, `#1009`_)81 unmodified lines151 .. _#883: https://github.com/pallets/click/pull/883152 .. _#887: https://github.com/pallets/click/pull/887153 .. _#889: https://github.com/pallets/click/pull/889154 .. _#918: https://github.com/pallets/click/pull/918155+.. _#919: https://github.com/pallets/click/issues/919156 .. _#920: https://github.com/pallets/click/pull/920157+.. _#925: https://github.com/pallets/click/issues/925158 .. _#926: https://github.com/pallets/click/issues/926159 .. _#929: https://github.com/pallets/click/pull/929160+.. _#930: https://github.com/pallets/click/pull/930161 .. _#935: https://github.com/pallets/click/pull/935162 .. _#949: https://github.com/pallets/click/issues/949163 .. _#954: https://github.com/pallets/click/pull/954164 .. _#962: https://github.com/pallets/click/pull/962376 unmodified lines