From 1a4c871bd8a0eb5774646fa3b60ed0be42d65689 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Tue, 23 May 2017 07:33:14 +0300 Subject: [PATCH 1/3] Fix installation Fixes #36 --- complete.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/complete.go b/complete.go index be0876e..1df6617 100644 --- a/complete.go +++ b/complete.go @@ -6,6 +6,7 @@ package complete import ( + "flag" "fmt" "os" "strings" @@ -36,11 +37,21 @@ func New(name string, command Command) *Complete { } } -// Run get a command, get the typed arguments from environment -// variable, and print out the complete options +// Run runs the completion and add installation flags beforehand. +// The flags are added to the main flag CommandLine variable. +func (c *Complete) Run() bool { + c.AddFlags(nil) + flag.Parse() + return c.Complete() +} + +// Complete a command from completion line in environment variable, +// and print out the complete options. // returns success if the completion ran or if the cli matched // any of the given flags, false otherwise -func (c *Complete) Run() bool { +// For installation: it assumes that flags were added and parsed before +// it was called. +func (c *Complete) Complete() bool { line, ok := getLine() if !ok { // make sure flags parsed, From 3636c19b38fba5bf6a11469227d68c1f6189c7f7 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Tue, 23 May 2017 07:33:38 +0300 Subject: [PATCH 2/3] Fix example/self Fixes #34 --- example/self/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/self/main.go b/example/self/main.go index ae4c2e4..9479e64 100644 --- a/example/self/main.go +++ b/example/self/main.go @@ -19,7 +19,7 @@ func main() { // create the complete command cmp := complete.New( "self", - complete.Command{Flags: complete.Flags{"name": complete.PredictAnything}}, + complete.Command{Flags: complete.Flags{"-name": complete.PredictAnything}}, ) // AddFlags adds the completion flags to the program flags, @@ -39,7 +39,7 @@ func main() { // and ran as a completion script or handled a flag that passed // as argument, the Run method will return true, // in that case, our program have nothing to do and should return. - if cmp.Run() { + if cmp.Complete() { return } From 17b9aed67cff5e3ed6032e82785d774eece5c7a1 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Tue, 23 May 2017 07:39:54 +0300 Subject: [PATCH 3/3] readme: fix example --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index e6c6bb8..74077e3 100644 --- a/readme.md +++ b/readme.md @@ -78,7 +78,7 @@ func main() { // build sub command has a flag '-cpus', which // expects number of cpus after it. in that case // anything could complete this flag. - "-cpus": complete.Anything, + "-cpus": complete.PredictAnything, }, }, }, @@ -94,7 +94,7 @@ func main() { // define global flags of the 'run' main command // those will show up also when a sub command was entered in the // command line - Flags: complete.Flags{ + GlobalFlags: complete.Flags{ // a flag '-h' which does not expects anything after it "-h": complete.PredictNothing,