https://github.com/fraugster/cli
Utility functions for running command line applications in Go.
https://github.com/fraugster/cli
cli context go
Last synced: 6 months ago
JSON representation
Utility functions for running command line applications in Go.
- Host: GitHub
- URL: https://github.com/fraugster/cli
- Owner: fraugster
- License: bsd-3-clause
- Created: 2017-10-05T14:05:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-04T12:03:27.000Z (over 7 years ago)
- Last Synced: 2025-08-13T21:30:19.074Z (11 months ago)
- Topics: cli, context, go
- Language: Go
- Size: 12.7 KB
- Stars: 6
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## github.com/fraugster/cli
[](http://travis-ci.org/fraugster/cli)
[](https://godoc.org/github.com/fraugster/cli)
[](https://github.com/fraugster/cli/blob/master/LICENSE)
Package `cli` implements utility functions for running command line applications.
The following simple features are currently implemented:
- creating a `context.Context` which is closed when `SIGINT`, `SIGQUIT` or `SIGTERM` is received.
- context aware reading lines from stdin into a channel
- printing values using user a defined format (e.g. `json`, `yml` or `table`)
## Motivation
CLI applications often perform similar tasks such as handling stop signals, reading
from stdin and printing results to stdout. This package provides these functions
in a single and coherent repository. Applications built with `github.com/fraugster/cli`
treat `context.Context` as first class citizen to remain responsive and implement
graceful shutdown. Naturally this is visible when consuming (multi-line) input
from stdin. The `cli.Print` function is especially useful since it encourages
developers to support multiple machine readable output formats which makes it easy
to pipe the results of one application into another (e.g. `my-app -o=json | jq …`).
## Installation
```sh
$ go get github.com/fraugster/cli
```
## Usage
```go
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/fraugster/cli"
)
func main() {
// Create a context that is done when SIGINT, SIGQUIT or SIGTERM is received
ctx := cli.Context()
// Let the user decide what output format she prefers.
format := flag.String("output", "json", "Output format. One of json|yaml|table|raw")
flag.Parse()
// Reading a single line from stdin (returns "" if context is canceled).
fmt.Print("Please insert your name: ")
name := cli.ReadLine(ctx)
fmt.Println("Hello", name)
// Continuously read lines from stdin and return them in a channel.
fmt.Println("You have 10 seconds to talk to me")
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var inputs []string
for line := range cli.ReadLines(ctx) {
inputs = append(inputs, line)
fmt.Println("OK, go on")
}
fmt.Println("Time is up")
// Print something to stdout in a format specified by the user.
fmt.Println("Your inputs were:")
cli.Print(*format, inputs)
}
```
## Dependencies
- `gopkg.in/yaml.v2` for YAML output
- `github.com/stretchr/testify` to run unit tests
### License
This package is licensed under the the BSD 3-Clause License.
Please see the [LICENSE](LICENSE) file for details.
## Contributing
Contributions are welcome but must be discussed first in a new github issue before
Pull Requests will be accepted.