@@ -1749,6 +1749,228 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) {
1749
1749
}
1750
1750
}
1751
1751
1752
+ func TestFlagCompletionWithNotInterspersedArgs (t * testing.T ) {
1753
+ rootCmd := & Command {Use : "root" , Run : emptyRun }
1754
+ childCmd := & Command {
1755
+ Use : "child" ,
1756
+ Run : emptyRun ,
1757
+ ValidArgsFunction : func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
1758
+ return []string {"--validarg" , "test" }, ShellCompDirectiveDefault
1759
+ },
1760
+ }
1761
+ childCmd2 := & Command {
1762
+ Use : "child2" ,
1763
+ Run : emptyRun ,
1764
+ ValidArgs : []string {"arg1" , "arg2" },
1765
+ }
1766
+ rootCmd .AddCommand (childCmd , childCmd2 )
1767
+ childCmd .Flags ().Bool ("bool" , false , "test bool flag" )
1768
+ childCmd .Flags ().String ("string" , "" , "test string flag" )
1769
+ _ = childCmd .RegisterFlagCompletionFunc ("string" , func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
1770
+ return []string {"myval" }, ShellCompDirectiveDefault
1771
+ })
1772
+
1773
+ // Test flag completion with no argument
1774
+ output , err := executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" )
1775
+ if err != nil {
1776
+ t .Errorf ("Unexpected error: %v" , err )
1777
+ }
1778
+
1779
+ expected := strings .Join ([]string {
1780
+ "--bool\t test bool flag" ,
1781
+ "--string\t test string flag" ,
1782
+ ":4" ,
1783
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
1784
+
1785
+ if output != expected {
1786
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1787
+ }
1788
+
1789
+ // Test that no flags are completed after the -- arg
1790
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "-" )
1791
+ if err != nil {
1792
+ t .Errorf ("Unexpected error: %v" , err )
1793
+ }
1794
+
1795
+ expected = strings .Join ([]string {
1796
+ "--validarg" ,
1797
+ "test" ,
1798
+ ":0" ,
1799
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1800
+
1801
+ if output != expected {
1802
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1803
+ }
1804
+
1805
+ // Test that no flags are completed after the -- arg with a flag set
1806
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--bool" , "--" , "-" )
1807
+ if err != nil {
1808
+ t .Errorf ("Unexpected error: %v" , err )
1809
+ }
1810
+
1811
+ expected = strings .Join ([]string {
1812
+ "--validarg" ,
1813
+ "test" ,
1814
+ ":0" ,
1815
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1816
+
1817
+ if output != expected {
1818
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1819
+ }
1820
+
1821
+ // set Interspersed to false which means that no flags should be completed after the first arg
1822
+ childCmd .Flags ().SetInterspersed (false )
1823
+
1824
+ // Test that no flags are completed after the first arg
1825
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "arg" , "--" )
1826
+ if err != nil {
1827
+ t .Errorf ("Unexpected error: %v" , err )
1828
+ }
1829
+
1830
+ expected = strings .Join ([]string {
1831
+ "--validarg" ,
1832
+ "test" ,
1833
+ ":0" ,
1834
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1835
+
1836
+ if output != expected {
1837
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1838
+ }
1839
+
1840
+ // Test that no flags are completed after the fist arg with a flag set
1841
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--string" , "t" , "arg" , "--" )
1842
+ if err != nil {
1843
+ t .Errorf ("Unexpected error: %v" , err )
1844
+ }
1845
+
1846
+ expected = strings .Join ([]string {
1847
+ "--validarg" ,
1848
+ "test" ,
1849
+ ":0" ,
1850
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1851
+
1852
+ if output != expected {
1853
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1854
+ }
1855
+
1856
+ // Check that args are still completed after --
1857
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "" )
1858
+ if err != nil {
1859
+ t .Errorf ("Unexpected error: %v" , err )
1860
+ }
1861
+
1862
+ expected = strings .Join ([]string {
1863
+ "--validarg" ,
1864
+ "test" ,
1865
+ ":0" ,
1866
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1867
+
1868
+ if output != expected {
1869
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1870
+ }
1871
+
1872
+ // Check that args are still completed even if flagname with ValidArgsFunction exists
1873
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "--string" , "" )
1874
+ if err != nil {
1875
+ t .Errorf ("Unexpected error: %v" , err )
1876
+ }
1877
+
1878
+ expected = strings .Join ([]string {
1879
+ "--validarg" ,
1880
+ "test" ,
1881
+ ":0" ,
1882
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1883
+
1884
+ if output != expected {
1885
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1886
+ }
1887
+
1888
+ // Check that args are still completed even if flagname with ValidArgsFunction exists
1889
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child2" , "--" , "a" )
1890
+ if err != nil {
1891
+ t .Errorf ("Unexpected error: %v" , err )
1892
+ }
1893
+
1894
+ expected = strings .Join ([]string {
1895
+ "arg1" ,
1896
+ "arg2" ,
1897
+ ":4" ,
1898
+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
1899
+
1900
+ if output != expected {
1901
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1902
+ }
1903
+
1904
+ // Check that --validarg is not parsed as flag after --
1905
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "--validarg" , "" )
1906
+ if err != nil {
1907
+ t .Errorf ("Unexpected error: %v" , err )
1908
+ }
1909
+
1910
+ expected = strings .Join ([]string {
1911
+ "--validarg" ,
1912
+ "test" ,
1913
+ ":0" ,
1914
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1915
+
1916
+ if output != expected {
1917
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1918
+ }
1919
+
1920
+ // Check that --validarg is not parsed as flag after an arg
1921
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "arg" , "--validarg" , "" )
1922
+ if err != nil {
1923
+ t .Errorf ("Unexpected error: %v" , err )
1924
+ }
1925
+
1926
+ expected = strings .Join ([]string {
1927
+ "--validarg" ,
1928
+ "test" ,
1929
+ ":0" ,
1930
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1931
+
1932
+ if output != expected {
1933
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1934
+ }
1935
+
1936
+ // Check that --validarg is added to args for the ValidArgsFunction
1937
+ childCmd .ValidArgsFunction = func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
1938
+ return args , ShellCompDirectiveDefault
1939
+ }
1940
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "--validarg" , "" )
1941
+ if err != nil {
1942
+ t .Errorf ("Unexpected error: %v" , err )
1943
+ }
1944
+
1945
+ expected = strings .Join ([]string {
1946
+ "--validarg" ,
1947
+ ":0" ,
1948
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1949
+
1950
+ if output != expected {
1951
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1952
+ }
1953
+
1954
+ // Check that --validarg is added to args for the ValidArgsFunction and toComplete is also set correctly
1955
+ childCmd .ValidArgsFunction = func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
1956
+ return append (args , toComplete ), ShellCompDirectiveDefault
1957
+ }
1958
+ output , err = executeCommand (rootCmd , ShellCompRequestCmd , "child" , "--" , "--validarg" , "--toComp=ab" )
1959
+ if err != nil {
1960
+ t .Errorf ("Unexpected error: %v" , err )
1961
+ }
1962
+
1963
+ expected = strings .Join ([]string {
1964
+ "--validarg" ,
1965
+ "--toComp=ab" ,
1966
+ ":0" ,
1967
+ "Completion ended with directive: ShellCompDirectiveDefault" , "" }, "\n " )
1968
+
1969
+ if output != expected {
1970
+ t .Errorf ("expected: %q, got: %q" , expected , output )
1971
+ }
1972
+ }
1973
+
1752
1974
func TestFlagCompletionInGoWithDesc (t * testing.T ) {
1753
1975
rootCmd := & Command {
1754
1976
Use : "root" ,
0 commit comments