Remove hidden commands and options from completion results
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicholas Wiles · committed by David Lord · parent df0f81d · Browse files · 3 files·+61 added−10 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+61 added−10 removed
- tests:+36 added−0 removed
- other:+25 added−10 removed
Provenance
- Commit
ecf4ce6- Tree
69498b0- Author
Nicholas Wiles <nwiles@google.com>- Committer
David Lord <davidism@gmail.com>- Parent
df0f81d
3 files changed+61 added−10 removed
modified:click/_bashcomplete.py+22 added-10 removed
21 unmodified lines22 23 %(complete_func)setup() {24 local COMPLETION_OPTIONS=""25 local BASH_VERSION_ARR=(${BASH_VERSION//./ })26- if [ ${BASH_VERSION_ARR[0]} -ge 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ];then26+ # Only BASH version 4.4 and later have the nosort option.27+ if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then28 COMPLETION_OPTIONS="-o nosort"29 fi30 31 complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s144 unmodified lines176 results = []177 if isinstance(cmd_param.type, Choice):178 # Choices don't support descriptions.179 results = [(c, None)179- for c in cmd_param.type.choices if c.startswith(incomplete)]180+ for c in cmd_param.type.choices if str(c).startswith(incomplete)]181 elif cmd_param.autocompletion is not None:182 dynamic_completions = cmd_param.autocompletion(ctx=ctx,183 args=args,184 incomplete=incomplete)1 unmodified line186 for c in dynamic_completions]187 return results188 189 190+def get_visible_commands_starting_with(ctx, starts_with):191+ """192+ :param ctx: context associated with the parsed command193+ :starts_with: string that visible commands must start with.194+ :return: all visible (not hidden) commands that start with starts_with.195+ """196+ for c in ctx.command.list_commands(ctx):197+ if c.startswith(starts_with):198+ command = ctx.command.get_command(ctx, c)199+ if not command.hidden:200+ yield command201+202+203 def add_subcommand_completions(ctx, incomplete, completions_out):204 # Add subcommand completions.205 if isinstance(ctx.command, MultiCommand):206 completions_out.extend(193- [(c, ctx.command.get_command(ctx, c).get_short_help_str()) for c in ctx.command.list_commands(ctx) if c.startswith(incomplete)])207+ [(c.name, c.get_short_help_str()) for c in get_visible_commands_starting_with(ctx, incomplete)])208 209 # Walk up the context list and add any other completion possibilities from chained commands210 while ctx.parent is not None:211 ctx = ctx.parent212 if isinstance(ctx.command, MultiCommand) and ctx.command.chain:199- remaining_commands = sorted(200- set(ctx.command.list_commands(ctx)) - set(ctx.protected_args))201- completions_out.extend(202- [(c, ctx.command.get_command(ctx, c).get_short_help_str()) for c in remaining_commands if c.startswith(incomplete)])213+ remaining_commands = [c for c in get_visible_commands_starting_with(ctx, incomplete)214+ if c.name not in ctx.protected_args]215+ completions_out.extend([(c.name, c.get_short_help_str()) for c in remaining_commands])216 217 218 def get_choices(cli, prog_name, args, incomplete):219 """21 unmodified lines241 completions = []242 if start_of_option(incomplete):243 # completions for partial options244 for param in ctx.command.params:232- if isinstance(param, Option):245+ if isinstance(param, Option) and not param.hidden:246 param_opts = [param_opt for param_opt in param.opts +247 param.secondary_opts if param_opt not in all_args or param.multiple]235- completions.extend(236- [(o, param.help) for o in param_opts if o.startswith(incomplete)])248+ completions.extend([(o, param.help) for o in param_opts if o.startswith(incomplete)])249 return completions250 # completion for option values from user supplied values251 for param in ctx.command.params:252 if is_incomplete_option(all_args, param):37 unmodified linesmodified:tests/test_bashcomplete.py+36 added
369 unmodified lines370 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']374+375+376+def test_hidden():377+ @click.group()378+ @click.option('--name', hidden=True)379+ @click.option('--choices', type=click.Choice([1, 2]), hidden=True)380+ def cli(name):381+ pass382+383+ @cli.group(hidden=True)384+ def hgroup():385+ pass386+387+ @hgroup.group()388+ def hgroupsub():389+ pass390+391+ @cli.command()392+ def asub():393+ pass394+395+ @cli.command(hidden=True)396+ @click.option('--hname')397+ def hsub():398+ pass399+400+ assert choices_without_help(cli, [], '--n') == []401+ assert choices_without_help(cli, [], '--c') == []402+ # If the user exactly types out the hidden param, complete its options.403+ assert choices_without_help(cli, ['--choices'], '') == [1, 2]404+ assert choices_without_help(cli, [], '') == ['asub']405+ assert choices_without_help(cli, [], '') == ['asub']406+ assert choices_without_help(cli, [], 'h') == []407+ # If the user exactly types out the hidden command, complete its subcommands.408+ assert choices_without_help(cli, ['hgroup'], '') == ['hgroupsub']409+ assert choices_without_help(cli, ['hsub'], '--h') == ['--hname']modified:CHANGES.rst+3 added
82 unmodified lines83 - Don't add newlines by default for progress bars. (`#1013`_)84 - Use Python sorting order for ZSH completions. (`#1047`_, `#1059`_)85 - Document that parameter names are lowercased by default. (`#1055`_)86 - Subcommands that are named by the function now automatically have the underscore replaced with a dash. If you register a function named ``my_command`` it becomes ``my-command`` in the command line interface.87+- Hide hidden commands and options from completion. (`#1058`_, `#1061`_)88 89 .. _#202: https://github.com/pallets/click/issues/20290 .. _#323: https://github.com/pallets/click/issues/32391 .. _#334: https://github.com/pallets/click/issues/334101 unmodified lines193 .. _#1022: https://github.com/pallets/click/pull/1022194 .. _#1027: https://github.com/pallets/click/pull/1027195 .. _#1047: https://github.com/pallets/click/pull/1047196 .. _#1055: https://github.com/pallets/click/pull/1055197+.. _#1058: https://github.com/pallets/click/pull/1058198 .. _#1059: https://github.com/pallets/click/pull/1059199+.. _#1061: https://github.com/pallets/click/pull/1061200 201 202 Version 6.7203 -----------343 unmodified lines