summaryrefslogtreecommitdiff
path: root/command.go
blob: 74ab299adf6f0bc64bbf7c088c5085c80cbb1204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package complete

// Command is an object that can be used to create complete options for a go executable that does
// not have a good binding to the `Completer` interface, or to use a Go program as complete binary
// for another executable (see ./gocomplete as an example.)
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 map[string]*Command
	// Flags is a map of flags that the command accepts. The key is the flag name, and the value is
	// it's predictions. In a chain of sub commands, no duplicate flags should be defined.
	Flags map[string]Predictor
	// Args are extra arguments that the command accepts, those who are given without any flag
	// before. In any chain of sub commands, only one of them should predict positional arguments.
	Args Predictor
}

// Complete runs the completion of the described command.
func (c *Command) Complete(name string) {
	Complete(name, c)
}

func (c *Command) SubCmdList() []string {
	subs := make([]string, 0, len(c.Sub))
	for sub := range c.Sub {
		subs = append(subs, sub)
	}
	return subs
}

func (c *Command) SubCmdGet(cmd string) Completer {
	if c.Sub[cmd] == nil {
		return nil
	}
	return c.Sub[cmd]
}
func (c *Command) FlagList() []string {
	flags := make([]string, 0, len(c.Flags))
	for flag := range c.Flags {
		flags = append(flags, flag)
	}
	return flags
}

func (c *Command) FlagGet(flag string) Predictor {
	return PredictFunc(func(prefix string) (options []string) {
		f := c.Flags[flag]
		if f == nil {
			return nil
		}
		return f.Predict(prefix)
	})
}

func (c *Command) ArgsGet() Predictor {
	return PredictFunc(func(prefix string) (options []string) {
		if c.Args == nil {
			return nil
		}
		return c.Args.Predict(prefix)
	})
}