-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommander_go.go
More file actions
104 lines (88 loc) · 2.1 KB
/
Copy pathcommander_go.go
File metadata and controls
104 lines (88 loc) · 2.1 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
package commandergo
type Action func(ctx *Context)
type ActionE func(ctx *Context) error
type Command struct {
name string
version string
description string
parent *Command
_arguments Arguments
_options Options
_subCommands Commands
actionFn ActionE
customHelpFn ActionE
}
func New(name string) *Command {
c := &Command{
name: name,
parent: nil,
_arguments: Arguments{},
_options: Options{},
_subCommands: Commands{},
actionFn: nil,
customHelpFn: nil,
}
c.options("-h, --help", "display help for command", nil)
return c
}
func NewFromJson(jsonData string) *Command {
return newFromJson(jsonData)
}
func (c *Command) Name(name string) *Command {
c.name = name
return c
}
func (c *Command) CustomHelp(hp ActionE) *Command {
c.customHelpFn = hp
return c
}
func (c *Command) Version(version string) *Command {
c.version = version
c.options("-V, --version", "output the version number", nil)
return c
}
func (c *Command) Description(description string) *Command {
c.description = description
return c
}
func (c *Command) Arguments(name, desc string, defaultValue any) *Command {
return c.arguments(name, desc, defaultValue)
}
func (c *Command) Options(flag, desc string, defaultValue any) *Command {
return c.options(flag, desc, defaultValue)
}
func (c *Command) Command(nameAndArg, desc string) *Command {
return c.command(nameAndArg, desc)
}
func (c *Command) Child(name string) *Command {
cmd, ok := c._subCommands.get(name)
if ok {
return cmd
}
return nil
}
func (c *Command) Parent() *Command {
return c.parent
}
func (c *Command) Action(call Action) *Command {
if call == nil {
// 允许 Action(nil) 清空之前注册的 action。
c.actionFn = nil
return c
}
c.actionFn = func(ctx *Context) error {
call(ctx)
return nil
}
return c
}
func (c *Command) ActionE(call ActionE) *Command {
c.actionFn = call
return c
}
func (c *Command) Parse(args []string) error {
return c.parse(args, &parseOption{strict: false})
}
func (c *Command) ParseStrict(args []string) error {
return c.parse(args, &parseOption{strict: true})
}