From 59b42998a8b866a410d30f2d5120f200163aa46d Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Wed, 1 Jul 2020 08:19:02 +0900 Subject: [PATCH] [#1076] Bugfix: Don't generate Autocomplete for hidden commands --- RELEASE-NOTES.md | 1 + src/main/java/picocli/AutoComplete.java | 7 ++++++- src/test/java/picocli/AutoCompleteTest.java | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 8f367b285..6d0aae892 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -222,6 +222,7 @@ To use the `ManPageGenerator` tool as a subcommand, you will need the `picocli-c * [#1055] Bugfix: The parser will no longer assign values that match an option name to options that take a parameter, unless the value is in quotes. Thanks to [waacc-gh](https://github.com/waacc-gh) for raising this. * [#1015] Bugfix: Parser improvement: varargs positional arguments no longer consume unmatched options unless `unmatchedOptionsArePositionalParams` is configured. Thanks to [Chris Smowton](https://github.com/smowton) for raising this. * [#1071] Bugfix: Usage help no longer renders options header when it is specified via `optionListHeading` when all options are hidden. +* [#1076] Bugfix: Don't generate Autocomplete for hidden commands. Thanks to [power721](https://github.com/power721) for raising this. * [#1081] Bugfix: `CommandLine.Help` constructor no longer calls overridable methods `addAllSubcommands` and `createDefaultParamLabelRenderer`. * [#1065] Bugfix: With a `List<>` option in `@ArgGroup`, group incorrectly appears twice in the synopsis. Thanks to [kap4lin](https://github.com/kap4lin) for raising this. * [#1067] Bugfix: `ParserSpec::initFrom` was not copying `useSimplifiedAtFiles`. diff --git a/src/main/java/picocli/AutoComplete.java b/src/main/java/picocli/AutoComplete.java index 026fb65ed..5de571bc0 100644 --- a/src/main/java/picocli/AutoComplete.java +++ b/src/main/java/picocli/AutoComplete.java @@ -605,7 +605,12 @@ private static String generateFunctionForCommand(String functionName, String com List argOptionFields = filter(commandSpec.options(), negate(new BooleanArgFilter())); String argOptionNames = optionNames(argOptionFields); - Set subCommands = commandLine.getSubcommands().keySet(); + Set subCommands = new LinkedHashSet(); + for (String sub : commandLine.getSubcommands().keySet()) { + if (!commandLine.getSubcommands().get(sub).getCommandSpec().usageMessage().hidden()) { + subCommands.add(sub); // #1076 Don't generate Autocomplete for hidden commands + } + } // If the command is a HelpCommand, append parent subcommands to the autocompletion list. if (commandLine.getParent() != null && commandLine.getCommand() instanceof HelpCommand) { subCommands = new LinkedHashSet(subCommands); diff --git a/src/test/java/picocli/AutoCompleteTest.java b/src/test/java/picocli/AutoCompleteTest.java index 770689b9c..0f71be7e7 100644 --- a/src/test/java/picocli/AutoCompleteTest.java +++ b/src/test/java/picocli/AutoCompleteTest.java @@ -1738,7 +1738,7 @@ private String getCompletionScriptTextWithHidden(String commandName) { "function _complete_%1$s() {\n" + " local cmds0=(help)\n" + "\n" + - " if CompWordsContainsArray \"${cmds0[@]}\"; then _picocli_CompletionDemo_help; return $?; fi\n" + + " if CompWordsContainsArray \"${cmds0[@]}\"; then _picocli_%1$s_help; return $?; fi\n" + "\n" + " # No subcommands were specified; generate completions for the top-level command.\n" + " _picocli_%1$s; return $?;\n" + @@ -1750,9 +1750,9 @@ private String getCompletionScriptTextWithHidden(String commandName) { " local curr_word=${COMP_WORDS[COMP_CWORD]}\n" + " local prev_word=${COMP_WORDS[COMP_CWORD-1]}\n" + "\n" + - " local commands=\"generate-completion help\"\n" + + " local commands=\"help\"\n" + // NOTE: no generate-completion: this command is hidden " local flag_opts=\"\"\n" + - " local arg_opts=\"--apples --bbb\"\n" + + " local arg_opts=\"--apples --bbb\"\n" + // NOTE: no --aaa: this option is hidden "\n" + " compopt +o default\n" + "\n" +