flagx

package module
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 11 Imported by: 1

README

Flagx

Golang command line tool with environment supproted, Command line arguments have higher priority than environment variables.

Examples

logFile    = flagx.NewString("log.file", "", "the log file")
logLevel   = flagx.NewBool("log.level", false, "the log level, default is bool")
serverHost = flagx.NewString("server.addr", ":7070", "the address http will serve on")
serverPort = flagx.NewInt("server.port", 8080, "the tcp port will listen on")
secs = flagx.NewDuration("secs", "30s", "the duration flag, 30 secodns")
secs2 = flagx.NewDuration("days", "30d", "the duration flag, 30 days")
secs3 = flagx.NewDuration("weeks", "2w", "the duration flag, 2 weeks")
bytes = flagx.NewBytes("bytes", 128, "the bytes flag, 128 Byte")
kbs = flagx.NewBytes("kbytes", 64, "the bytes flag, 64KB")
mbs = flagx.NewBytes("mbytes", 64, "the bytes flag, 64KB")
gbs = flagx.NewBytes("gbytes", 64, "the bytes flag, 64KB")
strs = flagx.NewArrayString("array.str", "the string array flag, default is empty")
ints = flagx.NewArrayInt("array.int", "the string array flag, default is empty")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurationValue

func DurationValue(s string, step int64) (int64, error)

DurationValue returns the duration in milliseconds for the given s and the given step.

Duration in s may be combined, i.e. 2h5m, -2h5m or 2h-5m.

The returned duration value can be negative.

func IsSecretFlag

func IsSecretFlag(s string) bool

IsSecretFlag returns true of s contains flag name with secret value, which shouldn't be exposed.

func Lookup

func Lookup(name string) (*flag.Flag, any)

Lookup a flag by name. the second return value is the real flag pointer which is returned by flagx.NewXXX. nil, nil will be returned if the flag is not found.

func NewBool

func NewBool(name string, value bool, usage string, opts ...Option) *bool

NewBool creates a new bool flag.

func NewFloat

func NewFloat(name string, value float64, usage string, opts ...Option) *float64

NewFloat creates a new float64 flag.

func NewInt

func NewInt(name string, value int, usage string, opts ...Option) *int

NewInt creates a new int flag.

func NewInt64

func NewInt64(name string, value int64, usage string, opts ...Option) *int64

NewInt64 creates a new int64 flag.

func NewString

func NewString(name string, value string, usage string, opts ...Option) *string

NewString creates a new string flag.

func Parse

func Parse()

Parse parses environment vars(if env.prefix provided) and command-line flags.

Flags set via command-line override flags set via environment vars.

This function must be called instead of flag.Parse() before using any flags in the program.

func ParseFlagSet

func ParseFlagSet(fs *flag.FlagSet, args []string)

ParseFlagSet parses the given args into the given fs.

func PositiveDurationValue

func PositiveDurationValue(s string, step int64) (int64, error)

PositiveDurationValue returns positive duration in milliseconds for the given s and the given step.

Duration in s may be combined, i.e. 2h5m or 2h-5m.

Error is returned if the duration in s is negative.

func RegisterSecretFlag

func RegisterSecretFlag(flagName string)

RegisterSecretFlag registers flagName as secret.

This function must be called before starting logging. It cannot be called from concurrent goroutines.

Secret flags aren't exported at `/metrics` page.

func Usage

func Usage(s string)

Usage prints s and optional description for all the flags if -h or -help flag is passed to the app.

func Visit

func Visit(fn func(string, string))

Visit the flags name and values set in command line

func VisitAll

func VisitAll(fn func(string, string))

Visit all the flag name and values, including those not set in command line.

func WriteFlags

func WriteFlags(w io.Writer)

WriteFlags writes all the explicitly set flags to w.

Types

type ArrayBool

type ArrayBool []bool

ArrayBool is a flag that holds an array of booleans values.

Has the same api as ArrayString.

func NewArrayBool

func NewArrayBool(name, usage string, opts ...Option) *ArrayBool

NewArrayBool returns new ArrayBool with the given name and description.

func (*ArrayBool) GetOptionalArg

func (a *ArrayBool) GetOptionalArg(argIdx int) bool

GetOptionalArg returns optional arg under the given argIdx.

func (*ArrayBool) IsBoolFlag

func (a *ArrayBool) IsBoolFlag() bool

IsBoolFlag implements flag.IsBoolFlag interface

func (*ArrayBool) Set

func (a *ArrayBool) Set(value string) error

Set implements flag.Value interface

func (*ArrayBool) String

func (a *ArrayBool) String() string

String implements flag.Value interface

type ArrayBytes

type ArrayBytes []*Bytes

ArrayBytes is flag that holds an array of Bytes.

Has the same api as ArrayString.

func NewArrayBytes

func NewArrayBytes(name, usage string, opts ...Option) *ArrayBytes

NewArrayBytes returns new ArrayBytes with the given name and description.

func (*ArrayBytes) GetOptionalArgOrDefault

func (a *ArrayBytes) GetOptionalArgOrDefault(argIdx int, defaultValue int64) int64

GetOptionalArgOrDefault returns optional arg under the given argIdx.

func (*ArrayBytes) Ints

func (a *ArrayBytes) Ints() []int

func (*ArrayBytes) Set

