Skip to content

Conversation

jonathan-conder
Copy link

Fixes #1877. This is just a workaround, a real fix is described in the issue itself.

Tested with:

func TestCompleteArgsLenAtDash(t *testing.T) {
	compareArgsLenAtDash(t, nil)
	compareArgsLenAtDash(t, []string{"foo"})
	compareArgsLenAtDash(t, []string{"--"})
	compareArgsLenAtDash(t, []string{"--", "foo"})
	compareArgsLenAtDash(t, []string{"--", "foo", "bar"})
	compareArgsLenAtDash(t, []string{"foo", "--"})
	compareArgsLenAtDash(t, []string{"foo", "--", "bar"})
	compareArgsLenAtDash(t, []string{"foo", "bar", "--"})
	compareArgsLenAtDash(t, []string{"--", "--"})
	compareArgsLenAtDash(t, []string{"--", "--", "foo"})
	compareArgsLenAtDash(t, []string{"--", "foo", "--"})
	compareArgsLenAtDash(t, []string{"foo", "--", "--"})
	compareArgsLenAtDash(t, []string{"foo", "--", "--", "bar"})
	compareArgsLenAtDash(t, []string{"foo", "--", "bar", "--"})
	compareArgsLenAtDash(t, []string{"foo", "bar", "--", "--"})
}

func compareArgsLenAtDash(t *testing.T, args []string) {
	argsLenAtDash1 := 0
	argsLenAtDash2 := 0

	cmd := &Command{
		Run: func(cmd *Command, args []string) {
			argsLenAtDash1 = cmd.ArgsLenAtDash()
		},
		ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {
			argsLenAtDash2 = cmd.ArgsLenAtDash()
			return nil, ShellCompDirectiveDefault
		},
	}

	cmd.SetArgs(args)
	if err := cmd.Execute(); err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	cmd.SetArgs(append(append([]string{"__complete"}, args...), ""))
	if err := cmd.Execute(); err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	if argsLenAtDash1 != argsLenAtDash2 {
		t.Errorf("expected %v, got: %v", argsLenAtDash1, argsLenAtDash2)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom autocompletion after double dash (--)
1 participant