commandflags

package module
v0.0.0-...-f61ae2b Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2015 License: MIT Imports: 3 Imported by: 0

README

commandflags

A bolt-on extension to the Go flag library that implements a tree of COMMAND + FLAGS + SUBCOMMAND + FLAGS ......

Each command may have its own flag.Flagset, and both short and long descriptions that are used when writing help. If a command has sub commands, then the subcommand must also be specified. The flags for a command immediately follow the command.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWidth int = 80

DefaultWidth is the default screen/term width assumed when wrapping text for help output.

View Source
var HelpIndent int = 2

HelpIndent sets the number of spaces each subcommand's help is indented

Functions

This section is empty.

Types

type CommandType

type CommandType struct {
	Name        string                 // Name of command
	ShortDesc   string                 // Short description of subcommand
	LongDesc    string                 // Detailed description of subcommand
	Help        string                 // Documentation of subcommand
	Flags       *flag.FlagSet          // Flagset for command
	SubCommands map[string]CommandType // map of subcommands
}

CommandType implements a nested Command-flag structure whereby options (flags) are processed, and then subcommands are processed. Each subcommand is another commandType and the process recurses, each having it's own flag set. The ShortDesc is displayed when the parent command's help lists available subcommands. The LongDesc is displayed at the top of the help for the command, and the Help is displayed at the bottom. The Help is often used to explain the interaction between flags or expand upon flag documentation. The flagset will be renamed, to the name of the command, and the flag.FlagSet error handling will be reset to flag.ContinueOnError. This allows the error handling to be done by commandflags and the downstream program.

func NewCommandType

func NewCommandType(name string, flags *flag.FlagSet) CommandType

NewCommandType returns an initialized CommandType

func (*CommandType) ProcessArgs

func (c *CommandType) ProcessArgs(args []string) ([]string, Error)

ProcessArgs starts the recursive process of setting flags and processing sub-commands and returns a slice of strings that correspond to the names of the commands/subcommands chosen.

type Error

type Error interface {
	Error() string             // standard error interface
	CommandType() *CommandType // reference to command type that had error
	Args() []string            // slice of remaining arguments
}

The Error interface implements error and adds CommandType() and Args() methods that return the CommandType object in which the error occurred and the remaining arguments that were being process when the error occurred, respectively

type FlagError

type FlagError struct {
	UsageError
}

A FlagError is returned when the upstream flag library encounters an error while parsing arguments for flags.

type InvalidCommandError

type InvalidCommandError struct {
	UsageError
}

An InvalidCommandError is returned when a command expected a sub-command, but the next remaining argument does not match the valid sub-commands in the CommandType object's SubCommands map.

type MissingCommandError

type MissingCommandError struct {
	UsageError
}

A MissingCommandError is returned when a command expected a sub-command (i.e. the CommandType object's SubCommands map was not empty) but there were no more arguments remaining to process.

type UsageError

type UsageError struct {
	// contains filtered or unexported fields
}

A UsageError object is defined as the underlying type for the MissingCommandError, InvalidCommandError, and FlagError types. It implements the Error interface.

func (UsageError) Args

func (e UsageError) Args() []string

Args returns the arguments being process when the error was encountered.

func (UsageError) CommandType

func (e UsageError) CommandType() *CommandType

CommandType returns the CommandType object that encountered the processing. error.

func (UsageError) Error

func (e UsageError) Error() string

Error implements the standard error interface in UsageError.

Directories

Path Synopsis
* example.go implements an api demo for command flags
* example.go implements an api demo for command flags

Jump to

Keyboard shortcuts

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