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 10:49:59 -05:00
|
|
|
// The key is the flag name, and the value is it's prediction predict.
|
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.
|
|
|
|
Args Predicate
|
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
|
|
|
|
|
|
|
|
// Flags is the type Flags of the Flags member, it maps a flag name to the flag
|
2017-05-11 10:49:59 -05:00
|
|
|
// prediction predict.
|
2017-05-06 14:06:49 -05:00
|
|
|
type Flags map[string]Predicate
|
|
|
|
|
2017-05-11 10:49:59 -05:00
|
|
|
// predict returns all available complete predict for the given command
|
|
|
|
// all are all except the last command line arguments relevant to the command
|
|
|
|
func (c *Command) predict(a args) (options []match.Matcher, 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 10:49:59 -05:00
|
|
|
if predicate, ok := c.Flags[a.lastCompleted]; ok && predicate != nil {
|
|
|
|
Log("Predicting according to flag %s", a.beingTyped)
|
|
|
|
return predicate.predict(a.beingTyped), 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 == "" {
|
|
|
|
options = append(options, c.subCommands()...)
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:49:59 -05:00
|
|
|
// add global available complete predict
|
2017-05-05 08:57:22 -05:00
|
|
|
for flag := range c.Flags {
|
2017-05-07 11:53:55 -05:00
|
|
|
options = append(options, match.Prefix(flag))
|
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 10:49:59 -05:00
|
|
|
options = append(options, c.Args.predict(a.beingTyped)...)
|
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 10:49:59 -05:00
|
|
|
func (c *Command) searchSub(a args) (sub string, all []match.Matcher, 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-06 14:06:49 -05:00
|
|
|
// suvCommands returns a list of matchers according to the sub command names
|
2017-05-07 11:53:55 -05:00
|
|
|
func (c *Command) subCommands() []match.Matcher {
|
|
|
|
subs := make([]match.Matcher, 0, len(c.Sub))
|
2017-05-05 08:57:22 -05:00
|
|
|
for sub := range c.Sub {
|
2017-05-07 11:53:55 -05:00
|
|
|
subs = append(subs, match.Prefix(sub))
|
2017-05-05 08:57:22 -05:00
|
|
|
}
|
|
|
|
return subs
|
|
|
|
}
|