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

Function TestMultipleShorthandFlagCompletion

completions_test.go:2694–2785  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2692}
2693
2694func TestMultipleShorthandFlagCompletion(t *testing.T) {
2695 rootCmd := &Command{
2696 Use: "root",
2697 ValidArgs: []string{"foo", "bar"},
2698 Run: emptyRun,
2699 }
2700 f := rootCmd.Flags()
2701 f.BoolP("short", "s", false, "short flag 1")
2702 f.BoolP("short2", "d", false, "short flag 2")
2703 f.StringP("short3", "f", "", "short flag 3")
2704 _ = rootCmd.RegisterFlagCompletionFunc("short3", func(*Command, []string, string) ([]string, ShellCompDirective) {
2705 return []string{"works"}, ShellCompDirectiveNoFileComp
2706 })
2707
2708 // Test that a single shorthand flag works
2709 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "")
2710 if err != nil {
2711 t.Errorf("Unexpected error: %v", err)
2712 }
2713
2714 expected := strings.Join([]string{
2715 "foo",
2716 "bar",
2717 ":4",
2718 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2719
2720 if output != expected {
2721 t.Errorf("expected: %q, got: %q", expected, output)
2722 }
2723
2724 // Test that multiple boolean shorthand flags work
2725 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sd", "")
2726 if err != nil {
2727 t.Errorf("Unexpected error: %v", err)
2728 }
2729
2730 expected = strings.Join([]string{
2731 "foo",
2732 "bar",
2733 ":4",
2734 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2735
2736 if output != expected {
2737 t.Errorf("expected: %q, got: %q", expected, output)
2738 }
2739
2740 // Test that multiple boolean + string shorthand flags work
2741 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf", "")
2742 if err != nil {
2743 t.Errorf("Unexpected error: %v", err)
2744 }
2745
2746 expected = strings.Join([]string{
2747 "works",
2748 ":4",
2749 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
2750
2751 if output != expected {

Callers

nothing calls this directly

Calls 3

FlagsMethod · 0.95
executeCommandFunction · 0.85

Tested by

no test coverage detected