Skip to content

Commit 6746e67

Browse files
author
Paul Holzinger
committed
Custom completion handle multiple shorhand flags together
Flag definitions like `-asd` are not handled correctly by the custom completion logic. They should be treated as multiple flags. For details refer to #1257. Fixes #1257 Signed-off-by: Paul Holzinger <[email protected]>
1 parent 142dfb1 commit 6746e67

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

custom_completions.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
442442
if len(lastArg) > 0 && lastArg[0] == '-' {
443443
if index := strings.Index(lastArg, "="); index >= 0 {
444444
// Flag with an =
445-
flagName = strings.TrimLeft(lastArg[:index], "-")
445+
if strings.HasPrefix(lastArg[:index], "--") {
446+
flagName = lastArg[2:index]
447+
} else {
448+
flagName = lastArg[index-1 : index]
449+
}
446450
lastArg = lastArg[index+1:]
447451
flagWithEqual = true
448452
} else {
@@ -459,8 +463,11 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
459463
// If the flag contains an = it means it has already been fully processed,
460464
// so we don't need to deal with it here.
461465
if index := strings.Index(prevArg, "="); index < 0 {
462-
flagName = strings.TrimLeft(prevArg, "-")
463-
466+
if strings.HasPrefix(prevArg, "--") {
467+
flagName = prevArg[2:]
468+
} else {
469+
flagName = prevArg[len(prevArg)-1:]
470+
}
464471
// Remove the uncompleted flag or else there could be an error created
465472
// for an invalid value for that flag
466473
trimmedArgs = args[:len(args)-1]

custom_completions_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,3 +1958,96 @@ func TestCompleteHelp(t *testing.T) {
19581958
t.Errorf("expected: %q, got: %q", expected, output)
19591959
}
19601960
}
1961+
1962+
func TestMultipleShorthandFlagCompletion(t *testing.T) {
1963+
rootCmd := &Command{
1964+
Use: "root",
1965+
ValidArgs: []string{"foo", "bar"},
1966+
Run: emptyRun,
1967+
}
1968+
f := rootCmd.Flags()
1969+
f.BoolP("short", "s", false, "short flag 1")
1970+
f.BoolP("short2", "d", false, "short flag 2")
1971+
f.StringP("short3", "f", "", "short flag 3")
1972+
rootCmd.RegisterFlagCompletionFunc("short3", func(*Command, []string, string) ([]string, ShellCompDirective) {
1973+
return []string{"works"}, ShellCompDirectiveNoFileComp
1974+
})
1975+
1976+
// Test that a single shorthand flag works
1977+
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "")
1978+
if err != nil {
1979+
t.Errorf("Unexpected error: %v", err)
1980+
}
1981+
1982+
expected := strings.Join([]string{
1983+
"foo",
1984+
"bar",
1985+
":4",
1986+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
1987+
1988+
if output != expected {
1989+
t.Errorf("expected: %q, got: %q", expected, output)
1990+
}
1991+
1992+
// Test that multiple boolean shorthand flags work
1993+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sd", "")
1994+
if err != nil {
1995+
t.Errorf("Unexpected error: %v", err)
1996+
}
1997+
1998+
expected = strings.Join([]string{
1999+
"foo",
2000+
"bar",
2001+
":4",
2002+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2003+
2004+
if output != expected {
2005+
t.Errorf("expected: %q, got: %q", expected, output)
2006+
}
2007+
2008+
// Test that multiple boolean + string shorthand flags work
2009+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf", "")
2010+
if err != nil {
2011+
t.Errorf("Unexpected error: %v", err)
2012+
}
2013+
2014+
expected = strings.Join([]string{
2015+
"works",
2016+
":4",
2017+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2018+
2019+
if output != expected {
2020+
t.Errorf("expected: %q, got: %q", expected, output)
2021+
}
2022+
2023+
// Test that multiple boolean + string with equal sign shorthand flags work
2024+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=")
2025+
if err != nil {
2026+
t.Errorf("Unexpected error: %v", err)
2027+
}
2028+
2029+
expected = strings.Join([]string{
2030+
"works",
2031+
":4",
2032+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2033+
2034+
if output != expected {
2035+
t.Errorf("expected: %q, got: %q", expected, output)
2036+
}
2037+
2038+
// Test that multiple boolean + string with equal sign with value shorthand flags work
2039+
output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=abc", "")
2040+
if err != nil {
2041+
t.Errorf("Unexpected error: %v", err)
2042+
}
2043+
2044+
expected = strings.Join([]string{
2045+
"foo",
2046+
"bar",
2047+
":4",
2048+
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2049+
2050+
if output != expected {
2051+
t.Errorf("expected: %q, got: %q", expected, output)
2052+
}
2053+
}

0 commit comments

Comments
 (0)