flagx

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 10 Imported by: 0

README

flagx

build-img pkg-img reportcard-img coverage-img version-img

Go flag utils.

Features

  • Simple API.
  • Dependency-free.
  • Clean and tested code.
  • Fully compatible with flag package.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/flagx

Example

args := []string{"-t", "20s"} // or os.Args[1:]

var d time.Duration
fset := flagx.NewFlagSet("testing", os.Stderr)
fset.Duration(&d, "timeout", "t", 10*time.Second, "just a timeout")

err := fset.Parse(args)
if err != nil {
	panic(err)
}

fmt.Println(d)

// Output: 20s

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.

Documentation

Overview

Example (TextVar)
package main

import (
	"fmt"
	"net"
	"os"

	"github.com/cristalhq/flagx"
)

func main() {
	fs := flagx.NewFlagSet("ExampleTextVar", os.Stdout)

	var ip net.IP
	fs.Text(&ip, "ip", "", net.IPv4(192, 168, 0, 100), "`IP address` to parse")

	err := fs.Parse([]string{"-ip", "127.0.0.1"})
	if err != nil {
		panic(err)
	}
	fmt.Printf("{ip: %v}\n\n", ip)

}
Output:

{ip: 127.0.0.1}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlagSet

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

FlagSet represents a set of flags. Flag names must be unique within a FlagSet. An attempt to define a flag whose name is already in use will cause a panic.

Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/cristalhq/flagx"
)

func main() {
	args := []string{"-t", "20s"} // or os.Args[1:]

	var d time.Duration
	fset := flagx.NewFlagSet("testing", os.Stderr)
	fset.Duration(&d, "timeout", "t", 10*time.Second, "just a timeout")

	err := fset.Parse(args)
	if err != nil {
		panic(err)
	}

	fmt.Println(d)

}
Output:

20s

func NewFlagSet

func NewFlagSet(name string, output io.Writer) *FlagSet

NewFlagSet returns new FlagSet.

func (*FlagSet) Arg

func (f *FlagSet) Arg(i int) string

func (*FlagSet) Args

func (f *FlagSet) Args() []string

func (*FlagSet) AsStdlib added in v0.2.0

func (f *FlagSet) AsStdlib() *flag.FlagSet

AsStdlib returns *flag.FlagSet with all flags. Note: aliases are duplicated.

func (*FlagSet) Bool

func (f *FlagSet) Bool(p *bool, name, alias string, value bool, usage string)

Bool defines a bool flag with specified name, alias, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) BoolSlice added in v0.2.0

func (f *FlagSet) BoolSlice(p *[]bool, name, alias string, value []bool, sep, usage string)

BoolSlice defines a slice of bool flag with specified name, alias, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. Empty string for alias means no alias will be created. BoolSlice panics on empty separator.

func (*FlagSet) Duration

func (f *FlagSet) Duration(p *time.Duration, name, alias string, value time.Duration, usage string)

Duration defines a time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created.

func (*FlagSet) DurationSet added in v0.2.0

func (f *FlagSet) DurationSet(p *map[time.Duration]struct{}, name, alias string, value map[time.Duration]struct{}, usage string)

DurationSet defines a set of time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created.

func (*FlagSet) DurationSlice added in v0.2.0

func (f *FlagSet) DurationSlice(p *[]time.Duration, name, alias string, value []time.Duration, sep, usage string)

DurationSlice defines a slice of time.Duration flag with specified name, alias, default value, separator, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created. DurationSlice panics on empty separator.

func (*FlagSet) Float64

func (f *FlagSet) Float64(p *float64, name, alias string, value float64, usage string)

Float64 defines a float64 flag with specified name, alias, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Float64Set added in v0.2.0

func (f *FlagSet) Float64Set(p *map[float64]struct{}, name, alias string, value map[float64]struct{}, usage string)

Float64Set defines a set of float64 flag with specified name, alias, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Float64Slice added in v0.2.0

func (f *FlagSet) Float64Slice(p *[]float64, name, alias string, value []float64, sep, usage string)

