Merge pull request #37 from posener/fixes

Fixes
This commit is contained in:
Eyal Posener 2017-05-23 07:45:28 +03:00 committed by GitHub
commit 3e6cff0740
3 changed files with 18 additions and 7 deletions

View File

@ -6,6 +6,7 @@
package complete package complete
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -36,11 +37,21 @@ func New(name string, command Command) *Complete {
} }
} }
// Run get a command, get the typed arguments from environment // Run runs the completion and add installation flags beforehand.
// variable, and print out the complete options // 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 // returns success if the completion ran or if the cli matched
// any of the given flags, false otherwise // 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() line, ok := getLine()
if !ok { if !ok {
// make sure flags parsed, // make sure flags parsed,

View File

@ -19,7 +19,7 @@ func main() {
// create the complete command // create the complete command
cmp := complete.New( cmp := complete.New(
"self", "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, // 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 // and ran as a completion script or handled a flag that passed
// as argument, the Run method will return true, // as argument, the Run method will return true,
// in that case, our program have nothing to do and should return. // in that case, our program have nothing to do and should return.
if cmp.Run() { if cmp.Complete() {
return return
} }

View File

@ -78,7 +78,7 @@ func main() {
// build sub command has a flag '-cpus', which // build sub command has a flag '-cpus', which
// expects number of cpus after it. in that case // expects number of cpus after it. in that case
// anything could complete this flag. // 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 // define global flags of the 'run' main command
// those will show up also when a sub command was entered in the // those will show up also when a sub command was entered in the
// command line // command line
Flags: complete.Flags{ GlobalFlags: complete.Flags{
// a flag '-h' which does not expects anything after it // a flag '-h' which does not expects anything after it
"-h": complete.PredictNothing, "-h": complete.PredictNothing,