github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/misc/bash/go (about) 1 # install in /etc/bash_completion.d/ or your personal directory 2 3 complete -f -X '!*.8' 8l 4 complete -f -X '!*.6' 6l 5 complete -f -X '!*.5' 5l 6 complete -f -X '!*.go' 8g 6g 5g gofmt gccgo 7 8 _go_importpath() 9 { 10 echo "$(compgen -W "$(go list all) all std" -- "$1")" 11 } 12 13 _go() 14 { 15 # TODO: Only allow flags before other arguments. run already does 16 # this. 17 18 local cur=`_get_cword` 19 local prev="${COMP_WORDS[COMP_CWORD-1]}" 20 21 local cmd="${COMP_WORDS[1]}" 22 23 local cmds="build clean doc fix fmt get 24 install list run test tool version vet" 25 local addhelp="gopath importpath remote 26 testflag testfunc" 27 local other="help" 28 29 if [ "$COMP_CWORD" == 1 ]; then 30 for opt in $cmds; do 31 if [[ "$opt" == "$cmd" ]]; then 32 COMPREPLY=("$opt") 33 return 34 fi 35 done 36 fi 37 38 case "$cmd" in 39 'build') 40 case "$prev" in 41 '-o') 42 _filedir 43 ;; 44 '-p') 45 ;; 46 *) 47 if [[ "$cur" == -* ]]; then 48 COMPREPLY=($(compgen -W "-a -n -o -p -v -x" -- "$cur")) 49 else 50 local found=0 51 for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do 52 case "$i" in 53 0|1|"$COMP_CWORD") 54 continue 55 ;; 56 esac 57 local opt="${COMP_WORDS[i]}" 58 if [[ "$opt" != -* ]]; then 59 if [[ "$opt" == *.go && -f "$opt" ]]; then 60 found=1 61 break 62 else 63 found=2 64 break 65 fi 66 fi 67 done 68 case "$found" in 69 0) 70 _filedir go 71 COMPREPLY+=(`_go_importpath "$cur"`) 72 ;; 73 1) 74 _filedir go 75 ;; 76 2) 77 COMPREPLY=(`_go_importpath "$cur"`) 78 ;; 79 esac 80 fi 81 ;; 82 esac 83 ;; 84 'clean') 85 if [[ "$cur" == -* ]]; then 86 COMPREPLY=($(compgen -W "-i -r -n -x" -- "$cur")) 87 else 88 COMPREPLY=(`_go_importpath "$cur"`) 89 fi 90 ;; 91 'doc') 92 COMPREPLY=(`_go_importpath "$cur"`) 93 ;; 94 'fix') 95 COMPREPLY=(`_go_importpath "$cur"`) 96 ;; 97 'fmt') 98 COMPREPLY=(`_go_importpath "$cur"`) 99 ;; 100 'get') 101 case "$prev" in 102 '-p') 103 ;; 104 *) 105 if [[ "$cur" == -* ]]; then 106 COMPREPLY=($(compgen -W "-a -d -fix -n -p -u -v -x" -- "$cur")) 107 else 108 COMPREPLY=(`_go_importpath "$cur"`) 109 fi 110 ;; 111 esac 112 ;; 113 'install') 114 case "$prev" in 115 '-p') 116 ;; 117 *) 118 if [[ "$cur" == -* ]]; then 119 COMPREPLY=($(compgen -W "-a -n -p -v -x" -- "$cur")) 120 else 121 COMPREPLY=(`_go_importpath "$cur"`) 122 fi 123 ;; 124 esac 125 ;; 126 'list') 127 case "$prev" in 128 '-f') 129 ;; 130 *) 131 if [[ "$cur" == -* ]]; then 132 COMPREPLY=($(compgen -W "-e -f -json" -- "$cur")) 133 else 134 COMPREPLY=(`_go_importpath "$cur"`) 135 fi 136 ;; 137 esac 138 ;; 139 'run') 140 if [[ "$cur" == -* && "$prev" != *.go ]]; then 141 COMPREPLY=($(compgen -W "-a -n -x" -- "$cur")) 142 else 143 _filedir 144 fi 145 ;; 146 'test') # TODO: Support for testflags. 147 case "$prev" in 148 '-file') 149 _filedir go 150 ;; 151 '-p') 152 ;; 153 *) 154 if [[ "$cur" == -* ]]; then 155 COMPREPLY=($(compgen -W "-c -file -i -p -x" -- "$cur")) 156 else 157 COMPREPLY=(`_go_importpath "$cur"`) 158 fi 159 ;; 160 esac 161 ;; 162 'tool') 163 if [ "$COMP_CWORD" == 2 ]; then 164 COMPREPLY=($(compgen -W "$(go tool)" -- "$cur")) 165 else 166 case "${COMP_WORDS[2]}" in 167 [568]a) # TODO: Implement something. 168 #_go_tool_568a 169 ;; 170 [568]c) # TODO: Implement something. 171 #_go_tool_568c 172 ;; 173 [568]g) # TODO: Implement something. 174 #_go_tool_568g 175 ;; 176 [568]l) # TODO: Implement something. 177 #_go_tool_568l 178 ;; 179 'api') # TODO: Implement something. 180 #_go_tool_api 181 ;; 182 'cgo') # TODO: Implement something. 183 #_go_tool_cgo 184 ;; 185 'cov') # TODO: Implement something. 186 #_go_tool_cov 187 ;; 188 'dist') # TODO: Implement something. 189 #_go_tool_dist 190 ;; 191 'ebnflint') # TODO: Implement something. 192 #_go_tool_ebnflint 193 ;; 194 'fix') # TODO: Implement something. 195 #_go_tool_fix 196 ;; 197 'gotype') # TODO: Implement something. 198 #_go_tool_gotype 199 ;; 200 'nm') # TODO: Implement something. 201 #_go_tool_nm 202 ;; 203 'pack') # TODO: Implement something. 204 #_go_tool_pack 205 ;; 206 'pprof') # TODO: Implement something. 207 #_go_tool_pprof 208 ;; 209 'prof') # TODO: Implement something. 210 #_go_tool_prof 211 ;; 212 'vet') # TODO: Implement something. 213 #_go_tool_vet 214 ;; 215 'yacc') # TODO: Implement something. 216 #_go_tool_yacc 217 ;; 218 esac 219 if [[ "$cur" == -* ]]; then 220 COMPREPLY=($(compgen -W "${COMPREPLY[*]} -h" -- "$cur")) 221 fi 222 fi 223 ;; 224 'version') 225 ;; 226 'vet') 227 if [[ "$cur" == -* ]]; then 228 : 229 else 230 COMPREPLY=(`_go_importpath "$cur"`) 231 fi 232 ;; 233 'help') 234 if [ "$COMP_CWORD" == 2 ]; then 235 COMPREPLY=($(compgen -W "$cmds $addhelp" -- "$cur")) 236 fi 237 ;; 238 *) 239 if [ "$COMP_CWORD" == 1 ]; then 240 COMPREPLY=($(compgen -W "$cmds $other" -- "$cur")) 241 else 242 _filedir 243 fi 244 ;; 245 esac 246 } 247 248 complete $filenames -F _go go 249 250 # vim:ts=2 sw=2 et syn=sh