complete/command.go

86 lines
2.5 KiB
Go
Raw Normal View History

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
// It holds the data that enables auto completion of a given typed command line
// 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-06 14:06:49 -05:00
// Flags is a map of flags that the command accepts.
// The key is the flag name, and the value is it's prediction options.
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
// prediction options.
type Flags map[string]Predicate
2017-05-05 08:57:22 -05:00
// options returns all available complete options for the given command
// args are all except the last command line arguments relevant to the command
2017-05-07 11:53:55 -05:00
func (c *Command) options(args []string) (options []match.Matcher, only bool) {
2017-05-05 08:57:22 -05:00
// remove the first argument, which is the command name
args = args[1:]
last := last(args)
2017-05-05 08:57:22 -05:00
// if prev has something that needs to follow it,
// it is the most relevant completion
if predicate, ok := c.Flags[last]; ok && predicate != nil {
return predicate.predict(last), true
2017-05-05 08:57:22 -05:00
}
sub, options, only := c.searchSub(args)
if only {
return
}
// if no subcommand was entered in any of the args, add the
// subcommands as complete options.
if sub == "" {
options = append(options, c.subCommands()...)
}
// add global available complete options
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
}
// add additional expected argument of the command
options = append(options, c.Args.predict(last)...)
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-07 11:53:55 -05:00
func (c *Command) searchSub(args []string) (sub string, all []match.Matcher, only bool) {
2017-05-05 08:57:22 -05:00
for i, arg := range args {
if cmd, ok := c.Sub[arg]; ok {
sub = arg
all, only = cmd.options(args[i:])
return
}
}
return "", nil, false
}
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
}