@@ -1958,3 +1958,96 @@ func TestCompleteHelp(t *testing.T) {
19581958 t .Errorf ("expected: %q, got: %q" , expected , output )
19591959 }
19601960}
1961+
1962+ func TestMultipleShorthandFlagCompletion (t * testing.T ) {
1963+ rootCmd := & Command {
1964+ Use : "root" ,
1965+ ValidArgs : []string {"foo" , "bar" },
1966+ Run : emptyRun ,
1967+ }
1968+ f := rootCmd .Flags ()
1969+ f .BoolP ("short" , "s" , false , "short flag 1" )
1970+ f .BoolP ("short2" , "d" , false , "short flag 2" )
1971+ f .StringP ("short3" , "f" , "" , "short flag 3" )
1972+ rootCmd .RegisterFlagCompletionFunc ("short3" , func (* Command , []string , string ) ([]string , ShellCompDirective ) {
1973+ return []string {"works" }, ShellCompDirectiveNoFileComp
1974+ })
1975+
1976+ // Test that a single shorthand flag works
1977+ output , err := executeCommand (rootCmd , ShellCompNoDescRequestCmd , "-s" , "" )
1978+ if err != nil {
1979+ t .Errorf ("Unexpected error: %v" , err )
1980+ }
1981+
1982+ expected := strings .Join ([]string {
1983+ "foo" ,
1984+ "bar" ,
1985+ ":4" ,
1986+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
1987+
1988+ if output != expected {
1989+ t .Errorf ("expected: %q, got: %q" , expected , output )
1990+ }
1991+
1992+ // Test that multiple boolean shorthand flags work
1993+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "-sd" , "" )
1994+ if err != nil {
1995+ t .Errorf ("Unexpected error: %v" , err )
1996+ }
1997+
1998+ expected = strings .Join ([]string {
1999+ "foo" ,
2000+ "bar" ,
2001+ ":4" ,
2002+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2003+
2004+ if output != expected {
2005+ t .Errorf ("expected: %q, got: %q" , expected , output )
2006+ }
2007+
2008+ // Test that multiple boolean + string shorthand flags work
2009+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "-sdf" , "" )
2010+ if err != nil {
2011+ t .Errorf ("Unexpected error: %v" , err )
2012+ }
2013+
2014+ expected = strings .Join ([]string {
2015+ "works" ,
2016+ ":4" ,
2017+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2018+
2019+ if output != expected {
2020+ t .Errorf ("expected: %q, got: %q" , expected , output )
2021+ }
2022+
2023+ // Test that multiple boolean + string with equal sign shorthand flags work
2024+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "-sdf=" )
2025+ if err != nil {
2026+ t .Errorf ("Unexpected error: %v" , err )
2027+ }
2028+
2029+ expected = strings .Join ([]string {
2030+ "works" ,
2031+ ":4" ,
2032+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2033+
2034+ if output != expected {
2035+ t .Errorf ("expected: %q, got: %q" , expected , output )
2036+ }
2037+
2038+ // Test that multiple boolean + string with equal sign with value shorthand flags work
2039+ output , err = executeCommand (rootCmd , ShellCompNoDescRequestCmd , "-sdf=abc" , "" )
2040+ if err != nil {
2041+ t .Errorf ("Unexpected error: %v" , err )
2042+ }
2043+
2044+ expected = strings .Join ([]string {
2045+ "foo" ,
2046+ "bar" ,
2047+ ":4" ,
2048+ "Completion ended with directive: ShellCompDirectiveNoFileComp" , "" }, "\n " )
2049+
2050+ if output != expected {
2051+ t .Errorf ("expected: %q, got: %q" , expected , output )
2052+ }
2053+ }
0 commit comments