Float64Slice defines a slice of float64 flag with specified name, alias, default value, separator, and usage string. The argument p points to a float64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Float64Slice panics on empty separator.

func (*FlagSet) Func

func (f *FlagSet) Func(name, alias, usage string, fn func(string) error)

Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. Empty string for alias means no alias will be created.

func (*FlagSet) Int

func (f *FlagSet) Int(p *int, name, alias string, value int, usage string)

Int defines an int flag with specified name, alias, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Int64

func (f *FlagSet) Int64(p *int64, name, alias string, value int64, usage string)

Int64 defines an int64 flag with specified name, alias, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Int64Set added in v0.2.0

func (f *FlagSet) Int64Set(p *map[int64]struct{}, name, alias string, value map[int64]struct{}, usage string)

Int64Set defines a set of int64 flag with specified name, alias, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Int64Slice added in v0.2.0

func (f *FlagSet) Int64Slice(p *[]int64, name, alias string, value []int64, sep, usage string)

Int64Slice defines a slice of int64 flag with specified name, alias, default value, separator, and usage string. The argument p points to an int64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Int64Slice panics on empty separator.

func (*FlagSet) IntSet added in v0.2.0

func (f *FlagSet) IntSet(p *map[int]struct{}, name, alias string, value map[int]struct{}, usage string)

IntSet defines a set of int flag with specified name, alias, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) IntSlice added in v0.2.0

func (f *FlagSet) IntSlice(p *[]int, name, alias string, value []int, sep, usage string)

IntSlice defines a slice of int flag with specified name, alias, default value, separator, and usage string. The argument p points to an int variable in which to store the value of the flag. Empty string for alias means no alias will be created. IntSlice panics on empty separator.

func (*FlagSet) IsParsed added in v0.2.0

func (f *FlagSet) IsParsed() bool

func (*FlagSet) Lookup

func (f *FlagSet) Lookup(name string) *flag.Flag

func (*FlagSet) NArg

func (f *FlagSet) NArg() int

func (*FlagSet) NFlag

func (f *FlagSet) NFlag() int

func (*FlagSet) Parse

func (f *FlagSet) Parse(arguments []string) error

func (*FlagSet) PrintDefaults

func (f *FlagSet) PrintDefaults()

PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set.

func (*FlagSet) Regexp added in v0.5.0

func (f *FlagSet) Regexp(p *regexp.Regexp, name, alias string, value string, usage string)

BoolSlice defines a slice of bool flag with specified name, alias, default value, separator, and usage string. Duration defines a time.Duration flag with specified name, alias, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. Empty string for alias means no alias will be created. Until https://github.com/golang/go/issues/46159 is implemented.

func (*FlagSet) Set

func (f *FlagSet) Set(name, value string) error

func (*FlagSet) String

func (f *FlagSet) String(p *string, name, alias, value, usage string)

String defines a string flag with specified name, alias, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) StringSet added in v0.2.0

func (f *FlagSet) StringSet(p *map[string]struct{}, name, alias string, value map[string]struct{}, usage string)

StringSet defines a set of string flag with specified name, alias, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) StringSlice added in v0.2.0

func (f *FlagSet) StringSlice(p *[]string, name, alias string, value []string, sep, usage string)

StringSlice defines a slice of string flag with specified name, alias, default value, separator, and usage string. The argument p points to a string variable in which to store the value of the flag. Empty string for alias means no alias will be created. StringSlice panics on empty separator.

func (*FlagSet) Text added in v0.2.1

func (f *FlagSet) Text(p encoding.TextUnmarshaler, name, alias string, value encoding.TextMarshaler, usage string)

Text defines a flag with the specified name and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p. Empty string for alias means no alias will be created.

func (*FlagSet) Uint

func (f *FlagSet) Uint(p *uint, name, alias string, value uint, usage string)

Uint defines a uint flag with specified name, alias, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Uint64

func (f *FlagSet) Uint64(p *uint64, name, alias string, value uint64, usage string)

