This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
86 lines (67 loc) · 2.15 KB
/
command.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cli
import (
"strings"
)
type commands map[string]*Command
type commandHandler func(c Command)
// A command for your program, this can either be a main command, regular
// command or a sub-command.
type Command struct {
parent *Command
cmds commands
handler commandHandler
// The name of the command that will be passed to the program for command
// invocation.
Name string
// The arguments passed to the individual command. Starting out this will
// be empty, and will be populated once the command's flags have been parsed
// from the programs input arguments.
Args args
// The flags that have been added to the command. Starting out this will be
// empty much like the command arguments. During parsing the command flags
// will be set accordingly, whether they be value flags or boolean flags.
Flags Flags
}
// Create a new command with the given name, and given command handler.
func (c *Command) Command(name string, handler commandHandler) *Command {
return addCommand(name, c, handler, c.cmds)
}
// Add a flag to the current command. If the given flag is a global program flag,
// then set it on each of the command's sub-commands.
func (c *Command) AddFlag(f *Flag) *Command {
if f.global {
for _, cmd := range c.cmds {
cmd.AddFlag(f)
}
}
c.Flags.expected[f.Name] = f
return c
}
// Get the full name of the current command. If the current command is a
// sub-command, then the command's name, and all grandparent command names will
// be concatenated into a hyphenated string.
func (c Command) FullName() string {
commands := make([]string, 0)
next := &c
for next != nil {
commands = append([]string{next.Name}, commands...)
next = next.parent
}
return strings.Join(commands, "-")
}
// Invoke the command's handler, and any flag handlers that are set on the flags
// on the command. If any of the flags on the command are exclusive then the
// command handle will not be invoked.
func (c Command) Run() {
shouldRun := true
for _, flags := range c.Flags.received {
flag := flags[0]
if flag.Handler != nil && flag.isSet {
flag.Handler(flag, c)
shouldRun = !flag.Exclusive
}
}
if shouldRun {
c.handler(c)
}
}