command

package
v0.0.0-...-5cb4c69 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

README

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.

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

View Source
const (
	OptionConfig  option = "config"
	OptionProfile option = "profile"
	OptionVerbose option = "verbose"
)

Variables

View Source
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

func Checkf(err error, format string, arg ...interface{})

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 Errorf

func Errorf(format string, args ...interface{})

func Exit

func Exit()

func Operate

func Operate(name string)

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

func RegisterOperation(handler func() error, name, syntax, description string)

func UsageError

func UsageError(err interface{})

Prepend error messages to Usage() output.

func V

func V(threshold int) verbose

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.

func (*BoolCount) IsBoolFlag

func (this *BoolCount) IsBoolFlag() bool

func (*BoolCount) Set

func (this *BoolCount) Set(value string) error

func (*BoolCount) String

func (this *BoolCount) String() string

type BoolMap

type BoolMap map[string]bool

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}

func (BoolMap) Set

func (this BoolMap) Set(value string) error

func (BoolMap) String

func (this BoolMap) String() string

type StringSet

type StringSet []string

A flag of type StringSet can be repeated on the command line. For example: `command -user=alice -user=bob -user=carol` gives the flag value []string{"alice", "bob", "carol"}

func (*StringSet) Set

func (this *StringSet) Set(value string) error

func (*StringSet) String

func (this *StringSet) String() string

func (*StringSet) Strings

func (this *StringSet) Strings() []string

Source Files

  • command.go
  • config.go
  • flags.go
  • log.go
  • release.go
  • verbose.go

Jump to

Keyboard shortcuts

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