flagutil

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: Apache-2.0 Imports: 9 Imported by: 8

Documentation

Overview

Package flagutil contains flags that parse string lists and string maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DualFormatBoolVar added in v0.10.0

func DualFormatBoolVar(fs *pflag.FlagSet, p *bool, name string, value bool, usage string)

DualFormatBoolVar creates a flag which supports both dashes and underscores

func DualFormatInt64Var added in v0.10.0

func DualFormatInt64Var(fs *pflag.FlagSet, p *int64, name string, value int64, usage string)

DualFormatInt64Var creates a flag which supports both dashes and underscores

func DualFormatIntVar added in v0.10.0

func DualFormatIntVar(fs *pflag.FlagSet, p *int, name string, value int, usage string)

DualFormatIntVar creates a flag which supports both dashes and underscores

func DualFormatStringListVar added in v0.10.0

func DualFormatStringListVar(fs *pflag.FlagSet, p *[]string, name string, value []string, usage string)

DualFormatStringListVar creates a flag which supports both dashes and underscores

func DualFormatStringVar added in v0.10.0

func DualFormatStringVar(fs *pflag.FlagSet, p *string, name string, value string, usage string)

DualFormatStringVar creates a flag which supports both dashes and underscores

func StringListVar

func StringListVar(fs *pflag.FlagSet, p *[]string, name string, defaultValue []string, usage string)

StringListVar defines a []string flag with the specified name, value and usage string. The argument 'p' points to a []string in which to store the value of the flag.

Types

type DurationOrIntVar added in v0.15.0

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

DurationOrIntVar implements pflag.Value for flags that have historically been of type IntVar (and then converted to seconds or some other unit) but are now transitioning to a proper DurationVar type.

When parsing a command-line argument, it will first attempt to parse the argument using time.ParseDuration; if this fails, it will fallback to strconv.ParseInt and multiply that value by the `fallback` unit value to get a duration. If the initial ParseDuration fails, it will also log a deprecation warning.

func NewDurationOrIntVar added in v0.15.0

func NewDurationOrIntVar(name string, val time.Duration, fallback time.Duration) *DurationOrIntVar

NewDurationOrIntVar returns a new DurationOrIntVar struct with the given name, default value, and fallback unit.

The name is used only when issuing a deprecation warning (so the user knows which flag needs its argument format updated).

The `fallback` argument is used when parsing an argument as an int (legacy behavior) as the multiplier to get a time.Duration value. As an example, if a flag used to be "the amount of time to wait in seconds" with a default of 60, you would do:

myFlag := flagutil.NewDurationOrIntVar("my-flag", time.Minute /* 60 second default */, time.Second /* fallback unit to multiply by */)

func (*DurationOrIntVar) Set added in v0.15.0

func (v *DurationOrIntVar) Set(s string) error

Set is part of the pflag.Value interface.

func (*DurationOrIntVar) String added in v0.15.0

func (v *DurationOrIntVar) String() string

String is part of the pflag.Value interface.

func (*DurationOrIntVar) Type added in v0.15.0

func (v *DurationOrIntVar) Type() string

Type is part of the pflag.Type interface.

func (*DurationOrIntVar) Value added in v0.15.0

func (v *DurationOrIntVar) Value() time.Duration

Value returns the underlying Duration value passed to the flag.

type OptionalFlag added in v0.11.0

type OptionalFlag interface {
	pflag.Value
	IsSet() bool
}

OptionalFlag augements the pflag.Value interface with a method to determine if a flag was set explicitly on the comand-line.

Though not part of the interface, because the return type would be different for each implementation, by convention, each implementation should define a Get() method to access the underlying value.

TODO (ajm188) - replace this interface with a generic type. c.f. https://github.com/vitessio/vitess/issues/11154.

type OptionalFloat64 added in v0.11.0

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

OptionalFloat64 implements OptionalFlag for float64 values.

func NewOptionalFloat64 added in v0.11.0

func NewOptionalFloat64(val float64) *OptionalFloat64

NewOptionalFloat64 returns an OptionalFloat64 with the specified value as its starting value.

func (*OptionalFloat64) Get added in v0.11.0

