1/* 2Package complete provides a tool for bash writing bash completion in go, and bash completion for the go command line. 3 4Writing bash completion scripts is a hard work. This package provides an easy way 5to create bash completion scripts for any command, and also an easy way to install/uninstall 6the completion of the command. 7 8Go Command Bash Completion 9 10In ./cmd/gocomplete there is an example for bash completion for the `go` command line. 11 12This is an example that uses the `complete` package on the `go` command - the `complete` package 13can also be used to implement any completions, see #usage. 14 15Install 16 171. Type in your shell: 18 19 go get -u github.com/posener/complete/gocomplete 20 gocomplete -install 21 222. Restart your shell 23 24Uninstall by `gocomplete -uninstall` 25 26Features 27 28- Complete `go` command, including sub commands and all flags. 29- Complete packages names or `.go` files when necessary. 30- Complete test names after `-run` flag. 31 32Complete package 33 34Supported shells: 35 36- [x] bash 37- [x] zsh 38- [x] fish 39 40Usage 41 42Assuming you have program called `run` and you want to have bash completion 43for it, meaning, if you type `run` then space, then press the `Tab` key, 44the shell will suggest relevant complete options. 45 46In that case, we will create a program called `runcomplete`, a go program, 47with a `func main()` and so, that will make the completion of the `run` 48program. Once the `runcomplete` will be in a binary form, we could 49`runcomplete -install` and that will add to our shell all the bash completion 50options for `run`. 51 52So here it is: 53 54 import "github.com/posener/complete" 55 56 func main() { 57 58 // create a Command object, that represents the command we want 59 // to complete. 60 run := complete.Command{ 61 62 // Sub defines a list of sub commands of the program, 63 // this is recursive, since every command is of type command also. 64 Sub: complete.Commands{ 65 66 // add a build sub command 67 "build": complete.Command { 68 69 // define flags of the build sub command 70 Flags: complete.Flags{ 71 // build sub command has a flag '-cpus', which 72 // expects number of cpus after it. in that case 73 // anything could complete this flag. 74 "-cpus": complete.PredictAnything, 75 }, 76 }, 77 }, 78 79 // define flags of the 'run' main command 80 Flags: complete.Flags{ 81 // a flag -o, which expects a file ending with .out after 82 // it, the tab completion will auto complete for files matching 83 // the given pattern. 84 "-o": complete.PredictFiles("*.out"), 85 }, 86 87 // define global flags of the 'run' main command 88 // those will show up also when a sub command was entered in the 89 // command line 90 GlobalFlags: complete.Flags{ 91 92 // a flag '-h' which does not expects anything after it 93 "-h": complete.PredictNothing, 94 }, 95 } 96 97 // run the command completion, as part of the main() function. 98 // this triggers the autocompletion when needed. 99 // name must be exactly as the binary that we want to complete. 100 complete.New("run", run).Run() 101 } 102 103Self completing program 104 105In case that the program that we want to complete is written in go we 106can make it self completing. 107Here is an example: ./example/self/main.go . 108 109*/ 110package complete 111