diff options
| author | Eyal Posener <[email protected]> | 2017-05-11 20:54:26 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-11 20:54:26 +0300 |
| commit | d3bbb859d52b45987e3cd2098e28423f32edd999 (patch) | |
| tree | 4265893d0c665ba0e763482a70c9044fa983a1ed /complete.go | |
| parent | dd2171d085ef5957a1c5c0794d6007822e47849b (diff) | |
| parent | ba23c350c73d2dfdf071c14c22152bcaf7e7fd7b (diff) | |
Merge pull request #12 from posener/improves
Enhance program structure and data structures
Diffstat (limited to 'complete.go')
| -rw-r--r-- | complete.go | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/complete.go b/complete.go index c91bf5f..be0876e 100644 --- a/complete.go +++ b/complete.go @@ -41,37 +41,23 @@ func New(name string, command Command) *Complete { // returns success if the completion ran or if the cli matched // any of the given flags, false otherwise func (c *Complete) Run() bool { - args, ok := getLine() + line, ok := getLine() if !ok { // make sure flags parsed, // in case they were not added in the main program return c.CLI.Run() } - Log("Completing args: %s", args) + Log("Completing line: %s", line) - options := complete(c.Command, args) + a := newArgs(line) + + options := c.Command.Predict(a) Log("Completion: %s", options) output(options) return true } -// 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) - - // choose only matching options - l := last(args) - for _, option := range options { - Log("option %T, %s -> %t", option, option, option.Match(l)) - if option.Match(l) { - matching = append(matching, option.String()) - } - } - return -} - func getLine() ([]string, bool) { line := os.Getenv(envComplete) if line == "" { @@ -80,13 +66,6 @@ func getLine() ([]string, bool) { 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) { Log("") // stdout of program defines the complete options |
