-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
231 lines (202 loc) · 5.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package kli
import (
"flag"
"fmt"
"os"
"strings"
)
// Command represents a command that may be run by the user
type Command struct {
name string
commandPath string
shortdescription string
longdescription string
subCommands []*Command
subCommandsMap map[string]*Command
longestSubcommand int
actionCallback Action
app *Cli
flags *flag.FlagSet
flagCount int
helpFlag bool
hidden bool
}
// NewCommand creates a new Command
// func NewCommand(name string, description string, app *Cli, parentCommandPath string) *Command {
func NewCommand(name string, description string) *Command {
result := &Command{
name: name,
shortdescription: description,
subCommandsMap: make(map[string]*Command),
hidden: false,
}
return result
}
func (c *Command) setParentCommandPath(parentCommandPath string) {
// Set up command path
if parentCommandPath != "" {
c.commandPath += parentCommandPath + " "
}
c.commandPath += c.name
// Set up flag set
c.flags = flag.NewFlagSet(c.commandPath, flag.ContinueOnError)
c.BoolFlag("help", "Get help on the '"+strings.ToLower(c.commandPath)+"' command.", &c.helpFlag)
}
func (c *Command) setApp(app *Cli) {
c.app = app
}
// parseFlags parses the given flags
func (c *Command) parseFlags(args []string) ([]string, error) {
if len(args) == 0 {
return args, nil
}
tmp := os.Stderr
os.Stderr = nil
err := c.flags.Parse(args)
os.Stderr = tmp
return c.flags.Args(), err
}
// Run - Runs the Command with the given arguments
func (c *Command) run(args []string) error {
// Parse flags
args, err := c.parseFlags(args)
if err != nil {
fmt.Printf("Error: %s\n\n", err.Error())
c.PrintHelp()
return err
}
// Help takes precedence
if c.helpFlag {
c.PrintHelp()
return nil
}
// Check for subcommand
if len(args) > 0 {
subcommand := c.subCommandsMap[args[0]]
if subcommand != nil {
return subcommand.run(args[1:])
}
}
// Do we have an action?
if c.actionCallback != nil {
return c.actionCallback()
}
// If we haven't specified a subcommand
// check for an app level default command
if c.app.defaultCommand != nil {
// Prevent recursion!
if c.app.defaultCommand != c {
// only run default command if no args passed
if len(args) == 0 {
return c.app.defaultCommand.run(args)
}
}
}
// Nothing left we can do
c.PrintHelp()
return nil
}
// Action - Define an action from this command
func (c *Command) Action(callback Action) *Command {
c.actionCallback = callback
return c
}
// PrintHelp - Output the help text for this command
func (c *Command) PrintHelp() {
if c.app != nil {
c.app.PrintBanner()
}
commandTitle := c.commandPath
if c.shortdescription != "" {
commandTitle += " - " + c.shortdescription
}
// Ignore root command
if c.commandPath != c.name {
fmt.Println(commandTitle)
}
if c.longdescription != "" {
fmt.Println(c.longdescription + "\n")
}
if len(c.subCommands) > 0 {
fmt.Println("Available commands:")
fmt.Println("")
for _, subcommand := range c.subCommands {
if subcommand.isHidden() {
continue
}
spacer := strings.Repeat(" ", 3+c.longestSubcommand-len(subcommand.name))
isDefault := ""
if subcommand.isDefaultCommand() {
isDefault = "[default]"
}
fmt.Printf(" %s%s%s %s\n", subcommand.name, spacer, subcommand.shortdescription, isDefault)
}
fmt.Println("")
}
if c.flagCount > 0 {
fmt.Print("Flags:\n\n")
c.flags.SetOutput(os.Stdout)
c.flags.PrintDefaults()
c.flags.SetOutput(os.Stderr)
}
fmt.Println()
}
// isDefaultCommand returns true if called on the default command
func (c *Command) isDefaultCommand() bool {
if c.app == nil {
return false
}
return c.app.defaultCommand == c
}
// isHidden returns true if the command is a hidden command
func (c *Command) isHidden() bool {
return c.hidden
}
// Hidden hides the command from the Help system
func (c *Command) Hidden() {
c.hidden = true
}
// NewSubCommand - Creates a new subcommand
func (c *Command) NewSubCommand(name, description string) *Command {
result := NewCommand(name, description)
c.AddCommand(result)
return result
}
// AddCommand - Adds a subcommand
func (c *Command) AddCommand(command *Command) {
command.setApp(c.app)
command.setParentCommandPath(c.commandPath)
name := command.name
c.subCommands = append(c.subCommands, command)
c.subCommandsMap[name] = command
if len(name) > c.longestSubcommand {
c.longestSubcommand = len(name)
}
}
// BoolFlag - Adds a boolean flag to the command
func (c *Command) BoolFlag(name, description string, variable *bool) *Command {
c.flags.BoolVar(variable, name, *variable, description)
c.flagCount++
return c
}
// StringFlag - Adds a string flag to the command
func (c *Command) StringFlag(name, description string, variable *string) *Command {
c.flags.StringVar(variable, name, *variable, description)
c.flagCount++
return c
}
// IntFlag - Adds an int flag to the command
func (c *Command) IntFlag(name, description string, variable *int) *Command {
c.flags.IntVar(variable, name, *variable, description)
c.flagCount++
return c
}
// LongDescription - Sets the long description for the command
func (c *Command) LongDescription(longdescription string) *Command {
c.longdescription = longdescription
return c
}
// OtherArgs - Returns the non-flag arguments passed to the subcommand. NOTE: This should only be called within the context of an action.
func (c *Command) OtherArgs() []string {
return c.flags.Args()
}