-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
150 lines (127 loc) · 3.83 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
package kli
import (
"fmt"
"log"
"os"
)
// Cli - The main application object
type Cli struct {
version string
rootCommand *Command
defaultCommand *Command
preRunCommand func(*Cli) error
bannerFunction func(*Cli) string
}
// Action represents a function that gets called when the command is executed
type Action func() error
// NewCli - Creates a new Cli application object
func NewCli(name, description, version string) *Cli {
result := &Cli{
version: version,
bannerFunction: defaultBannerFunction,
}
result.rootCommand = NewCommand(name, description)
result.rootCommand.setApp(result)
result.rootCommand.setParentCommandPath("")
return result
}
// 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 is used to set the function that is called
// to get the banner string.
func (c *Cli) SetBannerFunction(fn func(*Cli) string) {
c.bannerFunction = fn
}
// Abort prints the given error and terminates the application
func (c *Cli) Abort(err error) {
log.Fatal(err)
os.Exit(1)
}
// 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:]
}
return c.rootCommand.run(args)
}
// 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)
}
// PreRun - Calls the given function before running the specific command
func (c *Cli) PreRun(callback func(*Cli) error) {
c.preRunCommand = 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
}
// 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()
}
// defaultBannerFunction prints a banner for the application.
// If version is a blank string, it is ignored.
func defaultBannerFunction(c *Cli) string {
version := ""
if len(c.Version()) > 0 {
version = " " + c.Version()
}
return fmt.Sprintf("%s%s - %s", c.Name(), version, c.ShortDescription())
}