-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
377 lines (320 loc) · 9.97 KB
/
command.go
File metadata and controls
377 lines (320 loc) · 9.97 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package cli
import (
"context"
"fmt"
"strings"
"github.com/nyxstack/color"
)
// Command represents a command in the CLI application
type Command struct {
name string
description string
args []Argument
flags *FlagSet // All flags (automatically inherit to children)
subcommands map[string]*Command
parent *Command
action interface{}
hidden bool
// Lifecycle hooks
persistentPreRun func(context.Context, *Command) error
preRun func(context.Context, *Command) error
postRun func(context.Context, *Command) error
persistentPostRun func(context.Context, *Command) error
// Help system
helpEnabled bool
helpFlag string
helpShort string
}
// Getter methods (public API)
func (c *Command) GetName() string {
return c.name
}
func (c *Command) GetDescription() string {
return c.description
}
func (c *Command) GetParent() *Command {
return c.parent
}
func (c *Command) GetCommands() map[string]*Command {
return c.subcommands
}
func (c *Command) GetArgs() []Argument {
return c.args
}
// Cmd creates a new command with the given name
func Cmd(name string) *Command {
return &Command{
name: name,
flags: NewFlagSet(),
subcommands: make(map[string]*Command),
helpEnabled: true,
helpFlag: "help",
helpShort: "h",
}
}
// Root creates a new root command (convenience function)
func Root(name string) *Command {
return Cmd(name)
}
// Hidden marks the command as hidden from help output
func (c *Command) Hidden() *Command {
c.hidden = true
return c
}
// Show marks the command as visible in help output (default)
func (c *Command) Show() *Command {
c.hidden = false
return c
}
// IsHidden returns whether the command is hidden
func (c *Command) IsHidden() bool {
return c.hidden
}
// Description sets the command description
func (c *Command) Description(desc string) *Command {
c.description = desc
return c
}
// Flag adds a typed flag to the command using reflection
func (c *Command) Flag(ptr interface{}, name, shorthand string, defaultValue interface{}, usage string) *Command {
c.flags.Add(ptr, name, shorthand, defaultValue, usage)
return c
}
// FlagRequired adds a required flag to the command
func (c *Command) FlagRequired(ptr interface{}, name, shorthand string, defaultValue interface{}, usage string) *Command {
c.flags.Add(ptr, name, shorthand, defaultValue, usage)
// Mark the flag as required
if flag := c.flags.GetFlag(name); flag != nil {
flag.required = true
}
return c
}
// FlagHidden adds a hidden flag to the command
func (c *Command) FlagHidden(ptr interface{}, name, shorthand string, defaultValue interface{}, usage string) *Command {
c.flags.Add(ptr, name, shorthand, defaultValue, usage)
// Mark the flag as hidden
if flag := c.flags.GetFlag(name); flag != nil {
flag.hidden = true
}
return c
}
// Flags binds struct fields as flags using struct tags
func (c *Command) Flags(structPtr interface{}) *Command {
c.flags.BindStruct(structPtr)
return c
}
// Arg adds a positional argument to the command
func (c *Command) Arg(name, description string, required bool) *Command {
c.args = append(c.args, Argument{
Name: name,
Description: description,
Required: required,
})
return c
}
// Action sets the function to execute when this command is run
func (c *Command) Action(fn interface{}) *Command {
c.action = fn
return c
}
// PersistentPreRun sets a function to run before this command and all subcommands (inherits to children)
func (c *Command) PersistentPreRun(fn func(context.Context, *Command) error) *Command {
c.persistentPreRun = fn
return c
}
// PreRun sets a function to run before this command's action (command-specific)
func (c *Command) PreRun(fn func(context.Context, *Command) error) *Command {
c.preRun = fn
return c
}
// PostRun sets a function to run after this command's action (command-specific)
func (c *Command) PostRun(fn func(context.Context, *Command) error) *Command {
c.postRun = fn
return c
}
// PersistentPostRun sets a function to run after this command and all subcommands (inherits to children)
func (c *Command) PersistentPostRun(fn func(context.Context, *Command) error) *Command {
c.persistentPostRun = fn
return c
}
// AddCommand adds a subcommand
func (c *Command) AddCommand(cmd *Command) *Command {
cmd.parent = c
c.subcommands[cmd.name] = cmd
return c
}
// getAllFlags returns all flags including inherited from ancestors
func (c *Command) getAllFlags() []*Flag {
var allFlags []*Flag
seen := make(map[string]bool) // Track by primary name to avoid duplicates
// Collect flags from ancestors (root to current)
var ancestors []*Command
current := c
for current != nil {
ancestors = append([]*Command{current}, ancestors...)
current = current.parent
}
// Add flags from current to root (child flags shadow parent flags)
for i := len(ancestors) - 1; i >= 0; i-- {
cmd := ancestors[i]
for _, flag := range cmd.flags.GetFlags() {
primaryName := flag.PrimaryName()
if !seen[primaryName] {
allFlags = append(allFlags, flag)
seen[primaryName] = true
}
}
}
return allFlags
}
// getCommandPath returns the full command path from root to this command
func (c *Command) getCommandPath() string {
if c.parent == nil {
return c.name
}
return c.parent.getCommandPath() + " " + c.name
}
// DisableHelp disables the automatic help functionality
func (c *Command) DisableHelp() *Command {
c.helpEnabled = false
return c
}
// EnableHelp enables the automatic help functionality (default)
func (c *Command) EnableHelp() *Command {
c.helpEnabled = true
return c
}
// SetHelpFlag sets custom help flag names (default: "help" and "h")
func (c *Command) SetHelpFlag(long, short string) *Command {
c.helpFlag = long
c.helpShort = short
return c
}
// IsHelpEnabled returns whether help is enabled for this command
func (c *Command) IsHelpEnabled() bool {
return c.helpEnabled
}
// showHelp displays help information for the command
func (c *Command) showHelp() {
// Build the full command path for usage
commandPath := c.getCommandPath()
fmt.Printf("%s: %s", color.Bold+"Usage"+color.Reset, commandPath)
// Show subcommands indicator first
if len(c.subcommands) > 0 {
fmt.Printf(" %s", color.Cyan+"[command]"+color.Reset)
}
// Show arguments after subcommands
if len(c.args) > 0 {
argList := []string{}
for _, arg := range c.args {
if arg.Required {
argList = append(argList, fmt.Sprintf("<%s>", arg.Name))
} else {
argList = append(argList, fmt.Sprintf("[%s]", arg.Name))
}
}
if len(argList) > 0 {
fmt.Printf(" %s", color.Yellow+strings.Join(argList, " ")+color.Reset)
}
}
// Show flags indicator if any flags exist (local or inherited)
allFlags := c.getAllFlags()
if len(allFlags) > 0 || c.helpEnabled {
fmt.Printf(" %s", color.Dim+"[flags...]"+color.Reset)
}
fmt.Println()
if c.description != "" {
fmt.Printf("\n%s\n", c.description)
}
// Show arguments with descriptions
if len(c.args) > 0 {
fmt.Printf("\n%s:\n", color.Bold+"Arguments"+color.Reset)
for _, arg := range c.args {
required := ""
if arg.Required {
required = " " + color.Red + "(required)" + color.Reset
} else {
required = " " + color.Dim + "(optional)" + color.Reset
}
fmt.Printf(" %-15s %s%s\n", color.Yellow+arg.Name+color.Reset, arg.Description, required)
}
}
// Show all flags (local and inherited)
if len(allFlags) > 0 {
fmt.Printf("\n%s:\n", color.Bold+"Flags"+color.Reset)
// Track displayed flags by primary name to avoid duplicates
displayed := make(map[string]bool)
// Display flags (child command's local flags take precedence over inherited)
for _, flag := range allFlags {
primaryName := flag.PrimaryName()
if !displayed[primaryName] {
// Determine if this is an inherited flag
isLocal := false
for _, localFlag := range c.flags.GetFlags() {
if localFlag.PrimaryName() == primaryName {
isLocal = true
break
}
}
suffix := ""
if !isLocal && c.parent != nil {
suffix = color.Dim + " (inherited)" + color.Reset
}
c.displayFlag(flag, suffix)
displayed[primaryName] = true
}
}
// Add help flag if enabled
if c.helpEnabled {
helpNames := fmt.Sprintf("%s, %s", color.Green+fmt.Sprintf("-%s", c.helpShort)+color.Reset, color.Green+fmt.Sprintf("--%s", c.helpFlag)+color.Reset)
fmt.Printf(" %-30s %s\n", helpNames, "Show help information")
}
} else if c.helpEnabled {
// Show help flag even if no other flags
fmt.Printf("\n%s:\n", color.Bold+"Flags"+color.Reset)
helpNames := fmt.Sprintf("%s, %s", color.Green+fmt.Sprintf("-%s", c.helpShort)+color.Reset, color.Green+fmt.Sprintf("--%s", c.helpFlag)+color.Reset)
fmt.Printf(" %-30s %s\n", helpNames, "Show help information")
}
// Show subcommands
if len(c.subcommands) > 0 {
// Count visible subcommands
visibleCount := 0
for _, cmd := range c.subcommands {
if !cmd.IsHidden() {
visibleCount++
}
}
if visibleCount > 0 {
fmt.Printf("\n%s:\n", color.Bold+"Commands"+color.Reset)
for name, cmd := range c.subcommands {
if !cmd.IsHidden() {
fmt.Printf(" %-15s %s\n", color.Cyan+name+color.Reset, cmd.description)
}
}
// Show help command if enabled
if c.helpEnabled {
fmt.Printf("\n%s \"%s [command] %s\" %s\n",
color.Dim+"Use"+color.Reset,
c.name,
color.Green+"--"+c.helpFlag+color.Reset,
color.Dim+"for more information about a command."+color.Reset)
}
}
}
}
// displayFlag formats and displays a single flag
func (c *Command) displayFlag(flag *Flag, suffix string) {
names := color.Green + fmt.Sprintf("--%s", flag.PrimaryName()) + color.Reset
if flag.ShortName() != "" {
names = fmt.Sprintf("%s, %s", color.Green+fmt.Sprintf("-%s", flag.ShortName())+color.Reset, names)
}
defaultInfo := ""
if flag.GetDefault() != nil {
defaultInfo = color.Dim + fmt.Sprintf(" (default: %v)", flag.GetDefault()) + color.Reset
}
fmt.Printf(" %-30s %s%s%s\n", names, flag.GetUsage(), defaultInfo, suffix)
}
// ShowHelp displays help information (public API)
func (c *Command) ShowHelp() {
c.showHelp()
}