Skip to content

Commit 40be6a3

Browse files
authored
Bug/#99 (#100)
Closes #99
1 parent 594cd5b commit 40be6a3

13 files changed

+729
-885
lines changed

cli/README.md

+135-180
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
1-
## cli
1+
# cli
22

33
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.
44

55
---
6+
67
- [Installation](#installation)
78
- [Features](#features)
89
- [Usage](#usage)
910
- [Default Usage](#default)
1011
- [Subcommand Usage](#subcommands)
1112
- [Flags Usage](#flags)
13+
1214
---
1315

14-
### Installation
16+
## Installation
1517

1618
```bash
1719
go get oss.nandlabs.io/golly/cli
1820
```
1921

20-
### Features
22+
## Features
2123

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
2628

2729
### Usage
2830

2931
#### Default
32+
3033
```go
3134
package main
3235

3336
import (
34-
"fmt"
35-
"log"
36-
"os"
37+
"fmt"
38+
"log"
39+
"os"
3740

38-
"oss.nandlabs.io/golly/cli"
41+
"oss.nandlabs.io/golly/cli"
3942
)
4043

4144
func main() {
42-
app := &cli.App{
43-
Version: "v0.0.1",
44-
Action: func(ctx *cli.Context) error {
45-
fmt.Printf("Hello, Golang!")
46-
return nil
47-
},
48-
}
49-
50-
if err := 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+
return nil
53+
},
54+
}
55+
56+
app.AddCommand(gollyCmd)
57+
58+
if err := app.Execute(); err != nil {
59+
log.Fatal(err)
60+
}
5361
}
5462
```
5563

5664
CLI Command and Output
65+
5766
```shell
58-
~ % go run main.go greet
59-
Hello, Golang!
67+
~ % go run main.go welcome
68+
welcome to golly!
6069
```
6170

6271
#### Subcommands
@@ -65,78 +74,67 @@ Hello, Golang!
6574
package main
6675

6776
import (
68-
"fmt"
69-
"log"
70-
"os"
77+
"fmt"
78+
"log"
79+
"os"
7180

72-
"oss.nandlabs.io/golly/cli"
81+
"oss.nandlabs.io/golly/cli"
7382
)
7483

7584
func main() {
76-
app := &cli.App{
77-
Version: "v0.0.1",
78-
Action: func(ctx *cli.Context) error {
79-
fmt.Printf("Hello, Golang!")
80-
return nil
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-
return nil
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-
return nil
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-
return nil
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-
return nil
115-
},
116-
},
117-
},
118-
},
119-
},
120-
}
121-
122-
if err := 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+
return nil
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+
return nil
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+
return nil
110+
},
111+
},
112+
},
113+
}
114+
115+
app.AddCommand(welcomeCmd)
116+
117+
if err := app.Execute(); err != nil {
118+
fmt.Println("Error:", err)
119+
}
125120
}
126121
```
127122

128123
CLI Commands and Output
124+
129125
```shell
130-
~ % go run main.go test
131-
hello from test command
126+
~ % go run main.go welcome
127+
welcome to golly
132128
```
129+
133130
```shell
134-
~ % go run main.go run
135-
time to run away
131+
~ % go run main.go welcome home
132+
welcome home
136133
```
134+
137135
```shell
138-
~ % go run main.go run fast
139-
time to run fast
136+
~ % go run main.go welcome office
137+
welcome office
140138
```
141139

142140
#### Flags
@@ -145,112 +143,69 @@ time to run fast
145143
package main
146144

147145
import (
148-
"fmt"
149-
"log"
150-
"os"
151-
152-
"oss.nandlabs.io/golly/cli"
153-
)
146+
"fmt"
147+
"log"
148+
"os"
154149

155-
const (
156-
ProjectDir = "pd"
157-
ProfileFile = "pf"
150+
"oss.nandlabs.io/golly/cli"
158151
)
159152

160153
func main() {
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-
return nil
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-
return nil
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-
return nil
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-
return nil
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-
return nil
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-
if err := 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+
return nil
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+
return nil
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+
if err := app.Execute(); err != nil {
196+
fmt.Println("Error:", err)
197+
}
235198
}
236199
```
237200

238201
CLI Commands and Output
202+
239203
```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
244206
```
207+
245208
```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"
210+
SERVER TYPE t3.medium
250211
```
251-
```shell
252-
~ % go run main.go run fast -pd="test" -pf="dev"
253-
time to run fast
254-
test
255-
dev
256-
```

0 commit comments

Comments
 (0)