-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapplication.go
203 lines (166 loc) · 4.2 KB
/
application.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
package climax
import (
"fmt"
"io"
"os"
)
var (
outputDevice io.Writer = os.Stdout
errorDevice io.Writer = os.Stderr
)
// Application is a main CLI instance.
//
// By default, Climax provides its own implementation of version
// command, but it will use "version" command instead if you
// provide one.
type Application struct {
Name string // `go`
Brief string // `Go is a tool for managing Go source code.`
Version string // `1.5`
Commands []Command
Topics []Topic
Groups []Group
// Default is a default handler. It gets executed if there are
// no command line arguments (except the program name), when
// otherwise, by default, the help entry is being shown.
Default CmdHandler
ungroupedCmdsCount int
}
// Group connects a list of commands with a descriptive string.
//
// The Name is used in the help output to group related commands together.
type Group struct {
Name string
Commands []*Command
}
func (a *Application) println(stuff ...interface{}) {
fmt.Fprintln(outputDevice, stuff...)
}
func (a *Application) printf(format string, stuff ...interface{}) {
fmt.Fprintf(outputDevice, format, stuff...)
}
func (a *Application) printerr(err ...interface{}) {
for _, each := range err {
fmt.Fprintln(errorDevice, a.Name+":", each)
}
}
func (a *Application) commandByName(name string) *Command {
for i, command := range a.Commands {
if command.Name == name {
return &a.Commands[i]
}
}
return nil
}
func (a *Application) topicByName(name string) *Topic {
for i, topic := range a.Topics {
if topic.Name == name {
return &a.Topics[i]
}
}
return nil
}
func (a *Application) groupByName(name string) *Group {
for i, group := range a.Groups {
if group.Name == name {
return &a.Groups[i]
}
}
return nil
}
func (a *Application) isNameAvailable(name string) bool {
hypo, jypo := a.commandByName(name), a.topicByName(name)
if hypo != nil || jypo != nil {
return false
}
return true
}
// AddGroup adds a new empty, named group.
//
// Pass the returned group name to Command's Group member
// to make the command part of the group.
func (a *Application) AddGroup(name string) string {
a.Groups = append(a.Groups, Group{Name: name})
return name
}
// AddCommand does literally what its name says.
func (a *Application) AddCommand(command Command) {
a.Commands = append(a.Commands, command)
newCmd := &a.Commands[len(a.Commands)-1]
if newCmd.Group != "" {
group := a.groupByName(newCmd.Group)
if group == nil {
panic("group doesn't exist")
}
group.Commands = append(group.Commands, newCmd)
} else {
a.ungroupedCmdsCount++
}
}
// AddTopic does literally what its name says.
func (a *Application) AddTopic(topic Topic) {
a.Topics = append(a.Topics, topic)
}
// Run executes a CLI.
//
// Take a note, Run panics if len(os.Args) < 1
func (a *Application) Run() int {
if len(os.Args) < 1 {
panic("shell-provided arguments are not present")
}
arguments := os.Args[1:]
// $ program
// ^ no args
if len(arguments) == 0 {
if a.Default == nil {
a.println(a.globalHelp())
return 0
}
return a.Default(*newContext(a))
}
yankeeGoHome := func(errMsg string) {
a.printerr(errMsg)
os.Exit(1)
}
subcommandName := arguments[0]
subcommand := a.commandByName(subcommandName)
if subcommandName == "help" {
// $ program help
// ^ one argument
if len(arguments) <= 1 {
a.println(a.globalHelp())
return 0
}
command := a.commandByName(arguments[1])
if command != nil {
a.println(a.commandHelp(command))
return 0
}
topic := a.topicByName(arguments[1])
if topic != nil {
a.println(topic.Text)
return 0
}
yankeeGoHome("no such command or help topic")
}
if subcommandName == "version" {
if subcommand != nil {
return subcommand.Run(Context{})
}
a.printf("%s version %s\n", a.Name, a.Version)
return 0
}
if subcommand != nil {
context, err := a.parseContext(subcommand.Flags, arguments[1:])
if err != nil {
yankeeGoHome(err.Error())
}
return subcommand.Run(*context)
}
yankeeGoHome("unknown subcommand \"" + subcommandName + "\"\n")
return 1
}
// Log prints the message to stderrr (each argument takes a distinct line).
func (a *Application) Log(lines ...interface{}) {
a.printerr(lines...)
}