https://github.com/jobala/commando
An elegant Go CLI framework
https://github.com/jobala/commando
Last synced: 6 months ago
JSON representation
An elegant Go CLI framework
- Host: GitHub
- URL: https://github.com/jobala/commando
- Owner: jobala
- License: mit
- Created: 2022-07-28T17:05:34.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-17T12:07:45.000Z (almost 4 years ago)
- Last Synced: 2024-06-21T03:02:38.721Z (about 2 years ago)
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# commando
An elegant CLI framework
## Getting Started
### Installation
To install commando simply do
`go get github.com/jobala/commando`
### Usage
Create a CLI instance
```go
cli := commando.NewCli("animals", "A cli app about the animal kingdom")
```
Commando exposes a declarative api for grouping commands and adding commands to a command group. The code snippet below adds the following commands;
1. `$animals vertebrates warm mammals bear --loudnes `
2. `$animals vertebrates warm mammals cow --loudness `
```go
cli.NewCommandGroup("vertebrates warm mammals").
WithCommand(commando.Command("bear", Bear)).
WithCommand(commando.Command("cow", Cow)).
func Bear(args BearArgs) {
// do something with BearArgs and print result
}
type BearArgs struct {
Loudness int
}
func Cow(args CowArgs) {
// do something CowArgs and print result
}
type CowArgs struct {
Loudness int
}
```
The handler function takes a single struct as an argument and the struct fields will be used to create the command's flags. The fields **must** be public.
To add another command group under `vertebrates`, do the following
```go
cli.NewCommandGroup("vertebrates cold").
WithCommand(commando.Command("snake", Snake))
```
Now you'll have `$animals vertebrates cold ...` on top of the initially defined commands
To add another command group under animals, simply do the following
```go
cli.NewCommandGroup("invertebrates").
WithCommand(commando.Command("leech", handlerFunc)).
```
The snippet above will add `$animals invertebrates leech`
You can use **--help** to show the available commands in a command group.
`$animals --help` will list available sub command[group]s