DisableFlagParsing must disable flag completion (#1161)
When a command has set DisableFlagParsing=true, it means Cobra should not be handling flags for that command but should let the command handle them itself. In fact, Cobra normally won't have been told at all about flags for this command. Not knowing about the flags of the command also implies that Cobra cannot perform flag completion as it does not have the necessary info. This commit still tries to do flag name completion, but when DisableFlagParsing==true, it continues on so that ValidArgsFunction will be called; this allows the program to handle completion for its own flags when DisableFlagParsing==true. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
@ -2543,3 +2543,64 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) {
|
||||
t.Errorf("expected: %q, got: %q", expected, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteWithDisableFlagParsing(t *testing.T) {
|
||||
|
||||
flagValidArgs := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
||||
return []string{"--flag", "-f"}, ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
||||
childCmd := &Command{
|
||||
Use: "child",
|
||||
Run: emptyRun,
|
||||
DisableFlagParsing: true,
|
||||
ValidArgsFunction: flagValidArgs,
|
||||
}
|
||||
rootCmd.AddCommand(childCmd)
|
||||
|
||||
rootCmd.PersistentFlags().StringP("persistent", "p", "", "persistent flag")
|
||||
childCmd.Flags().StringP("nonPersistent", "n", "", "non-persistent flag")
|
||||
|
||||
// Test that when DisableFlagParsing==true, ValidArgsFunction is called to complete flag names,
|
||||
// after Cobra tried to complete the flags it knows about.
|
||||
childCmd.DisableFlagParsing = true
|
||||
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "-")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
expected := strings.Join([]string{
|
||||
"--persistent",
|
||||
"-p",
|
||||
"--nonPersistent",
|
||||
"-n",
|
||||
"--flag",
|
||||
"-f",
|
||||
":4",
|
||||
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("expected: %q, got: %q", expected, output)
|
||||
}
|
||||
|
||||
// Test that when DisableFlagParsing==false, Cobra completes the flags itself and ValidArgsFunction is not called
|
||||
childCmd.DisableFlagParsing = false
|
||||
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "-")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Cobra was not told of any flags, so it returns nothing
|
||||
expected = strings.Join([]string{
|
||||
"--persistent",
|
||||
"-p",
|
||||
"--nonPersistent",
|
||||
"-n",
|
||||
":4",
|
||||
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("expected: %q, got: %q", expected, output)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user