2017-05-05 08:57:22 -05:00
|
|
|
package complete
|
|
|
|
|
2017-05-07 11:53:55 -05:00
|
|
|
import "github.com/posener/complete/match"
|
|
|
|
|
2017-05-06 14:06:49 -05:00
|
|
|
// Command represents a command line
|
2017-05-11 10:49:59 -05:00
|
|
|
// It holds the data that enables auto completion of command line
|
2017-05-06 14:06:49 -05:00
|
|
|
// Command can also be a sub command.
|
|
|
|
type Command struct {
|
|
|
|
// Sub is map of sub commands of the current command
|
|
|
|
// The key refer to the sub command name, and the value is it's
|
|
|
|
// Command descriptive struct.
|
|
|
|
Sub Commands
|
2017-05-05 10:06:31 -05:00
|
|
|
|
2017-05-06 14:06:49 -05:00
|
|
|
// Flags is a map of flags that the command accepts.
|
2017-05-11 12:28:31 -05:00
|
|
|
// The key is the flag name, and the value is it's predictions.
|
2017-05-05 10:06:31 -05:00
|
|
|
Flags Flags
|
2017-05-06 14:06:49 -05:00
|
|
|
|
|
|
|
// Args are extra arguments that the command accepts, those who are
|
|
|
|
// given without any flag before.
|
2017-05-11 12:28:31 -05:00
|
|
|
Args Predictor
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
|
2017-05-06 14:06:49 -05:00
|
|
|
// Commands is the type of Sub member, it maps a command name to a command struct
|
|
|
|
type Commands map[string]Command
|
|
|
|
|
2017-05-11 12:28:31 -05:00
|
|
|
// Flags is the type Flags of the Flags member, it maps a flag name to the flag predictions.
|
|
|
|
type Flags map[string]Predictor
|
2017-05-06 14:06:49 -05:00
|
|
|
|
2017-05-11 12:28:31 -05:00
|
|
|
// Predict returns all possible predictions for args according to the command struct
|
|
|
|
func (c *Command) Predict(a Args) (predictions []string) {
|
|
|
|
predictions, _ = c.predict(a)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) predict(a Args) (options []string, only bool) {
|
2017-05-05 08:57:22 -05:00
|
|
|
|
2017-05-10 16:38:24 -05:00
|
|
|
// if wordCompleted has something that needs to follow it,
|
2017-05-05 08:57:22 -05:00
|
|
|
// it is the most relevant completion
|
2017-05-11 12:28:31 -05:00
|
|
|
if predictor, ok := c.Flags[a.LastCompleted]; ok && predictor != nil {
|
|
|
|
Log("Predicting according to flag %s", a.Last)
|
|
|
|
return predictor.Predict(a), true
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
|
2017-05-11 10:49:59 -05:00
|
|
|
sub, options, only := c.searchSub(a)
|
2017-05-05 08:57:22 -05:00
|
|
|
if only {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:49:59 -05:00
|
|
|
// if no sub command was found, return a list of the sub commands
|
2017-05-05 08:57:22 -05:00
|
|
|
if sub == "" {
|
2017-05-11 12:28:31 -05:00
|
|
|
options = append(options, c.subCommands(a.Last)...)
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
|
2017-05-11 12:51:33 -05:00
|
|
|
// add global available complete options
|
2017-05-05 08:57:22 -05:00
|
|
|
for flag := range c.Flags {
|
2017-05-11 12:48:40 -05:00
|
|
|
if match.Prefix(flag, a.Last) {
|
|
|
|
options = append(options, flag)
|
2017-05-11 12:28:31 -05:00
|
|
|
}
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
|
2017-05-05 15:07:18 -05:00
|
|
|
// add additional expected argument of the command
|
2017-05-11 12:28:31 -05:00
|
|
|
if c.Args != nil {
|
|
|
|
options = append(options, c.Args.Predict(a)...)
|
|
|
|
}
|
2017-05-05 15:07:18 -05:00
|
|
|
|
2017-05-05 08:57:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-06 14:06:49 -05:00
|
|
|
// searchSub searches recursively within sub commands if the sub command appear
|
|
|
|
// in the on of the arguments.
|
2017-05-11 12:28:31 -05:00
|
|
|
func (c *Command) searchSub(a Args) (sub string, all []string, only bool) {
|
|
|
|
for i, arg := range a.Completed {
|
2017-05-05 08:57:22 -05:00
|
|
|
if cmd, ok := c.Sub[arg]; ok {
|
|
|
|
sub = arg
|
2017-05-11 10:49:59 -05:00
|
|
|
all, only = cmd.predict(a.from(i))
|
2017-05-05 08:57:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 10:49:59 -05:00
|
|
|
return
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
|
2017-05-11 12:28:31 -05:00
|
|
|
// subCommands returns a list of matching sub commands
|
|
|
|
func (c *Command) subCommands(last string) (prediction []string) {
|
2017-05-05 08:57:22 -05:00
|
|
|
for sub := range c.Sub {
|
2017-05-11 12:48:40 -05:00
|
|
|
if match.Prefix(sub, last) {
|
|
|
|
prediction = append(prediction, sub)
|
2017-05-11 12:28:31 -05:00
|
|
|
}
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
2017-05-11 12:28:31 -05:00
|
|
|
return
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|