Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/draganm/decli
Declarative CLI library for golang
https://github.com/draganm/decli
Last synced: 9 days ago
JSON representation
Declarative CLI library for golang
- Host: GitHub
- URL: https://github.com/draganm/decli
- Owner: draganm
- License: bsd-3-clause
- Created: 2019-03-12T17:03:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-30T09:49:48.000Z (over 4 years ago)
- Last Synced: 2024-11-17T12:51:12.293Z (about 2 months ago)
- Language: Go
- Size: 17.6 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# De(clarative) CLI
Purpose of DeCLI is to simplify parsing complex CLI arguments and defining rich CLI tools with minimal amount of required code.
DeCLI builds on top of [cli](https://github.com/urfave/cli/v2). It relies heavily on defining CLI parameters as field tags in Golang structs instead of programmatically defining the arguments using Go code.
## Why should I use it?
DeCLI enables you to write more concise code and will add type safety to your arguments.
As an illustration, here's a hello world example of a DeCLI:
```go
package mainimport (
"fmt"
"os""github.com/draganm/decli"
)type App struct {
FirstName string `name:"first-name" usage:"your first name" aliases:"fn"`
LastName string `name:"last-name" usage:"your last name" aliases:"ln"`
Age int `name:"age" usage:"your age"`
}func (a *App) Run([]string) error {
fmt.Printf("Hello %s %s (%d)\n", a.FirstName, a.LastName, a.Age)
return nil
}func main() {
decli.RunAndFinish(&App{FirstName: "John", LastName: "Doe", Age: -1}, os.Args)
}
```as opposed to using raw [cli](https://github.com/urfave/cli/v2):
```go
package mainimport (
"fmt"
"log"
"os""github.com/urfave/cli/v2"
)func main() {
app := cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "first-name",
Usage: "your first name",
Aliases: []string{"fn"},
Value: "John",
},
&cli.StringFlag{
Name: "last-name",
Usage: "your last name",
Aliases: []string{"ln"},
Value: "Doe",
},
&cli.IntFlag{
Name: "age",
Usage: "your age",
Value: -1,
},
},
Action: func(c *cli.Context) error {
fmt.Printf("Hello %s %s (%d)\n", c.String("first-name"), c.String("last-name"), c.Int("age"))
return nil
},
}err := app.Run(os.Args)
if err != nil {
log.Fatalf("error: %s\n", err.Error())
}
}
```Both sources do the same, DeCLI version has almost the half of lines of code.