MCPcopy
hub / github.com/spf13/cobra / TestFlagNameCompletionInGo

Function TestFlagNameCompletionInGo

completions_test.go:493–581  ·  completions_test.go::TestFlagNameCompletionInGo
(t *testing.T)

Source from the content-addressed store, hash-verified

491}
492
493func TestFlagNameCompletionInGo(t *testing.T) {
494 rootCmd := &Command{
495 Use: "root",
496 Run: emptyRun,
497 }
498 childCmd := &Command{
499 Use: "childCmd",
500 Version: "1.2.3",
501 Run: emptyRun,
502 }
503 rootCmd.AddCommand(childCmd)
504
505 rootCmd.Flags().IntP("first", "f", -1, "first flag")
506 rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")
507 childCmd.Flags().String("subFlag", "", "sub flag")
508
509 // Test that flag names are not shown if the user has not given the '-' prefix
510 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")
511 if err != nil {
512 t.Errorf("Unexpected error: %v", err)
513 }
514
515 expected := strings.Join([]string{
516 "childCmd",
517 "completion",
518 "help",
519 ":4",
520 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
521
522 if output != expected {
523 t.Errorf("expected: %q, got: %q", expected, output)
524 }
525
526 // Test that flag names are completed
527 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")
528 if err != nil {
529 t.Errorf("Unexpected error: %v", err)
530 }
531
532 expected = strings.Join([]string{
533 "--first",
534 "-f",
535 "--help",
536 "-h",
537 "--second",
538 "-s",
539 ":4",
540 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
541
542 if output != expected {
543 t.Errorf("expected: %q, got: %q", expected, output)
544 }
545
546 // Test that flag names are completed when a prefix is given
547 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--f")
548 if err != nil {
549 t.Errorf("Unexpected error: %v", err)
550 }

Callers

nothing calls this directly

Calls 5

AddCommandMethod · 0.95
FlagsMethod · 0.95
PersistentFlagsMethod · 0.95
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected