Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boreq/guinea
Go library for building command line interfaces.
https://github.com/boreq/guinea
cli command-line go golang
Last synced: 24 days ago
JSON representation
Go library for building command line interfaces.
- Host: GitHub
- URL: https://github.com/boreq/guinea
- Owner: boreq
- License: mit
- Created: 2016-12-04T20:24:29.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-08T13:09:28.000Z (almost 2 years ago)
- Last Synced: 2024-10-14T12:21:33.240Z (about 1 month ago)
- Topics: cli, command-line, go, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/boreq/guinea
- Size: 15.6 KB
- Stars: 62
- Watchers: 6
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# guinea [![CI](https://github.com/boreq/guinea/actions/workflows/ci.yml/badge.svg)](https://github.com/boreq/guinea/actions/workflows/ci.yml) [![Go Reference](https://pkg.go.dev/badge/github.com/boreq/guinea.svg)](https://pkg.go.dev/github.com/boreq/guinea)
Guinea is a command line interface library.
## Description
Programs very often organise the user interface in the form of subcommands. As
an example the `go` command lets the user invoke multiple subcommands such as
`go build` or `go get`. This library lets you nest any numbers of subcommands
(which can be thought of as separate programs) in each other easily building
complex user interfaces.## Example
This program implements a root command which displays the program version and
two subcommands.package main
import (
"fmt"
"github.com/boreq/guinea"
"os"
)var rootCommand = guinea.Command{
Options: []guinea.Option{
guinea.Option{
Name: "version",
Type: guinea.Bool,
Description: "Display version",
},
},
Run: func(c guinea.Context) error {
if c.Options["version"].Bool() {
fmt.Println("v0.0.0-dev")
return nil
}
return guinea.ErrInvalidParms
},
Subcommands: map[string]*guinea.Command{
"display_text": &commandDisplayText,
"greet": &commandGreet,
},
ShortDescription: "an example program using the guinea library",
Description: `This program demonstrates the use of a CLI library.`,
}var commandDisplayText = guinea.Command{
Run: func(c guinea.Context) error {
fmt.Println("Hello world!")
return nil
},
ShortDescription: "displays text on the screen",
Description: `This is a subcommand that displays "Hello world!" on the screen.`,
}var commandGreet = guinea.Command{
Arguments: []guinea.Argument{
guinea.Argument{
Name: "person",
Multiple: false,
Description: "a person to greet",
},
},
Options: []guinea.Option{
guinea.Option{
Name: "times",
Type: guinea.Int,
Description: "Number of greetings",
Default: 1,
},
},
Run: func(c guinea.Context) error {
for i := 0; i < c.Options["times"].Int(); i++ {
fmt.Printf("Hello %s!\n", c.Arguments[0])
}
return nil
},
ShortDescription: "greets the specified person",
Description: `This is a subcommand that greets the specified person.`,
}func main() {
if err := guinea.Run(&rootCommand); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}And here are the example invocations of the program:
$ ./main --help
$ ./main --version
$ ./main display_text
$ ./main hello --help
$ ./main hello boreq
$ ./main hello --times 10 boreq