cli

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDefaultCommand = errors.New("default command is not registered")

ErrNoDefaultCommand is used when the default default command isn't registered.

View Source
var Registry = make(commandRegistry)

Registry holds every command that gets saved in this global commands registry.

Functions

func Errf

func Errf(format string, a ...interface{}) (int, error)

Errf writes to os.Stderr.

Shortcut for fmt.Fprintf(os.Stderr, format).

func Exitf

func Exitf(code int, format string, a ...interface{})

Exitf writes to os.Stderr and exits the process with a given code.

Shortcut for Errf(format); os.Exit(code).

func Outf

func Outf(format string, a ...interface{}) (int, error)

Outf writes to os.Stdout.

Shortcut for fmt.Fprintf(os.Stdout, format).

func RegisterCommand

func RegisterCommand(name, desc string, action CommandFn)

RegisterCommand registers a command in the global command registry. ParseArguments looks into it to decide which command to dispatch.

Types

type Args

type Args []string

Args represents an ordered collection of command line argument.

func ParseArgs

func ParseArgs(args []string) Args

ParseArgs parses an OS-like command line input arguments.

If you need to create an arguments object out of a plain []string use Args{args}.

func (Args) CommandName

func (a Args) CommandName() string

CommandName extracts a command name out of all the arguments.

A command cannot start with --. We think of those arguments as options.

func (Args) First added in v0.3.0

func (a Args) First() string

First extracts the first argument, no matter if command or option.

func (Args) Get added in v0.10.0

func (a Args) Get(option, defaultValue string) string

Get checks if an --option has a value given.

If the option is not given, the default value is returned. There is currently no concept of optional arguments, as we haven't got the need.

func (Args) Has added in v0.3.0

func (a Args) Has(option string) bool

Has tells whether the arguments contains a specific argument.

No distinction is made between arguments or options. Everything is matched as is.

func (Args) Raw added in v0.18.0

func (a Args) Raw() []string

Raw returns the arguments as a slice of strings.

func (Args) Rest

func (a Args) Rest() Args

Rest extracts the arguments after the command name.

type Command

type Command struct {
	Name   string
	Desc   string
	Action CommandFn
}

Command represents a command line action.

func DispatchCommand

func DispatchCommand(args Args, defaultCommand string) (*Command, error)

DispatchCommand dispatches the control to an registered command, if possible.

A command name is guessed out of the arguments. If the guessed name isn't registered, the dispatch will fall-back to the default command specified. It is expected that it is always registered. It is an error if its not.

func (*Command) IsOption

func (c *Command) IsOption() bool

IsOption returns whether the current command starts with --.

We consider commands starting with -- options, so we can display them as such.

type CommandFn

type CommandFn func(Args, config.Config) error

CommandFn represents a command handler function.

type CommandRegistry

type CommandRegistry interface {
	Commands() []Command
	Options() []Command
}

CommandRegistry can be queried for the available commands and options.

Jump to

Keyboard shortcuts

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