cli

package
v7.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2021 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package cli is a package to build CLI programs.

eg:

 func main() {
     opts := struct {
	        Foo string `env:"FOO" help:"A simple string."`
		    Bar int    `env:"BAR" help:"A simple integer."`
     }{
		    Foo: "foo",
		    Bar: 42,
     }

     ctx, cancel := cli.ContextWithSignals(context.Background(),
         os.Interrupt,
		    syscall.SIGTERM,
     )
     defer cancel()

     cli.Register().
		    Help("A simple command").
		    Options(&opts)

     cli.Register("hello").
		    Help("A sub command").
		    Options(&opts)

     cli.Register("world").
		    Help("Another sub command").
		    Options(&opts)

     switch cli.Load() {
	    case "hello":
		    helloCmd(ctx, opts)

     case "world":
		    worldCmd(ctx, opts)

	    default:
		    defaultCmd(ctx, opts)
	    }
 }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithSignals

func ContextWithSignals(parent context.Context, sig ...os.Signal) (ctx context.Context, cancel func())

ContextWithSignals returns a copy of the parent context that gets canceled when one of the specified signals is emitted. If no signals are provided, all incoming signals will cancel the context.

Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.

func Error

func Error(err error)

Error prints the given error and exit the program with code -1.

func Load

func Load() (cmd string)

Load loads the registered command that matches the program args. If defined, environment variables and flags are loaded in the command options.

It prints the command usage and exits the program with code -1 when an error occurs.

func Usage

func Usage()

Usage prints the loaded command usage. It panics when called before the Load function.

Types

type Command

type Command interface {
	// Sets the command help description.
	Help(string) Command

	// Sets the command options with the given receiver. The receiver must be a
	// pointer to a struct.
	Options(interface{}) Command
}

Command is the interface that describes a command.

func Register

func Register(cmd ...string) Command

Register registers and returns the named command.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL