Skip to content
Myr

Remove __complete cmd for program without subcmds (#1563)

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

Fixes #1562 Programs that don't have sub-commands can accept any number of args. However, when doing shell completion for such programs, within the __complete code this very __complete command makes it that the program suddenly has a sub-command, and the call to Find() -> legacyArgs() will then return an error if there are more than one argument on the command-line being completed. To avoid this, we first remove the __complete command in such a case so as to get back to having no sub-commands.

authored by Marc Khouzam · committed by GitHub · parent 19c9c74 · Browse files · 2 files+56 added−1 removed
This commit

State

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

Trailers

  • Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Diff stats

2 files+56 added−1 removed

  • other:+56 added−1 removed

2 files+56 added−1 removed

modified:completions_test.go+45 added
2617 unmodified lines
2618 if output != expected {2619 t.Errorf("expected: %q, got: %q", expected, output)2620 }2621}26222623func TestCompleteWithRootAndLegacyArgs(t *testing.T) {2624 // Test a lonely root command which uses legacyArgs(). In such a case, the root2625 // command should accept any number of arguments and completion should behave accordingly.2626 rootCmd := &Command{2627 Use: "root",2628 Args: nil, // Args must be nil to trigger the legacyArgs() function2629 Run: emptyRun,2630 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {2631 return []string{"arg1", "arg2"}, ShellCompDirectiveNoFileComp2632 },2633 }26342635 // Make sure the first arg is completed2636 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")2637 if err != nil {2638 t.Errorf("Unexpected error: %v", err)2639 }26402641 expected := strings.Join([]string{2642 "arg1",2643 "arg2",2644 ":4",2645 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")26462647 if output != expected {2648 t.Errorf("expected: %q, got: %q", expected, output)2649 }26502651 // Make sure the completion of arguments continues2652 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "arg1", "")2653 if err != nil {2654 t.Errorf("Unexpected error: %v", err)2655 }26562657 expected = strings.Join([]string{2658 "arg1",2659 "arg2",2660 ":4",2661 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")26622663 if output != expected {2664 t.Errorf("expected: %q, got: %q", expected, output)2665 }2666}
modified:completions.go+11 added-1 removed
226 unmodified lines
227 // check if we need to traverse here to parse local flags on parent commands228 if c.Root().TraverseChildren {229 finalCmd, finalArgs, err = c.Root().Traverse(trimmedArgs)230 } else {231 // For Root commands that don't specify any value for their Args fields, when we call232 // Find(), if those Root commands don't have any sub-commands, they will accept arguments.233 // However, because we have added the __complete sub-command in the current code path, the234 // call to Find() -> legacyArgs() will return an error if there are any arguments.235 // To avoid this, we first remove the __complete command to get back to having no sub-commands.236 rootCmd := c.Root()237 if len(rootCmd.Commands()) == 1 {238 rootCmd.RemoveCommand(c)239 }240231 finalCmd, finalArgs, err = c.Root().Find(trimmedArgs)241 finalCmd, finalArgs, err = rootCmd.Find(trimmedArgs)242 }243 if err != nil {244 // Unable to find the real command. E.g., <program> someInvalidCmd <TAB>245 return c, []string{}, ShellCompDirectiveDefault, fmt.Errorf("Unable to find a command for arguments: %v", trimmedArgs)
566 unmodified lines