func (f *OptionalFloat64) Get() float64

Get returns the underlying float64 value of this flag. If the flag was not explicitly set, this will be the initial value passed to the constructor.

func (*OptionalFloat64) IsSet added in v0.11.0

func (f *OptionalFloat64) IsSet() bool

IsSet is part of the OptionalFlag interface.

func (*OptionalFloat64) Set added in v0.11.0

func (f *OptionalFloat64) Set(arg string) error

Set is part of the pflag.Value interface.

func (*OptionalFloat64) String added in v0.11.0

func (f *OptionalFloat64) String() string

String is part of the pflag.Value interface.

func (*OptionalFloat64) Type added in v0.15.0

func (f *OptionalFloat64) Type() string

Type is part of the pflag.Value interface.

type OptionalString added in v0.11.0

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

OptionalString implements OptionalFlag for string values.

func NewOptionalString added in v0.11.0

func NewOptionalString(val string) *OptionalString

NewOptionalString returns an OptionalString with the specified value as its starting value.

func (*OptionalString) Get added in v0.11.0

func (f *OptionalString) Get() string

Get returns the underlying string value of this flag. If the flag was not explicitly set, this will be the initial value passed to the constructor.

func (*OptionalString) IsSet added in v0.11.0

func (f *OptionalString) IsSet() bool

IsSet is part of the OptionalFlag interface.

func (*OptionalString) Set added in v0.11.0

func (f *OptionalString) Set(arg string) error

Set is part of the pflag.Value interface.

func (*OptionalString) String added in v0.11.0

func (f *OptionalString) String() string

String is part of the pflag.Value interface.

func (*OptionalString) Type added in v0.15.0

func (f *OptionalString) Type() string

Type is part of the pflag.Value interface.

type StringListValue

type StringListValue []string

StringListValue is a []string flag that accepts a comma separated list of elements. To include an element containing a comma, quote it with a backslash '\'.

func (StringListValue) Get

func (value StringListValue) Get() any

Get returns the []string value of this flag.

func (*StringListValue) Set

func (value *StringListValue) Set(v string) error

Set sets the value of this flag from parsing the given string.

func (StringListValue) String

func (value StringListValue) String() string

String returns the string representation of this flag.

func (StringListValue) Type added in v0.15.0

func (value StringListValue) Type() string

type StringMapValue

type StringMapValue map[string]string

StringMapValue is a map[string]string flag. It accepts a comma-separated list of key value pairs, of the form key:value. The keys cannot contain colons.

TODO (andrew): Look into whether there's a native pflag Flag type that we can use/transition to instead.

func (StringMapValue) Get

func (value StringMapValue) Get() any

Get returns the map[string]string value of this flag.

func (*StringMapValue) Set

func (value *StringMapValue) Set(v string) error

Set sets the value of this flag from parsing the given string.

func (StringMapValue) String

func (value StringMapValue) String() string

String returns the string representation of this flag.

func (StringMapValue) Type added in v0.15.0

func (value StringMapValue) Type() string

Type is part of the pflag.Value interface.

type StringSetFlag added in v0.11.0

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

StringSetFlag can be used to collect multiple instances of a flag into a set of values.

For example, defining the following:

var x flagutil.StringSetFlag
flag.Var(&x, "foo", "")

And then specifying "-foo x -foo y -foo x", will result in a set of {x, y}.

In addition to implemnting the standard flag.Value interface, it also provides an implementation of pflag.Value, so it is usable in libraries like cobra.

func (*StringSetFlag) Set added in v0.11.0

func (set *StringSetFlag) Set(s string) error

Set is part of the pflag.Value and flag.Value interfaces.

func (*StringSetFlag) String added in v0.11.0

func (set *StringSetFlag) String() string

String is part of the pflag.Value and flag.Value interfaces.

func (*StringSetFlag) ToSet added in v0.11.0

func (set *StringSetFlag) ToSet() sets.String

ToSet returns the underlying string set, or an empty set if the underlying set is nil.

func (*StringSetFlag) Type added in v0.11.0

func (set *StringSetFlag) Type() string

Type is part of the pflag.Value interface.

Jump to

Keyboard shortcuts

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