You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project is a Go library for building powerful and user-friendly command-line interfaces (CLIs). The library makes it easy to create and manage complex command structures, parse command-line arguments, and provide helpful error messages and usage information to the user.
4
4
5
5
---
6
+
6
7
-[Installation](#installation)
7
8
-[Features](#features)
8
9
-[Usage](#usage)
9
10
-[Default Usage](#default)
10
11
-[Subcommand Usage](#subcommands)
11
12
-[Flags Usage](#flags)
13
+
12
14
---
13
15
14
-
###Installation
16
+
## Installation
15
17
16
18
```bash
17
19
go get oss.nandlabs.io/golly/cli
18
20
```
19
21
20
-
###Features
22
+
## Features
21
23
22
-
* Easy to use API for building complex command structures
23
-
* Argument parsing and validation
24
-
* Automatically generates usage and help information
25
-
* Written in Go and follows best practices for Go programming
24
+
- Easy to use API for building complex command structures
25
+
- Argument parsing and validation
26
+
- Automatically generates usage and help information
27
+
- Written in Go and follows best practices for Go programming
26
28
27
29
### Usage
28
30
29
31
#### Default
32
+
30
33
```go
31
34
package main
32
35
33
36
import (
34
-
"fmt"
35
-
"log"
36
-
"os"
37
+
"fmt"
38
+
"log"
39
+
"os"
37
40
38
-
"oss.nandlabs.io/golly/cli"
41
+
"oss.nandlabs.io/golly/cli"
39
42
)
40
43
41
44
funcmain() {
42
-
app:= &cli.App{
43
-
Version: "v0.0.1",
44
-
Action: func(ctx *cli.Context) error {
45
-
fmt.Printf("Hello, Golang!")
46
-
returnnil
47
-
},
48
-
}
49
-
50
-
iferr:= app.Execute(os.Args); err != nil {
51
-
log.Fatal(err)
52
-
}
45
+
app:= cli.NewCLI()
46
+
47
+
gollyCmd:= &cli.Command{
48
+
Name: "welcome",
49
+
Description: "Welcome command",
50
+
Handler: func(ctx *cli.Context) error {
51
+
fmt.Println("welcome to golly!")
52
+
returnnil
53
+
},
54
+
}
55
+
56
+
app.AddCommand(gollyCmd)
57
+
58
+
iferr:= app.Execute(); err != nil {
59
+
log.Fatal(err)
60
+
}
53
61
}
54
62
```
55
63
56
64
CLI Command and Output
65
+
57
66
```shell
58
-
~ % go run main.go greet
59
-
Hello, Golang!
67
+
~ % go run main.go welcome
68
+
welcome to golly!
60
69
```
61
70
62
71
#### Subcommands
@@ -65,78 +74,67 @@ Hello, Golang!
65
74
package main
66
75
67
76
import (
68
-
"fmt"
69
-
"log"
70
-
"os"
77
+
"fmt"
78
+
"log"
79
+
"os"
71
80
72
-
"oss.nandlabs.io/golly/cli"
81
+
"oss.nandlabs.io/golly/cli"
73
82
)
74
83
75
84
funcmain() {
76
-
app:= &cli.App{
77
-
Version: "v0.0.1",
78
-
Action: func(ctx *cli.Context) error {
79
-
fmt.Printf("Hello, Golang!")
80
-
returnnil
81
-
},
82
-
Commands: []*cli.Command{
83
-
{
84
-
Name: "test",
85
-
Usage: "this is a test command",
86
-
Aliases: []string{"t"},
87
-
Action: func(ctx *cli.Context) error {
88
-
fmt.Println("hello from test command")
89
-
returnnil
90
-
},
91
-
},
92
-
{
93
-
Name: "run",
94
-
Usage: "time to run",
95
-
Aliases: []string{"r"},
96
-
Action: func(ctx *cli.Context) error {
97
-
fmt.Println("time to run away")
98
-
returnnil
99
-
},
100
-
Commands: []*cli.Command{
101
-
{
102
-
Name: "slow",
103
-
Usage: "run slow",
104
-
Action: func(ctx *cli.Context) error {
105
-
fmt.Println("time to run slow")
106
-
returnnil
107
-
},
108
-
},
109
-
{
110
-
Name: "fast",
111
-
Usage: "run fast",
112
-
Action: func(ctx *cli.Context) error {
113
-
fmt.Println("time to run fast")
114
-
returnnil
115
-
},
116
-
},
117
-
},
118
-
},
119
-
},
120
-
}
121
-
122
-
iferr:= app.Execute(os.Args); err != nil {
123
-
log.Fatal(err)
124
-
}
85
+
86
+
app:= cli.NewCLI()
87
+
88
+
welcomeCmd:= &cli.Command{
89
+
Name: "welcome",
90
+
Description: "Welcome to golly",
91
+
Handler: func(ctx *cli.Context) error {
92
+
fmt.Println("welcome to golly")
93
+
returnnil
94
+
},
95
+
SubCommands: map[string]*cli.Command{
96
+
"home": {
97
+
Name: "home",
98
+
Description: "welcome home",
99
+
Handler: func(ctx *cli.Context) error {
100
+
fmt.Println("welcome home")
101
+
returnnil
102
+
},
103
+
},
104
+
"office": {
105
+
Name: "level",
106
+
Description: "level of the skill",
107
+
Handler: func(ctx *cli.Context) error {
108
+
fmt.Println("welcome office")
109
+
returnnil
110
+
},
111
+
},
112
+
},
113
+
}
114
+
115
+
app.AddCommand(welcomeCmd)
116
+
117
+
iferr:= app.Execute(); err != nil {
118
+
fmt.Println("Error:", err)
119
+
}
125
120
}
126
121
```
127
122
128
123
CLI Commands and Output
124
+
129
125
```shell
130
-
~ % go run main.go test
131
-
hello from testcommand
126
+
~ % go run main.go welcome
127
+
welcome to golly
132
128
```
129
+
133
130
```shell
134
-
~ % go run main.go run
135
-
time to run away
131
+
~ % go run main.go welcome home
132
+
welcome home
136
133
```
134
+
137
135
```shell
138
-
~ % go run main.go run fast
139
-
time to run fast
136
+
~ % go run main.go welcome office
137
+
welcome office
140
138
```
141
139
142
140
#### Flags
@@ -145,112 +143,69 @@ time to run fast
145
143
package main
146
144
147
145
import (
148
-
"fmt"
149
-
"log"
150
-
"os"
151
-
152
-
"oss.nandlabs.io/golly/cli"
153
-
)
146
+
"fmt"
147
+
"log"
148
+
"os"
154
149
155
-
const (
156
-
ProjectDir = "pd"
157
-
ProfileFile = "pf"
150
+
"oss.nandlabs.io/golly/cli"
158
151
)
159
152
160
153
funcmain() {
161
-
app:= &cli.App{
162
-
Version: "v0.0.1",
163
-
Action: func(ctx *cli.Context) error {
164
-
fmt.Printf("Hello, Golang!\n")
165
-
fmt.Println(ctx.GetFlag(ProjectDir))
166
-
fmt.Println(ctx.GetFlag(ProfileFile))
167
-
returnnil
168
-
},
169
-
Commands: []*cli.Command{
170
-
{
171
-
Name: "test",
172
-
Usage: "this is a test command",
173
-
Aliases: []string{"t"},
174
-
Action: func(ctx *cli.Context) error {
175
-
fmt.Println("hello from test command")
176
-
fmt.Println(ctx.GetFlag(ProjectDir))
177
-
fmt.Println(ctx.GetFlag(ProfileFile))
178
-
returnnil
179
-
},
180
-
},
181
-
{
182
-
Name: "run",
183
-
Usage: "time to run",
184
-
Aliases: []string{"r"},
185
-
Action: func(ctx *cli.Context) error {
186
-
fmt.Println("time to run away")
187
-
fmt.Println(ctx.GetFlag(ProjectDir))
188
-
fmt.Println(ctx.GetFlag(ProfileFile))
189
-
returnnil
190
-
},
191
-
Commands: []*cli.Command{
192
-
{
193
-
Name: "slow",
194
-
Usage: "run slow",
195
-
Action: func(ctx *cli.Context) error {
196
-
fmt.Println("time to run slow")
197
-
fmt.Println(ctx.GetFlag(ProjectDir))
198
-
fmt.Println(ctx.GetFlag(ProfileFile))
199
-
returnnil
200
-
},
201
-
},
202
-
{
203
-
Name: "fast",
204
-
Usage: "run fast",
205
-
Action: func(ctx *cli.Context) error {
206
-
fmt.Println("time to run fast")
207
-
fmt.Println(ctx.GetFlag(ProjectDir))
208
-
fmt.Println(ctx.GetFlag(ProfileFile))
209
-
returnnil
210
-
},
211
-
},
212
-
},
213
-
},
214
-
},
215
-
// global app flags
216
-
Flags: []*cli.Flag{
217
-
{
218
-
Name: ProjectDir,
219
-
Aliases: []string{"pd"},
220
-
Default: "",
221
-
Usage: "Directory of the project to be built",
222
-
},
223
-
{
224
-
Name: ProfileFile,
225
-
Aliases: []string{"pf"},
226
-
Default: "",
227
-
Usage: "Profile file name to be used",
228
-
},
229
-
},
230
-
}
231
-
232
-
iferr:= app.Execute(os.Args); err != nil {
233
-
log.Fatal(err)
234
-
}
154
+
app:= cli.NewCLI()
155
+
156
+
server:= &cli.Command{
157
+
Name: "server",
158
+
Description: "Server command",
159
+
Handler: func(ctx *cli.Context) error {
160
+
region, _:= ctx.GetFlag("region")
161
+
fmt.Printf("IN REGION, %s\n", region)
162
+
returnnil
163
+
},
164
+
Flags: []cli.Flag{
165
+
{
166
+
Name: "region",
167
+
Aliases: []string{"r"},
168
+
Usage: "Provide region",
169
+
Default: "",
170
+
},
171
+
},
172
+
SubCommands: map[string]*cli.Command{
173
+
"create": {
174
+
Name: "create",
175
+
Description: "create",
176
+
Handler: func(ctx *cli.Context) error {
177
+
typ, _:= ctx.GetFlag("type")
178
+
fmt.Printf("SERVER TYPE %s\n", typ)
179
+
returnnil
180
+
},
181
+
Flags: []cli.Flag{
182
+
{
183
+
Name: "type",
184
+
Aliases: []string{"t"},
185
+
Usage: "server type",
186
+
Default: "",
187
+
},
188
+
},
189
+
},
190
+
},
191
+
}
192
+
193
+
app.AddCommand(server)
194
+
195
+
iferr:= app.Execute(); err != nil {
196
+
fmt.Println("Error:", err)
197
+
}
235
198
}
236
199
```
237
200
238
201
CLI Commands and Output
202
+
239
203
```shell
240
-
~ % go run main.go test -pd="test" -pf="dev"
241
-
Hello, Golang!
242
-
test
243
-
dev
204
+
~ % go run main.go server --region="us-east-1"
205
+
IN REGION, us-east-1
244
206
```
207
+
245
208
```shell
246
-
~ % go run main.go run -pd="test" -pf="dev"
247
-
time to run away
248
-
test
249
-
dev
209
+
~ % go run main.go server create --type="t3.medium"
0 commit comments