Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Repository files navigation

cli

A minimal, command-oriented CLI package.

GoDoc

Features

  • Very small, simple API.
  • Support for POSIX flags.
  • Only external dependency is spf13/pflag.
  • Subcommands and subcommand aliases.
  • Auto-generated help.

Install

go get -u go.coder.com/cli

Examples

See examples/ for more.

Simple CLI

package main
import (
"fmt""github.com/spf13/pflag""go.coder.com/cli"
)
typecmdstruct {
verbosebool
}
func (c*cmd) Run(fl*pflag.FlagSet) {
ifc.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")
}
funcmain() {
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

package main
import (
"fmt""github.com/spf13/pflag""go.coder.com/cli"
)
typesubcmdstruct {
}
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.`,
}
}
typecmdstruct {
}
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{},
}
}
funcmain() {
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.

About

A minimal Go CLI package.

Resources

Code of conduct

Security policy

Stars

50 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages