An open API service indexing awesome lists of open source software.

https://github.com/livebud/cli

Build beautiful CLIs in Go.
https://github.com/livebud/cli

Last synced: 2 months ago
JSON representation

Build beautiful CLIs in Go.

Awesome Lists containing this project

README

          

# CLI

[![Go Reference](https://pkg.go.dev/badge/github.com/livebud/cli.svg)](https://pkg.go.dev/github.com/livebud/cli)

Build beautiful CLIs in Go. A simpler alternative to [kingpin](https://github.com/alecthomas/kingpin).

CleanShot 2023-06-20 at 22 23 14@2x

## Features

- Type-safe, fluent API
- Flag, command and argument support
- Required and optional parameters
- Built entirely on the [flag](https://pkg.go.dev/flag) package the standard library
- Supports both space-based and colon-based subcommands (e.g. `controller new` & `controller:new`)
- `SIGINT` context cancellation out-of-the-box
- Custom help messages
- Built-in tab completion with `complete -o nospace -C `
- Respects `NO_COLOR`

## Install

```sh
go get -u github.com/livebud/cli
```

## Example

```go
package main

import (
"context"
"fmt"
"os"

"github.com/livebud/cli"
)

func main() {
flag := new(Flag)
cli := cli.New("app", "your awesome cli").Writer(os.Stdout)
cli.Flag("log", "log level").Short('L').String(&flag.Log).Default("info")
cli.Flag("embed", "embed the code").Bool(&flag.Embed).Default(false)

{ // new
cmd := &New{Flag: flag}
cli := cli.Command("new", "create a new project")
cli.Arg("dir").String(&cmd.Dir)
cli.Run(cmd.Run)
}

ctx := context.Background()
if err := cli.Parse(ctx, os.Args[1:]...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

type Flag struct {
Log string
Embed bool
}

type New struct {
Flag *Flag
Dir string
}

// Run new
func (n *New) Run(ctx context.Context) error {
return nil
}
```

## Contributing

First, clone the repo:

```sh
git clone https://github.com/livebud/cli
cd cli
```

Next, install dependencies:

```sh
go mod tidy
```

Finally, try running the tests:

```sh
go test ./...
```

## License

MIT