commit
3e6cff0740
17
complete.go
17
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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue