command

package
v0.0.0-...-d046166 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package command contains code shared by executables (e.g. test runners and test bundles).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFieldIfNonZero

func CopyFieldIfNonZero(src, dst interface{})

CopyFieldIfNonZero copies *src to *dst if *src does not contain the zero value. This is intended to be used when deprecating fields in structs used for IPC. src and dst must be of the same type; *bool, *string, and *[]string are supported. This function panics if passed any other types.

func WriteError

func WriteError(w io.Writer, err error) int

WriteError writes a newline-terminated fatal error to w and returns the status code to use when exiting. If err is not a *StatusError, status code 1 is returned.

Types

type DurationFlag

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

DurationFlag implements flag.Value to save a user-supplied integer time duration with fixed units to a time.Duration.

Example
var dest time.Duration
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(NewDurationFlag(time.Second, &dest, 5*time.Second), "flag", "usage")

// When the flag isn't supplied, the default is used.
flags.Parse([]string{})
fmt.Println("no flag:", dest)

// When the flag is supplied, it's interpreted as an integer duration using the supplied units.
flags.Parse([]string{"-flag=10"})
fmt.Println("flag:", dest)
Output:

no flag: 5s
flag: 10s

func NewDurationFlag

func NewDurationFlag(units time.Duration, dst *time.Duration, def time.Duration) *DurationFlag

NewDurationFlag returns a DurationFlag that will save a duration with the supplied units to dst.

func (*DurationFlag) Set

func (f *DurationFlag) Set(v string) error

Set sets the flag value.

func (*DurationFlag) String

func (f *DurationFlag) String() string

type EnumFlag

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

EnumFlag implements flag.Value to map a user-supplied string value to an enum value.

Example
type enum int
const (
	foo enum = 1
	bar      = 2
)

var dest enum
valid := map[string]int{"foo": int(foo), "bar": int(bar)}
assign := func(v int) { dest = enum(v) }
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(NewEnumFlag(valid, assign, "foo"), "flag", "usage")

// When the flag isn't supplied, the default is used.
flags.Parse([]string{})
fmt.Println("no flag:", dest)

// When a value is supplied, it's converted to the corresponding enum value.
flags.Parse([]string{"-flag=bar"})
fmt.Println("flag:", dest)
Output:

no flag: 1
flag: 2

func NewEnumFlag

func NewEnumFlag(valid map[string]int, assign EnumFlagAssignFunc, def string) *EnumFlag

NewEnumFlag returns an EnumFlag using the supplied map of valid values and assignment function. def contains a default value to assign when the flag is unspecified.

func (*EnumFlag) Default

func (f *EnumFlag) Default() string

Default returns the default value used if the flag is unset.

func (*EnumFlag) QuotedValues

func (f *EnumFlag) QuotedValues() string

QuotedValues returns a comma-separated list of quoted values the user can supply.

func (*EnumFlag) Set

func (f *EnumFlag) Set(v string) error

Set sets the flag value.

func (*EnumFlag) String

func (f *EnumFlag) String() string

type EnumFlagAssignFunc

type EnumFlagAssignFunc func(val int)

EnumFlagAssignFunc is used by EnumFlag to assign an enum value to a target variable.

type ListFlag

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

ListFlag implements flag.Value to split a user-supplied string with a custom delimiter into a slice of strings.

Example
var dest []string
assign := func(v []string) { dest = v }
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(NewListFlag(",", assign, []string{"a", "b"}), "flag", "usage")

// When the flag isn't supplied, the default is used.
flags.Parse([]string{})
fmt.Println("no flag:", dest)

// When the flag is supplied, its value is split into a slice.
flags.Parse([]string{"-flag=c,d,e"})
fmt.Println("flag:", dest)
Output:

no flag: [a b]
flag: [c d e]

func NewListFlag

func NewListFlag(sep string, assign ListFlagAssignFunc, def []string) *ListFlag

NewListFlag returns a ListFlag using the supplied separator and assignment function. def contains a default value to assign when the flag is unspecified.

func (*ListFlag) Default

func (f *ListFlag) Default() string

Default returns the default value used if the flag is unset.

func (*ListFlag) Set

func (f *ListFlag) Set(v string) error

Set sets the flag value.

func (*ListFlag) String

func (f *ListFlag) String() string

type ListFlagAssignFunc

type ListFlagAssignFunc func(vals []string)

ListFlagAssignFunc is called by ListFlag to assign a slice to a target variable.

type RepeatedFlag

type RepeatedFlag func(v string) error

RepeatedFlag implements flag.Value around an assignment function that is executed each time the flag is supplied.

Example
var dest []int
rf := RepeatedFlag(func(v string) error {
	i, err := strconv.Atoi(v)
	if err != nil {
		return err
	}
	dest = append(dest, i)
	return nil
})
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(&rf, "flag", "usage")

// When the flag isn't supplied, the slice is unchanged.
flags.Parse([]string{})
fmt.Println("no flag:", dest)

// The function is called each time the flag is provided.
flags.Parse([]string{"-flag=1", "-flag=2"})
fmt.Println("flag:", dest)
Output:

no flag: []
flag: [1 2]

func (*RepeatedFlag) Default

func (f *RepeatedFlag) Default() string

Default returns the default value used if the flag is unset.

func (*RepeatedFlag) Set

func (f *RepeatedFlag) Set(v string) error

Set sets the flag value.

func (*RepeatedFlag) String

func (f *RepeatedFlag) String() string

type StatusError

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

StatusError implements the error interface and contains an additional status code.

func NewStatusErrorf

func NewStatusErrorf(status int, format string, args ...interface{}) *StatusError

NewStatusErrorf creates a StatusError with the passed status code and formatted string.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) Status

func (e *StatusError) Status() int

Status returns e's status code.

Jump to

Keyboard shortcuts

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