func (a *ArrayBytes) Set(value string) error

Set implemented flag.Value interface

func (*ArrayBytes) String

func (a *ArrayBytes) String() string

String implements flag.Value interface

type ArrayDuration

type ArrayDuration []time.Duration

ArrayDuration is a flag that holds an array of time.Duration values.

Has the same api as ArrayString.

func NewArrayDuration

func NewArrayDuration(name, usage string, opts ...Option) *ArrayDuration

NewArrayDuration returns new ArrayDuration with the given name and description.

func (*ArrayDuration) GetOptionalArgOrDefault

func (a *ArrayDuration) GetOptionalArgOrDefault(argIdx int, defaultValue time.Duration) time.Duration

GetOptionalArgOrDefault returns optional arg under the given argIdx, or default value, if argIdx not found.

func (*ArrayDuration) Set

func (a *ArrayDuration) Set(value string) error

Set implements flag.Value interface

func (*ArrayDuration) String

func (a *ArrayDuration) String() string

String implements flag.Value interface

type ArrayInt

type ArrayInt []int

ArrayInt is flag that holds an array of ints.

Has the same api as ArrayString.

func NewArrayInt

func NewArrayInt(name, usage string, opts ...Option) *ArrayInt

NewArrayInt returns new ArrayInt with the given name and description.

func (*ArrayInt) GetOptionalArgOrDefault

func (a *ArrayInt) GetOptionalArgOrDefault(argIdx, defaultValue int) int

GetOptionalArgOrDefault returns optional arg under the given argIdx.

func (*ArrayInt) Set

func (a *ArrayInt) Set(value string) error

Set implements flag.Value interface

func (*ArrayInt) String

func (a *ArrayInt) String() string

String implements flag.Value interface

type ArrayString

type ArrayString []string

ArrayString is a flag that holds an array of strings.

It may be set either by specifying multiple flags with the given name passed to NewArray or by joining flag values by comma.

The following example sets equivalent flag array with two items (value1, value2):

-foo=value1 -foo=value2
-foo=value1,value2

Each flag value may contain commas inside single quotes, double quotes, [], () or {} braces. For example, -foo=[a,b,c] defines a single command-line flag with `[a,b,c]` value.

Flag values may be quoted. For instance, the following arg creates an array of ("a", "b,c") items:

-foo='a,"b,c"'

func NewArrayString

func NewArrayString(name, usage string, opts ...Option) *ArrayString

NewArrayString returns new ArrayString with the given name and description.

func (*ArrayString) GetOptionalArg

func (a *ArrayString) GetOptionalArg(argIdx int) string

GetOptionalArg returns optional arg under the given argIdx.

func (*ArrayString) Set

func (a *ArrayString) Set(value string) error

Set implements flag.Value interface

func (*ArrayString) String

func (a *ArrayString) String() string

String implements flag.Value interface

type ArrayText

type ArrayText []*Text

ArrayText is a flag that used to hold multiple Text values.

func NewArrayText

func NewArrayText(name, usage string, opts ...Option) *ArrayText

NewArrayBytes returns new ArrayText with the given name and description.

func (*ArrayText) Set

func (a *ArrayText) Set(value string) error

Set implements flag.Value interface

func (*ArrayText) String

func (a *ArrayText) String() string

String implements flag.Value interface

func (*ArrayText) Strings

func (a *ArrayText) Strings() []string

type Bytes

type Bytes struct {
	// N contains parsed value for the given flag.
	N int64
	// contains filtered or unexported fields
}

Bytes is a flag for holding size in bytes.

It supports the following optional suffixes for values: KB, MB, GB, TB, KiB, MiB, GiB, TiB.

func NewBytes

func NewBytes(name string, defaultValue int64, usage string, opts ...Option) *Bytes

NewBytes returns new `bytes` flag with the given name, defaultValue and description.

func (*Bytes) IntN

func (b *Bytes) IntN() int

IntN returns the stored value capped by int type.

func (*Bytes) Set

func (b *Bytes) Set(value string) error

Set implements flag.Value interface

func (*Bytes) String

func (b *Bytes) String() string

String implements flag.Value interface

type Duration

type Duration struct {
	// Msecs contains parsed duration in milliseconds.
	Msecs int64
	// contains filtered or unexported fields
}

Duration is a flag for holding duration.

func NewDuration

func NewDuration(name string, defaultValue string, usage string, opts ...Option) *Duration

NewDuration returns new `duration` flag with the given name, defaultValue and description.

DefaultValue is in months.

func (*Duration) Set

func (d *Duration) Set(value string) error

Set implements flag.Value interface

func (*Duration) String

func (d *Duration) String() string

String implements flag.Value interface

type Option

type Option func(*flagx)

Option for flags

func Env

func Env(env string) Option

Env customize environment for flag.

func Required

func Required() Option

Required mark the flag MUST BE set via command line or environment

type Text

type Text struct {
	Value string
}

Text is a flag that used to hold complex content, such as json. the passed value should be base64-encoded string, it will be decoded before assignment.

func NewText

func NewText(name string, value string, usage string, opts ...Option) *Text

NewText creates a new text flag.

func (*Text) MarshalText

func (t *Text) MarshalText() ([]byte, error)

func (*Text) UnmarshalText

func (t *Text) UnmarshalText(text []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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