command

package
v0.0.0-...-b5d9cbe Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 12 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 InstallSignalHandler

func InstallSignalHandler(out io.Writer, callback func(sig os.Signal))

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).

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

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
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) 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
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.

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
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) 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