https://github.com/coder/cli
A minimal Go CLI package.
https://github.com/coder/cli
Last synced: about 1 month ago
JSON representation
A minimal Go CLI package.
- Host: GitHub
- URL: https://github.com/coder/cli
- Owner: coder
- License: mit
- Archived: true
- Created: 2019-04-26T21:00:03.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-18T20:26:57.000Z (almost 5 years ago)
- Last Synced: 2025-12-26T12:55:10.535Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 50
- Watchers: 13
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# cli
A minimal, command-oriented CLI package.
[](https://godoc.org/go.coder.com/cli)
## Features
- Very small, simple API.
- Support for POSIX flags.
- Only external dependency is [spf13/pflag](https://github.com/spf13/pflag).
- Subcommands and subcommand aliases.
- Auto-generated help.
## Install
```bash
go get -u go.coder.com/cli
```
## Examples
See `examples/` for more.
### Simple CLI
```go
package main
import (
"fmt"
"github.com/spf13/pflag"
"go.coder.com/cli"
)
type cmd struct {
verbose bool
}
func (c *cmd) Run(fl *pflag.FlagSet) {
if c.verbose {
fmt.Println("verbose enabled")
}
fmt.Println("we run")
}
func (c *cmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "simple-example",
Usage: "[flags]",
Desc: `This is a simple example of the cli package.`,
}
}
func (c *cmd) RegisterFlags(fl *pflag.FlagSet) {
fl.BoolVar(&c.verbose, "v", false, "sets verbose mode")
}
func main() {
cli.RunRoot(&cmd{})
}
```
renders a help like
```
Usage: simple-example [flags]
This is a simple example of the cli package.
simple-example flags:
-v sets verbose mode (false)
```
### Subcommands
```go
package main
import (
"fmt"
"github.com/spf13/pflag"
"go.coder.com/cli"
)
type subcmd struct {
}
func (c *subcmd) Run(fl *pflag.FlagSet) {
fmt.Println("subcommand invoked")
}
func (c *subcmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "sub",
Usage: "",
Aliases: []string{"s"},
Desc: `This is a simple subcommand.`,
}
}
type cmd struct {
}
func (c *cmd) Run(fl *pflag.FlagSet) {
// This root command has no default action, so print the help.
fl.Usage()
}
func (c *cmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "subcommand",
Usage: "[flags]",
Desc: `This is a simple example of subcommands.`,
}
}
func (c *cmd) Subcommands() []cli.Command {
return []cli.Command{
&subcmd{},
}
}
func main() {
cli.RunRoot(&cmd{})
}
```
renders a help like
```
Usage: subcommand [flags]
This is a simple example of subcommands.
Commands:
s, sub - This is a simple subcommand.
```