Skip to content
Myr

Custom completion handle multiple shorhand flags together (#1258)

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

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

authored by Paul Holzinger · committed by GitHub · parent 6d00909 · Browse files · 2 files+113 added−3 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: Paul Holzinger <paul.holzinger@web.de>

Diff stats

2 files+113 added−3 removed

  • other:+113 added−3 removed

2 files+113 added−3 removed

modified:completions_test.go+93 added
2199 unmodified lines
2200 t.Errorf("expected: %q, got: %q", expected, output)2201 }2202 }2203}22042205func TestMultipleShorthandFlagCompletion(t *testing.T) {2206 rootCmd := &Command{2207 Use: "root",2208 ValidArgs: []string{"foo", "bar"},2209 Run: emptyRun,2210 }2211 f := rootCmd.Flags()2212 f.BoolP("short", "s", false, "short flag 1")2213 f.BoolP("short2", "d", false, "short flag 2")2214 f.StringP("short3", "f", "", "short flag 3")2215 _ = rootCmd.RegisterFlagCompletionFunc("short3", func(*Command, []string, string) ([]string, ShellCompDirective) {2216 return []string{"works"}, ShellCompDirectiveNoFileComp2217 })22182219 // Test that a single shorthand flag works2220 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "")2221 if err != nil {2222 t.Errorf("Unexpected error: %v", err)2223 }22242225 expected := strings.Join([]string{2226 "foo",2227 "bar",2228 ":4",2229 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")22302231 if output != expected {2232 t.Errorf("expected: %q, got: %q", expected, output)2233 }22342235 // Test that multiple boolean shorthand flags work2236 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sd", "")2237 if err != nil {2238 t.Errorf("Unexpected error: %v", err)2239 }22402241 expected = strings.Join([]string{2242 "foo",2243 "bar",2244 ":4",2245 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")22462247 if output != expected {2248 t.Errorf("expected: %q, got: %q", expected, output)2249 }22502251 // Test that multiple boolean + string shorthand flags work2252 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf", "")2253 if err != nil {2254 t.Errorf("Unexpected error: %v", err)2255 }22562257 expected = strings.Join([]string{2258 "works",2259 ":4",2260 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")22612262 if output != expected {2263 t.Errorf("expected: %q, got: %q", expected, output)2264 }22652266 // Test that multiple boolean + string with equal sign shorthand flags work2267 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=")2268 if err != nil {2269 t.Errorf("Unexpected error: %v", err)2270 }22712272 expected = strings.Join([]string{2273 "works",2274 ":4",2275 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")22762277 if output != expected {2278 t.Errorf("expected: %q, got: %q", expected, output)2279 }22802281 // Test that multiple boolean + string with equal sign with value shorthand flags work2282 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=abc", "")2283 if err != nil {2284 t.Errorf("Unexpected error: %v", err)2285 }22862287 expected = strings.Join([]string{2288 "foo",2289 "bar",2290 ":4",2291 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")22922293 if output != expected {2294 t.Errorf("expected: %q, got: %q", expected, output)2295 }2296}
modified:completions.go+20 added-3 removed
467 unmodified lines
468 // requires the flag name to be complete469 if len(lastArg) > 0 && lastArg[0] == '-' {470 if index := strings.Index(lastArg, "="); index >= 0 {471 // Flag with an =472 flagName = strings.TrimLeft(lastArg[:index], "-")472 if strings.HasPrefix(lastArg[:index], "--") {473 // Flag has full name474 flagName = lastArg[2:index]475 } else {476 // Flag is shorthand477 // We have to get the last shorthand flag name478 // e.g. `-asd` => d to provide the correct completion479 // https://github.com/spf13/cobra/issues/1257480 flagName = lastArg[index-1 : index]481 }482 lastArg = lastArg[index+1:]483 flagWithEqual = true484 } else {485 // Normal flag completion
8 unmodified lines
494 // Only consider the case where the flag does not contain an =.495 // If the flag contains an = it means it has already been fully processed,496 // so we don't need to deal with it here.497 if index := strings.Index(prevArg, "="); index < 0 {489 flagName = strings.TrimLeft(prevArg, "-")490498 if strings.HasPrefix(prevArg, "--") {499 // Flag has full name500 flagName = prevArg[2:]501 } else {502 // Flag is shorthand503 // We have to get the last shorthand flag name504 // e.g. `-asd` => d to provide the correct completion505 // https://github.com/spf13/cobra/issues/1257506 flagName = prevArg[len(prevArg)-1:]507 }508 // Remove the uncompleted flag or else there could be an error created509 // for an invalid value for that flag510 trimmedArgs = args[:len(args)-1]511 }
234 unmodified lines