https://github.com/stevencyb/gocli
A simple CLI library for GoLang
https://github.com/stevencyb/gocli
Last synced: about 1 year ago
JSON representation
A simple CLI library for GoLang
- Host: GitHub
- URL: https://github.com/stevencyb/gocli
- Owner: StevenCyb
- License: mit
- Created: 2025-04-17T05:48:31.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-17T05:50:23.000Z (about 1 year ago)
- Last Synced: 2025-04-17T20:11:04.181Z (about 1 year ago)
- Language: Go
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Structure
This is a simple CLI tool that can be configured as seen in the chart below.
``` mermaid
flowchart LR
CLI[cli] --> COMMAND[commands]
COMMAND --> OPTIONS[options]
COMMAND --> SUBCOMMAND[subcommands 1]
SUBCOMMAND2 --> ARGS
SUBCOMMAND --> ARGS
COMMAND --> ARGS[args]
SUBCOMMAND --> SUBCOMMAND2[subcommands ...N]
SUBCOMMAND --> OPTIONS
SUBCOMMAND2 --> OPTIONS
ARGS --> OPTIONS
CLI --> OPTIONS
CLI --> ARGS
```
## Installation
```bash
get get github.com/StevenCyb/GoCLI
```
## Examples
```go
func main() {
c := cli.New(
cli.Name("gurl"),
cli.Banner(`╔═╗╔═╗┬ ┬┬─┐┬
║ ╦║ │ │├┬┘│
╚═╝╚═╝└─┘┴└─┴─┘`),
cli.Description("A simple CLI for making HTTP requests"),
cli.Version("1.0.0"),
cli.Command(
"get", cli.Argument(
"url",
cli.Validate(regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)),
cli.Description("The URL to get"),
cli.Handler(
func(ctx *cli.Context) error {
url := ctx.GetArgument("url")
fmt.Printf("Perform [GET] %s\n", *url)
return nil
},
),
cli.Option(
"verbose",
cli.Short('v'),
cli.Default(""),
),
),
cli.Description("Get a resource"),
cli.Example("cli get http://example.com"),
),
cli.Command(
"post", cli.Argument(
"url",
cli.Validate(regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)),
cli.Description("The URL to post"),
cli.Handler(
func(ctx *cli.Context) error {
url := ctx.GetArgument("url")
bodyFile := ctx.GetOption("verbose")
fmt.Printf("Perform [POST] %s\n", *url)
if bodyFile != nil {
fmt.Printf("\t With %s\n", *bodyFile)
}
return nil
},
),
cli.Option(
"body_file",
cli.Short('b'),
),
cli.Option(
"verbose",
cli.Short('v'),
cli.Default(""),
),
),
cli.Description("Get a resource"),
cli.Example("cli get http://example.com"),
),
// ...
cli.Command(
"version", cli.Handler(
func(_ *cli.Context) error {
fmt.Println("1.0.0")
return nil
},
),
cli.Description("Get the version of the CLI"),
),
)
_, err := c.RunWith(os.Args)
if err != nil {
fmt.Println(err)
c.PrintHelp()
os.Exit(1)
}
}
```