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

Function TestFlagNameCompletionInGoWithDesc

completions_test.go:583–672  ·  completions_test.go::TestFlagNameCompletionInGoWithDesc
(t *testing.T)

Source from the content-addressed store, hash-verified

581}
582
583func TestFlagNameCompletionInGoWithDesc(t *testing.T) {
584 rootCmd := &Command{
585 Use: "root",
586 Run: emptyRun,
587 }
588 childCmd := &Command{
589 Use: "childCmd",
590 Short: "first command",
591 Version: "1.2.3",
592 Run: emptyRun,
593 }
594 rootCmd.AddCommand(childCmd)
595
596 rootCmd.Flags().IntP("first", "f", -1, "first flag\nlonger description for flag")
597 rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")
598 childCmd.Flags().String("subFlag", "", "sub flag")
599
600 // Test that flag names are not shown if the user has not given the '-' prefix
601 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "")
602 if err != nil {
603 t.Errorf("Unexpected error: %v", err)
604 }
605
606 expected := strings.Join([]string{
607 "childCmd\tfirst command",
608 "completion\tGenerate the autocompletion script for the specified shell",
609 "help\tHelp about any command",
610 ":4",
611 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
612
613 if output != expected {
614 t.Errorf("expected: %q, got: %q", expected, output)
615 }
616
617 // Test that flag names are completed
618 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "-")
619 if err != nil {
620 t.Errorf("Unexpected error: %v", err)
621 }
622
623 expected = strings.Join([]string{
624 "--first\tfirst flag",
625 "-f\tfirst flag",
626 "--help\thelp for root",
627 "-h\thelp for root",
628 "--second\tsecond flag",
629 "-s\tsecond flag",
630 ":4",
631 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
632
633 if output != expected {
634 t.Errorf("expected: %q, got: %q", expected, output)
635 }
636
637 // Test that flag names are completed when a prefix is given
638 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--f")
639 if err != nil {
640 t.Errorf("Unexpected error: %v", err)

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