diff options
| author | Eyal Posener <[email protected]> | 2017-05-10 07:28:43 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-10 19:21:35 +0300 |
| commit | 9de57bdcf5246827e9b1a57c905203e2edf6edf4 (patch) | |
| tree | 139eb01e2305d3d43800095bc5db3fecd407a15d /run.go | |
| parent | 5db452a63f1b8ff0319f08986a4a04324647738f (diff) | |
Enable completion and executable be the same command
Fixes #6
Diffstat (limited to 'run.go')
| -rw-r--r-- | run.go | 75 |
1 files changed, 0 insertions, 75 deletions
@@ -1,75 +0,0 @@ -// Package complete provides a tool for bash writing bash completion in go. -// -// Writing bash completion scripts is a hard work. This package provides an easy way -// to create bash completion scripts for any command, and also an easy way to install/uninstall -// the completion of the command. -package complete - -import ( - "fmt" - "os" - "strings" - - "github.com/posener/complete/cmd" -) - -const ( - envComplete = "COMP_LINE" - envDebug = "COMP_DEBUG" -) - -// Run get a command, get the typed arguments from environment -// variable, and print out the complete options -// name is the name of command we want to auto complete. -// IMPORTANT: it must be the same name - if the auto complete -// completes the 'go' command, name must be equal to "go". -func Run(name string, c Command) { - args, ok := getLine() - if !ok { - cmd.Run(name) - return - } - Log("Completing args: %s", args) - - options := complete(c, args) - - Log("Completion: %s", options) - output(options) -} - -// complete get a command an command line arguments and returns -// matching completion options -func complete(c Command, args []string) (matching []string) { - options, _ := c.options(args[:len(args)-1]) - - // choose only matching options - l := last(args) - for _, option := range options { - if option.Match(l) { - matching = append(matching, option.String()) - } - } - return -} - -func getLine() ([]string, bool) { - line := os.Getenv(envComplete) - if line == "" { - return nil, false - } - return strings.Split(line, " "), true -} - -func last(args []string) (last string) { - if len(args) > 0 { - last = args[len(args)-1] - } - return -} - -func output(options []string) { - // stdout of program defines the complete options - for _, option := range options { - fmt.Println(option) - } -} |