Uint64 defines a uint64 flag with specified name, alias, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Uint64Set added in v0.2.0

func (f *FlagSet) Uint64Set(p *map[uint64]struct{}, name, alias string, value map[uint64]struct{}, usage string)

Uint64Set defines a set of uint64 flag with specified name, alias, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) Uint64Slice added in v0.2.0

func (f *FlagSet) Uint64Slice(p *[]uint64, name, alias string, value []uint64, sep, usage string)

Uint64Slice defines a slice of uint64 flag with specified name, alias, default value, separator, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. Empty string for alias means no alias will be created. Uint64Slice panics on empty separator.

func (*FlagSet) UintSet added in v0.2.0

func (f *FlagSet) UintSet(p *map[uint]struct{}, name, alias string, value map[uint]struct{}, usage string)

UintSet defines a set of uint flag with specified name, alias, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created.

func (*FlagSet) UintSlice added in v0.2.0

func (f *FlagSet) UintSlice(p *[]uint, name, alias string, value []uint, sep, usage string)

UintSlice defines a slice of uint flag with specified name, alias, default value, separator, and usage string. The argument p points to a uint variable in which to store the value of the flag. Empty string for alias means no alias will be created. UintSlice panics on empty separator.

func (*FlagSet) Var

func (f *FlagSet) Var(value flag.Value, name, alias, usage string)

Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice. Empty string for alias means no alias will be created.

func (*FlagSet) Visit

func (f *FlagSet) Visit(fn func(*flag.Flag))

func (*FlagSet) VisitAll

func (f *FlagSet) VisitAll(fn func(*flag.Flag))

type Getter added in v0.5.0

type Getter = flag.Getter

type SetOfDuration added in v0.2.0

type SetOfDuration map[time.Duration]struct{}

func (*SetOfDuration) Set added in v0.2.0

func (sd *SetOfDuration) Set(v string) error

Set is flag.Value.Set

func (*SetOfDuration) String added in v0.2.0

func (sd *SetOfDuration) String() string

type SetOfFloat64 added in v0.2.0

type SetOfFloat64 map[float64]struct{}

func (*SetOfFloat64) Set added in v0.2.0

func (sf *SetOfFloat64) Set(v string) error

Set is flag.Value.Set

func (*SetOfFloat64) String added in v0.2.0

func (sf *SetOfFloat64) String() string

type SetOfInt added in v0.2.0

type SetOfInt map[int]struct{}

func (*SetOfInt) Set added in v0.2.0

func (si *SetOfInt) Set(v string) error

Set is flag.Value.Set

func (*SetOfInt) String added in v0.2.0

func (si *SetOfInt) String() string

type SetOfInt64 added in v0.2.0

type SetOfInt64 map[int64]struct{}

func (*SetOfInt64) Set added in v0.2.0

func (si *SetOfInt64) Set(v string) error

Set is flag.Value.Set

func (*SetOfInt64) String added in v0.2.0

func (si *SetOfInt64) String() string

type SetOfString added in v0.2.0

type SetOfString map[string]struct{}

func (*SetOfString) Set added in v0.2.0

func (ss *SetOfString) Set(v string) error

Set is flag.Value.Set TODO(cristaloleg): how to configure separator? , \t |

func (*SetOfString) String added in v0.2.0

func (ss *SetOfString) String() string

type SetOfUint added in v0.2.0

type SetOfUint map[uint]struct{}

func (*SetOfUint) Set added in v0.2.0

func (su *SetOfUint) Set(v string) error

Set is flag.Value.Set

func (*SetOfUint) String added in v0.2.0

func (su *SetOfUint) String() string

type SetOfUint64 added in v0.2.0

type SetOfUint64 map[uint64]struct{}

func (*SetOfUint64) Set added in v0.2.0

func (su *SetOfUint64) Set(v string) error

Set is flag.Value.Set

func (*SetOfUint64) String added in v0.2.0

func (su *SetOfUint64) String() string

type Value added in v0.5.0

type Value = flag.Value

Jump to

Keyboard shortcuts

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