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 InstallSignalHandler ¶
InstallSignalHandler installs a signal handler that calls callback and runs common logic (e.g. dumping stack traces). out is the output stream to write messages to (typically stderr).
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 ¶
package main
import (
"flag"
"fmt"
"time"
"go.chromium.org/tast/core/internal/command"
)
func main() {
var dest time.Duration
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(command.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 ¶
NewDurationFlag returns a DurationFlag that will save a duration with the supplied units to dst.
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 ¶
package main
import (
"flag"
"fmt"
"go.chromium.org/tast/core/internal/command"
)
func main() {
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(command.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) QuotedValues ¶
QuotedValues returns a comma-separated list of quoted values the user can supply.
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 ¶
package main
import (
"flag"
"fmt"
"go.chromium.org/tast/core/internal/command"
)
func main() {
var dest []string
assign := func(v []string) { dest = v }
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Var(command.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.
type ListFlagAssignFunc ¶
type ListFlagAssignFunc func(vals []string)
ListFlagAssignFunc is called by ListFlag to assign a slice to a target variable.
type RepeatedFlag ¶
RepeatedFlag implements flag.Value around an assignment function that is executed each time the flag is supplied.
Example ¶
package main
import (
"flag"
"fmt"
"strconv"
"go.chromium.org/tast/core/internal/command"
)
func main() {
var dest []int
rf := command.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) 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