Fish does not accept - or : in vars (#1122)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1121. This is for programs that may contain a : or - in their name.
authored by Marc Khouzam · committed by GitHub · parent 5d52907 · Browse files · 3 files·+88 added−46 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Diff stats
3 files+88 added−46 removed
- other:+88 added−46 removed
Provenance
- Commit
675ae5f- Tree
8370747- Author
Marc Khouzam <marc.khouzam@montreal.ca>- Committer
GitHub <noreply@github.com>- Parent
5d52907
3 files changed+88 added−46 removed
modified:custom_completions_test.go-33 removed
1405 unmodified lines1406 1407 check(t, output, ShellCompNoDescRequestCmd)1408 }1409 1410-func TestCompleteNoDesCmdInFishScript(t *testing.T) {1411- rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1412- child := &Command{1413- Use: "child",1414- ValidArgsFunction: validArgsFunc,1415- Run: emptyRun,1416- }1417- rootCmd.AddCommand(child)1418-1419- buf := new(bytes.Buffer)1420- rootCmd.GenFishCompletion(buf, false)1421- output := buf.String()1422-1423- check(t, output, ShellCompNoDescRequestCmd)1424-}1425-1426-func TestCompleteCmdInFishScript(t *testing.T) {1427- rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1428- child := &Command{1429- Use: "child",1430- ValidArgsFunction: validArgsFunc,1431- Run: emptyRun,1432- }1433- rootCmd.AddCommand(child)1434-1435- buf := new(bytes.Buffer)1436- rootCmd.GenFishCompletion(buf, true)1437- output := buf.String()1438-1439- check(t, output, ShellCompRequestCmd)1440- checkOmit(t, output, ShellCompNoDescRequestCmd)1441-}1442-1410 func TestCompleteNoDesCmdInZshScript(t *testing.T) {1411 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1412 child := &Command{1413 Use: "child",487 unmodified linesadded:fish_completions_test.go+69 added
1+package cobra2+3+import (4+ "bytes"5+ "testing"6+)7+8+func TestCompleteNoDesCmdInFishScript(t *testing.T) {9+ rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}10+ child := &Command{11+ Use: "child",12+ ValidArgsFunction: validArgsFunc,13+ Run: emptyRun,14+ }15+ rootCmd.AddCommand(child)16+17+ buf := new(bytes.Buffer)18+ rootCmd.GenFishCompletion(buf, false)19+ output := buf.String()20+21+ check(t, output, ShellCompNoDescRequestCmd)22+}23+24+func TestCompleteCmdInFishScript(t *testing.T) {25+ rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}26+ child := &Command{27+ Use: "child",28+ ValidArgsFunction: validArgsFunc,29+ Run: emptyRun,30+ }31+ rootCmd.AddCommand(child)32+33+ buf := new(bytes.Buffer)34+ rootCmd.GenFishCompletion(buf, true)35+ output := buf.String()36+37+ check(t, output, ShellCompRequestCmd)38+ checkOmit(t, output, ShellCompNoDescRequestCmd)39+}40+41+func TestProgWithDash(t *testing.T) {42+ rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun}43+ buf := new(bytes.Buffer)44+ rootCmd.GenFishCompletion(buf, false)45+ output := buf.String()46+47+ // Functions name should have replace the '-'48+ check(t, output, "__root_dash_perform_completion")49+ checkOmit(t, output, "__root-dash_perform_completion")50+51+ // The command name should not have replaced the '-'52+ check(t, output, "-c root-dash")53+ checkOmit(t, output, "-c root_dash")54+}55+56+func TestProgWithColon(t *testing.T) {57+ rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun}58+ buf := new(bytes.Buffer)59+ rootCmd.GenFishCompletion(buf, false)60+ output := buf.String()61+62+ // Functions name should have replace the ':'63+ check(t, output, "__root_colon_perform_completion")64+ checkOmit(t, output, "__root:colon_perform_completion")65+66+ // The command name should not have replaced the ':'67+ check(t, output, "-c root:colon")68+ checkOmit(t, output, "-c root_colon")69+}modified:fish_completions.go+19 added-13 removed
3 unmodified lines4 "bytes"5 "fmt"6 "io"7 "os"8+ "strings"9 )10 11 func genFishComp(buf *bytes.Buffer, name string, includeDesc bool) {12+ // Variables should not contain a '-' or ':' character13+ nameForVar := name14+ nameForVar = strings.Replace(nameForVar, "-", "_", -1)15+ nameForVar = strings.Replace(nameForVar, ":", "_", -1)16+17 compCmd := ShellCompRequestCmd18 if !includeDesc {19 compCmd = ShellCompNoDescRequestCmd20 }22 unmodified lines43 end44 __%[1]s_debug "emptyArg: $emptyArg"45 46 if not type -q "$args[1]"41- # This can happen when "complete --do-complete %[1]s" is called when running this script.47+ # This can happen when "complete --do-complete %[2]s" is called when running this script.48 __%[1]s_debug "Cannot find $args[1]. No completions."49 return50 end51 46- set requestComp "$args[1] %[2]s $args[2..-1] $emptyArg"52+ set requestComp "$args[1] %[3]s $args[2..-1] $emptyArg"53 __%[1]s_debug "Calling $requestComp"54 55 set results (eval $requestComp 2> /dev/null)56 set comps $results[1..-2]47 unmodified lines104 105 __%[1]s_debug "Completions are: $__%[1]s_comp_results"106 __%[1]s_debug "Directive is: $directive"107 102- set shellCompDirectiveError %[3]d103- set shellCompDirectiveNoSpace %[4]d104- set shellCompDirectiveNoFileComp %[5]d105- set shellCompDirectiveFilterFileExt %[6]d106- set shellCompDirectiveFilterDirs %[7]d108+ set shellCompDirectiveError %[4]d109+ set shellCompDirectiveNoSpace %[5]d110+ set shellCompDirectiveNoFileComp %[6]d111+ set shellCompDirectiveFilterFileExt %[7]d112+ set shellCompDirectiveFilterDirs %[8]d113 114 if test -z "$directive"115 set directive 0116 end46 unmodified lines163 # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves164 # so we can properly delete any completions provided by another script.165 # The space after the the program name is essential to trigger completion for the program166 # and not completion of the program name itself.161-complete --do-complete "%[1]s " &> /dev/null167+complete --do-complete "%[2]s " &> /dev/null168 169 # Remove any pre-existing completions for the program since we will be handling all of them.164-complete -c %[1]s -e170+complete -c %[2]s -e171 172 # The order in which the below two lines are defined is very important so that __%[1]s_prepare_completions173 # is called first. It is __%[1]s_prepare_completions that sets up the __%[1]s_comp_do_file_comp variable.174 #175 # This completion will be run second as complete commands are added FILO.176 # It triggers file completion choices when __%[1]s_comp_do_file_comp is set.171-complete -c %[1]s -n 'set --query __%[1]s_comp_do_file_comp'177+complete -c %[2]s -n 'set --query __%[1]s_comp_do_file_comp'178 179 # This completion will be run first as complete commands are added FILO.174-# The call to __%[1]s_prepare_completions will setup both __%[1]s_comp_results abd __%[1]s_comp_do_file_comp.180+# The call to __%[1]s_prepare_completions will setup both __%[1]s_comp_results and __%[1]s_comp_do_file_comp.181 # It provides the program's completion choices.176-complete -c %[1]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'182+complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'183 178-`, name, compCmd,184+`, nameForVar, name, compCmd,185 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,186 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))187 }188 18 unmodified lines