cockroach: github.com/cockroachdb/cockroach/pkg/util/flagutil Index | Examples | Files

package flagutil

import "github.com/cockroachdb/cockroach/pkg/util/flagutil"

Package flagutil facilitates creation of rich flag types.

Code:

var when time.Time
var re *regexp.Regexp
flags := pflag.NewFlagSet("flags", pflag.PanicOnError)
flags.Var(Time(&when), "when", "sets the time when it happens")
flags.Var(Regexp(&re), "re", "pattern to tell if it's a match")
if err := flags.Parse([]string{
    "--when", "13:02",
    "--re", "a match$",
}); err != nil {
    panic(err)
}
fmt.Println("it happens at", when.Format(time.Kitchen), re.MatchString("it's a match"))

Output:

it happens at 1:02PM true

Index

Examples

Package Files

flagutil.go

Variables

var TimeFormats = []string{
    log.MessageTimeFormat,
    log.FileTimeFormat,
    time.RFC3339Nano,
    time.RFC3339,
    time.Kitchen,
    "15:04:00.999999Z",
    "15:04:00.999999",
    "15:04Z",
    "15:04",
}

TimeFormats are the time formats which a time flag accepts for parsing time as described in time.Parse.

func Regexp Uses

func Regexp(r **regexp.Regexp) pflag.Value

Regexp returns a value which can be used with pflag.Var to create flags for regexp variables. The flag attempts to compile its input to a regular expression and assigns the result to *r. If the flag is empty, r is set to nil.

func Time Uses

func Time(t *time.Time) pflag.Value

Time returns a value which can be used with pflag.Var to create flags for time variables. The flag attempts to parse its in a variety of formats. The first is as a duration using time.ParseDuration and subtracting the result from Now() and then by using the values in Formats for parsing using time.Parse. An empty flag sets t to the zero value.

Package flagutil imports 6 packages (graph) and is imported by 2 packages. Updated 2019-07-21. Refresh now. Tools for package owners.