commander

package module
v0.0.0-...-4888e7f Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 6 Imported by: 7

README

commander

Go library to facilitate command line tools that have different flags for different commands.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnrecognizedCommand = errors.New("No command executed")

Functions

func Execute

func Execute(args []string, commandFns ...CommandFunction) error

Execute takes an args array, and executes the appropriate command from the array of commandFunctions. If nil is passed as the args array, os.Args is used by default.

func MightExecute

func MightExecute(args []string, commandFns ...CommandFunction) (bool, error)

MightExecute returns a boolean indicating if the command executed, and the error if it did (which can be nil if no error occurred) This is useful in a situation where you might execute a command, but don't mind if no command is executed, but want to catch an error if a command fails. This is coded as:

if did,err=commander.MightExecute(...); did&&nil!=err {
  panic(err)
}

func MightExecuteWithErrorHandler

func MightExecuteWithErrorHandler(errHandler func(err error), args []string, commandFns ...CommandFunction) bool

MightExecuteWithErrorHandler might execute any command, and has an associated error handler if an error occurs. It returns true if a command was executed, false otherwise.

Types

type Command

type Command struct {
	Command     string
	Description string
	FlagSet     *flag.FlagSet
	F           func(args []string) error
}

Command wraps together the short command name, the description for a command, the commands Flags and the function that will handle the command.

func NewCommand

func NewCommand(cmd, description string, flagset *flag.FlagSet, f func(args []string) error) *Command

NewCommand creates a new comandeer Command struct with the given parameters.

type CommandFunction

type CommandFunction func() *Command

CommandFunction returns a command

type CommandList

type CommandList []*Command

func (CommandList) Len

func (cl CommandList) Len() int

func (CommandList) Less

func (cl CommandList) Less(i, j int) bool

func (CommandList) Swap

func (cl CommandList) Swap(i, j int)

Jump to

Keyboard shortcuts

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