Add completion for help command (#1136)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Don't exclude 'help' from bash completions Fixes #1000. * Provide completion for the help command 1- Show 'help' as a possible completion 2- Provide completions for the help command itself Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
authored by Marc Khouzam · committed by GitHub · parent ed7b60e · Browse files · 4 files·+94 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Zaven Muradyan <voithos@google.com>
Diff stats
4 files+94 added−6 removed
- other:+94 added−6 removed
Provenance
- Commit
0431872- Tree
d8a7c31- Author
Marc Khouzam <marc.khouzam@montreal.ca>- Committer
GitHub <noreply@github.com>- Parent
ed7b60e
4 files changed+94 added−6 removed
modified:bash_completions.go+2 added-2 removed
387 unmodified lines388 389 func writeCommands(buf *bytes.Buffer, cmd *Command) {390 buf.WriteString(" commands=()\n")391 for _, c := range cmd.Commands() {392- if !c.IsAvailableCommand() || c == cmd.helpCommand {392+ if !c.IsAvailableCommand() && c != cmd.helpCommand {393 continue394 }395 buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))396 writeCmdAliases(buf, c)184 unmodified lines581 }582 583 func gen(buf *bytes.Buffer, cmd *Command) {584 for _, c := range cmd.Commands() {585- if !c.IsAvailableCommand() || c == cmd.helpCommand {585+ if !c.IsAvailableCommand() && c != cmd.helpCommand {586 continue587 }588 gen(buf, c)589 }51 unmodified linesmodified:command.go+19 added-1 removed
1054 unmodified lines1055 Use: "help [command]",1056 Short: "Help about any command",1057 Long: `Help provides help for any command in the application.1058 Simply type ` + c.Name() + ` help [path to command] for full details.`,1059-1059+ ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1060+ var completions []string1061+ cmd, _, e := c.Root().Find(args)1062+ if e != nil {1063+ return nil, ShellCompDirectiveNoFileComp1064+ }1065+ if cmd == nil {1066+ // Root help command.1067+ cmd = c.Root()1068+ }1069+ for _, subCmd := range cmd.Commands() {1070+ if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {1071+ if strings.HasPrefix(subCmd.Name(), toComplete) {1072+ completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))1073+ }1074+ }1075+ }1076+ return completions, ShellCompDirectiveNoFileComp1077+ },1078 Run: func(c *Command, args []string) {1079 cmd, _, e := c.Root().Find(args)1080 if cmd == nil || e != nil {1081 c.Printf("Unknown help topic %#q\n", args)575 unmodified linesmodified:custom_completions_test.go+68 added
627 unmodified lines628 if output != expected {629 t.Errorf("expected: %q, got: %q", expected, output)630 }631 }632+633+func TestCompleteHelp(t *testing.T) {634+ rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}635+ child1Cmd := &Command{636+ Use: "child1",637+ Run: emptyRun,638+ }639+ child2Cmd := &Command{640+ Use: "child2",641+ Run: emptyRun,642+ }643+ rootCmd.AddCommand(child1Cmd, child2Cmd)644+645+ child3Cmd := &Command{646+ Use: "child3",647+ Run: emptyRun,648+ }649+ child1Cmd.AddCommand(child3Cmd)650+651+ // Test that completion includes the help command652+ output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")653+ if err != nil {654+ t.Errorf("Unexpected error: %v", err)655+ }656+657+ expected := strings.Join([]string{658+ "child1",659+ "child2",660+ "help",661+ ":0",662+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")663+664+ if output != expected {665+ t.Errorf("expected: %q, got: %q", expected, output)666+ }667+668+ // Test sub-commands are completed on first level of help command669+ output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "")670+ if err != nil {671+ t.Errorf("Unexpected error: %v", err)672+ }673+674+ expected = strings.Join([]string{675+ "child1",676+ "child2",677+ "help", // "<program> help help" is a valid command, so should be completed678+ ":4",679+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")680+681+ if output != expected {682+ t.Errorf("expected: %q, got: %q", expected, output)683+ }684+685+ // Test sub-commands are completed on first level of help command686+ output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "child1", "")687+ if err != nil {688+ t.Errorf("Unexpected error: %v", err)689+ }690+691+ expected = strings.Join([]string{692+ "child3",693+ ":4",694+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")695+696+ if output != expected {697+ t.Errorf("expected: %q, got: %q", expected, output)698+ }699+}modified:custom_completions.go+5 added-3 removed
181 unmodified lines182 }183 }184 185 if flag == nil {186- // Complete subcommand names186+ // Complete subcommand names, including the help command187 for _, subCmd := range finalCmd.Commands() {188+ if subCmd.IsAvailableCommand() || subCmd == finalCmd.helpCommand {188- if subCmd.IsAvailableCommand() && strings.HasPrefix(subCmd.Name(), toComplete) {189- completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))189+ if strings.HasPrefix(subCmd.Name(), toComplete) {190+ completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))191+ }192 }193 }194 195 if len(finalCmd.ValidArgs) > 0 {191 unmodified lines