@@ -2691,3 +2691,171 @@ func TestFixedCompletions(t *testing.T) {
2691
2691
t .Errorf ("expected: %q, got: %q" , expected , output )
2692
2692
}
2693
2693
}
2694
+
2695
+ func TestCompletionForGroupedFlags (t * testing.T ) {
2696
+ rootCmd := & Command {
2697
+ Use : "root" ,
2698
+ Run : emptyRun ,
2699
+ }
2700
+ childCmd := & Command {
2701
+ Use : "child" ,
2702
+ ValidArgsFunction : func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
2703
+ return []string {"subArg" }, ShellCompDirectiveNoFileComp
2704
+ },
2705
+ Run : emptyRun ,
2706
+ }
2707
+ rootCmd .AddCommand (childCmd )
2708
+
2709
+ rootCmd .PersistentFlags ().Int ("group1-1" , - 1 , "group1-1" )
2710
+ rootCmd .PersistentFlags ().String ("group1-2" , "" , "group1-2" )
2711
+
2712
+ childCmd .Flags ().Bool ("group1-3" , false , "group1-3" )
2713
+ childCmd .Flags ().Bool ("flag2" , false , "flag2" )
2714
+
2715
+ // Add flags to a group
2716
+ childCmd .MarkFlagsRequiredTogether ("group1-1" , "group1-2" , "group1-3" )
2717
+
2718
+ // Test that flags in a group are not suggested without the - prefix
2719
+ output , err := executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "" )
2720
+ if err != nil {
2721
+ t .Errorf ("Unexpected error: %v" , err )
2722
+ }
2723
+
2724
+ expected := strings .Join ([]string {
2725
+ "subArg" ,
2726
+ ":4" ,
2727
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2728
+
2729
+ if output != expected {
2730
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2731
+ }
2732
+
2733
+ // Test that flags in a group are suggested with the - prefix
2734
+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "-" )
2735
+ if err != nil {
2736
+ t .Errorf ("Unexpected error: %v" , err )
2737
+ }
2738
+
2739
+ expected = strings .Join ([]string {
2740
+ "--group1-1" ,
2741
+ "--group1-2" ,
2742
+ "--flag2" ,
2743
+ "--group1-3" ,
2744
+ ":4" ,
2745
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2746
+
2747
+ if output != expected {
2748
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2749
+ }
2750
+
2751
+ // Test that when a flag in a group is present, the other flags in the group are suggested
2752
+ // even without the - prefix
2753
+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "--group1-2" , "value" , "" )
2754
+ if err != nil {
2755
+ t .Errorf ("Unexpected error: %v" , err )
2756
+ }
2757
+
2758
+ expected = strings .Join ([]string {
2759
+ "--group1-1" ,
2760
+ "--group1-3" ,
2761
+ "subArg" ,
2762
+ ":4" ,
2763
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2764
+
2765
+ if output != expected {
2766
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2767
+ }
2768
+
2769
+ // Test that when all flags in a group are present, flags are not suggested without the - prefix
2770
+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" ,
2771
+ "--group1-1" , "8" ,
2772
+ "--group1-2" , "value2" ,
2773
+ "--group1-3" ,
2774
+ "" )
2775
+ if err != nil {
2776
+ t .Errorf ("Unexpected error: %v" , err )
2777
+ }
2778
+
2779
+ expected = strings .Join ([]string {
2780
+ "subArg" ,
2781
+ ":4" ,
2782
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2783
+
2784
+ if output != expected {
2785
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2786
+ }
2787
+ }
2788
+
2789
+ func TestCompletionForMutuallyExclusiveFlags (t * testing.T ) {
2790
+ rootCmd := & Command {
2791
+ Use : "root" ,
2792
+ Run : emptyRun ,
2793
+ }
2794
+ childCmd := & Command {
2795
+ Use : "child" ,
2796
+ ValidArgsFunction : func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
2797
+ return []string {"subArg" }, ShellCompDirectiveNoFileComp
2798
+ },
2799
+ Run : emptyRun ,
2800
+ }
2801
+ rootCmd .AddCommand (childCmd )
2802
+
2803
+ rootCmd .PersistentFlags ().IntSlice ("group1-1" , []int {1 }, "group1-1" )
2804
+ rootCmd .PersistentFlags ().String ("group1-2" , "" , "group1-2" )
2805
+
2806
+ childCmd .Flags ().Bool ("group1-3" , false , "group1-3" )
2807
+ childCmd .Flags ().Bool ("flag2" , false , "flag2" )
2808
+
2809
+ // Add flags to a group
2810
+ childCmd .MarkFlagsMutuallyExclusive ("group1-1" , "group1-2" , "group1-3" )
2811
+
2812
+ // Test that flags in a mutually exclusive group are not suggested without the - prefix
2813
+ output , err := executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "" )
2814
+ if err != nil {
2815
+ t .Errorf ("Unexpected error: %v" , err )
2816
+ }
2817
+
2818
+ expected := strings .Join ([]string {
2819
+ "subArg" ,
2820
+ ":4" ,
2821
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2822
+
2823
+ if output != expected {
2824
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2825
+ }
2826
+
2827
+ // Test that flags in a mutually exclusive group are suggested with the - prefix
2828
+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "-" )
2829
+ if err != nil {
2830
+ t .Errorf ("Unexpected error: %v" , err )
2831
+ }
2832
+
2833
+ expected = strings .Join ([]string {
2834
+ "--group1-1" ,
2835
+ "--group1-2" ,
2836
+ "--flag2" ,
2837
+ "--group1-3" ,
2838
+ ":4" ,
2839
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2840
+
2841
+ if output != expected {
2842
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2843
+ }
2844
+
2845
+ // Test that when a flag in a mutually exclusive group is present, the other flags in the group are
2846
+ // not suggested even with the - prefix
2847
+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "child" , "--group1-1" , "8" , "-" )
2848
+ if err != nil {
2849
+ t .Errorf ("Unexpected error: %v" , err )
2850
+ }
2851
+
2852
+ expected = strings .Join ([]string {
2853
+ "--group1-1" , // Should be repeated since it is a slice
2854
+ "--flag2" ,
2855
+ ":4" ,
2856
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2857
+
2858
+ if output != expected {
2859
+ t .Errorf ("expected: %q, got: %q" , expected , output )
2860
+ }
2861
+ }
0 commit comments