summaryrefslogtreecommitdiff
path: root/complete.go
diff options
context:
space:
mode:
Diffstat (limited to 'complete.go')
-rw-r--r--complete.go31
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