-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli.go
159 lines (133 loc) · 4.14 KB
/
cli.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
package clir
import (
"fmt"
"os"
)
// Cli - The main application object.
type Cli struct {
version string
rootCommand *Command
defaultCommand *Command
preRunCommand func(*Cli) error
postRunCommand func(*Cli) error
bannerFunction func(*Cli) string
errorHandler func(string, error) error
}
// Version - Get the Application version string.
func (c *Cli) Version() string {
return c.version
}
// Name - Get the Application Name
func (c *Cli) Name() string {
return c.rootCommand.name
}
// ShortDescription - Get the Application short description.
func (c *Cli) ShortDescription() string {
return c.rootCommand.shortdescription
}
// SetBannerFunction - Set the function that is called
// to get the banner string.
func (c *Cli) SetBannerFunction(fn func(*Cli) string) {
c.bannerFunction = fn
}
// SetErrorFunction - Set custom error message when undefined
// flags are used by the user. First argument is a string containing
// the command path used. Second argument is the undefined flag error.
func (c *Cli) SetErrorFunction(fn func(string, error) error) {
c.errorHandler = fn
}
// AddCommand - Adds a command to the application.
func (c *Cli) AddCommand(command *Command) {
c.rootCommand.AddCommand(command)
}
// PrintBanner - Prints the application banner!
func (c *Cli) PrintBanner() {
fmt.Println(c.bannerFunction(c))
fmt.Println("")
}
// PrintHelp - Prints the application's help.
func (c *Cli) PrintHelp() {
c.rootCommand.PrintHelp()
}
// Run - Runs the application with the given arguments.
func (c *Cli) Run(args ...string) error {
if c.preRunCommand != nil {
err := c.preRunCommand(c)
if err != nil {
return err
}
}
if len(args) == 0 {
args = os.Args[1:]
}
if err := c.rootCommand.run(args); err != nil {
return err
}
if c.postRunCommand != nil {
err := c.postRunCommand(c)
if err != nil {
return err
}
}
return nil
}
// DefaultCommand - Sets the given command as the command to run when
// no other commands given.
func (c *Cli) DefaultCommand(defaultCommand *Command) *Cli {
c.defaultCommand = defaultCommand
return c
}
// NewSubCommand - Creates a new SubCommand for the application.
func (c *Cli) NewSubCommand(name, description string) *Command {
return c.rootCommand.NewSubCommand(name, description)
}
// NewSubCommandInheritFlags - Creates a new SubCommand for the application, inherit flags from parent Command
func (c *Cli) NewSubCommandInheritFlags(name, description string) *Command {
return c.rootCommand.NewSubCommandInheritFlags(name, description)
}
// PreRun - Calls the given function before running the specific command.
func (c *Cli) PreRun(callback func(*Cli) error) {
c.preRunCommand = callback
}
// PostRun - Calls the given function after running the specific command.
func (c *Cli) PostRun(callback func(*Cli) error) {
c.postRunCommand = callback
}
// BoolFlag - Adds a boolean flag to the root command.
func (c *Cli) BoolFlag(name, description string, variable *bool) *Cli {
c.rootCommand.BoolFlag(name, description, variable)
return c
}
// StringFlag - Adds a string flag to the root command.
func (c *Cli) StringFlag(name, description string, variable *string) *Cli {
c.rootCommand.StringFlag(name, description, variable)
return c
}
// IntFlag - Adds an int flag to the root command.
func (c *Cli) IntFlag(name, description string, variable *int) *Cli {
c.rootCommand.IntFlag(name, description, variable)
return c
}
func (c *Cli) AddFlags(flags interface{}) *Cli {
c.rootCommand.AddFlags(flags)
return c
}
// Action - Define an action from this command.
func (c *Cli) Action(callback Action) *Cli {
c.rootCommand.Action(callback)
return c
}
// LongDescription - Sets the long description for the command.
func (c *Cli) LongDescription(longdescription string) *Cli {
c.rootCommand.LongDescription(longdescription)
return c
}
// OtherArgs - Returns the non-flag arguments passed to the cli.
// NOTE: This should only be called within the context of an action.
func (c *Cli) OtherArgs() []string {
return c.rootCommand.flags.Args()
}
func (c *Cli) NewSubCommandFunction(name string, description string, test interface{}) *Cli {
c.rootCommand.NewSubCommandFunction(name, description, test)
return c
}