Documentation
¶
Overview ¶
Package command provides consistency in parsing command line flags, reading config files, and writing logs. This package is intended for launching commands with operations (sub-commands).
Commands take the form:
command [flag ...] <operation> [operation flag ...] [arg ...]
Common Operations ¶
Provides a "help" psuedo-operation, which may be invoked as:
command help # shows general usage command help <operation> # shows operation-specific usage
Common Flags ¶
Top level flags include "-v" for verbosity, and "-config" to specify the directory where configuration files are found.
Flag Helpers ¶
Flags support provided by the golang standard flags package. The command package defines several types to support more advanced flag features.
See StringSet, BoolMap, and BoolCount.
Log Helpers ¶
Logging API is a simple addition to Go stdlib log package.
The V() helper limits verbosity. For example,
command.V(2).Log("hello world")
will write only if the "-v" flag appears twice (or more) in the command flags.
If command.Error() or command.Errorf() is called, then command.Exit() will terminate with a non-zero status.
Index ¶
- Constants
- Variables
- func Check(err error)
- func CheckUsage(err error)
- func Checkf(err error, format string, arg ...interface{})
- func ConfigDir() string
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Exit()
- func Operate(name string)
- func Parse() error
- func RegisterCommand(name, syntax, description string, option ...option)
- func RegisterOperation(handler func() error, name, syntax, description string)
- func UsageError(err interface{})
- func V(threshold int) verbose
- type BoolCount
- type BoolMap
- type StringSet
Constants ¶
const ( OptionConfig option = "config" OptionProfile option = "profile" OptionVerbose option = "verbose" )
Variables ¶
var (
Command command
)
Functions ¶
func Check ¶
func Check(err error)
Check logs an error and exits with non-zero status, when error is not nil.
func CheckUsage ¶
func CheckUsage(err error)
Helper function exits, after showing usage, if error is not nil.
func Checkf ¶
Checkf exits on non-nil error, logging a formatted message.
func ConfigDir ¶
func ConfigDir() string
ConfigDir provides a reasonable default location where configuration files may be found.
func Error ¶
func Error(args ...interface{})
func Exit ¶
func Exit()
func Parse ¶
func Parse() error
Parse is a wrapper around flag.CommandLine.Parse() that is command and operation aware. It strives to parse flags that appear either before or after the opeeration name. It supports a pseudo-operation "help" which behaves as if the "-h" flag is present.
func RegisterCommand ¶
func RegisterCommand(name, syntax, description string, option ...option)
Inject details about the current command.
func RegisterOperation ¶
Types ¶
type BoolCount ¶
type BoolCount int
A flag of type BoolCount can be repeated on the command line. For example: "command -v -v -v" gives the flag value 3.
type BoolMap ¶
A flag of type BoolMap can be repeated on the command line. For example, "command -user=alice -user=carol" gives the value {"alice": true, "carol": true}
Source Files
¶
- command.go
- config.go
- flags.go
- log.go
- release.go
- verbose.go