diff options
| author | Eyal Posener <[email protected]> | 2017-05-11 18:49:59 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-11 18:50:12 +0300 |
| commit | a28594d28ea9a838f174641a411fd537c2c495b9 (patch) | |
| tree | cfd33c66822461300730fb1c527cfddb581f342b /complete.go | |
| parent | dd2171d085ef5957a1c5c0794d6007822e47849b (diff) | |
Add args struct
Diffstat (limited to 'complete.go')
| -rw-r--r-- | complete.go | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/complete.go b/complete.go index c91bf5f..2780e62 100644 --- a/complete.go +++ b/complete.go @@ -41,15 +41,17 @@ 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 := complete(c.Command, a) Log("Completion: %s", options) output(options) @@ -58,14 +60,12 @@ func (c *Complete) Run() bool { // 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) +func complete(c Command, a args) (matching []string) { + options, _ := c.predict(a) - // 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) { + Log("option %T, %s -> %t", option, option, option.Match(a.beingTyped)) + if option.Match(a.beingTyped) { matching = append(matching, option.String()) } } @@ -80,13 +80,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 |
