Documentation ¶
Overview ¶
Package opt provides exclusive access and parsed input of generic options.
Example (Struct_init) ¶
text, err := json.MarshalIndent(StructExample{}, "", " ") if err != nil { fmt.Println(err) } else { os.Stdout.Write(text) }
Output: { "Scalar": { "Bool": false, "String": "", "Int": 0, "Float": 0, "Addr": "invalid IP", "AddrPort": "invalid AddrPort", "Prefix": "invalid Prefix", "Duration": "0s", "URL": "" }, "Slice": { "Ints": [], "Floats": [], "Strings": [], "Prefixes": [], "Structs": null } }
Example (Struct_toml) ¶
var x StructExample _, err := toml.Decode(` [[opt]] [scalar] bool = true string = "hello world" int = 789 float = 7.89 addr = "192.168.2.2" addr_port = "192.168.2.2:80" prefix = "192.168.2.2/24" duration = "15s" url = "https://golang.org/pkg/flag" [slice] ints = [ 3, 2, 1 ] floats = [ 3.3, 2.2, 1.1 ] strings = [ "hello world", "bonjour le monde", "こんにちは世界", ] prefixes = [ "192.168.1.1/32", "192.168.1.2/32", "192.168.1.3/32", ] [[slice.structs]] number = 123 string = "hello world" [[slice.structs]] number = 456 string = "bonjour le monde" [[slice.structs]] number = 789 string = "こんにちは世界" `, &x) if err != nil { fmt.Println(err) return } text, err := json.MarshalIndent(&x, "", " ") if err != nil { fmt.Println(err) } else { os.Stdout.Write(text) }
Output: { "Scalar": { "Bool": true, "String": "hello world", "Int": 789, "Float": 7.89, "Addr": "192.168.2.2", "AddrPort": "192.168.2.2:80", "Prefix": "192.168.2.2/24", "Duration": "15s", "URL": "https://golang.org/pkg/flag" }, "Slice": { "Ints": [ 3, 2, 1 ], "Floats": [ 3.3, 2.2, 1.1 ], "Strings": [ "hello world", "bonjour le monde", "こんにちは世界" ], "Prefixes": [ "192.168.1.1/32", "192.168.1.2/32", "192.168.1.3/32" ], "Structs": [ { "Number": 123, "String": "hello world" }, { "Number": 456, "String": "bonjour le monde" }, { "Number": 789, "String": "こんにちは世界" } ] } }
Example (Struct_yaml) ¶
var x StructExample err := yaml.Unmarshal([]byte(` scalar: bool: true string: hello world int: 789 float: 7.89 addr: 192.168.2.2 addr_port: 192.168.2.2:80 prefix: 192.168.2.2/24 duration: "55s" url: https://golang.org/pkg/flag slice: ints: - 3 - 2 - 1 floats: - 3.3 - 2.2 - 1.1 strings: - hello world - bonjour le monde - こんにちは世界 prefixes: - 192.168.1.1/32 - 192.168.1.2/32 - 192.168.1.3/32 structs: - number: 123 string: hello world - number: 456 string: bonjour le monde - number: 789 string: こんにちは世界 `), &x) if err != nil { fmt.Println(err) return } text, err := yaml.Marshal(&x) if err != nil { fmt.Println(err) } else { os.Stdout.Write(text) }
Output: scalar: bool: true string: hello world int: 789 float: 7.89 addr: 192.168.2.2 addr_port: 192.168.2.2:80 prefix: 192.168.2.2/24 duration: 55s url: https://golang.org/pkg/flag slice: ints: - 3 - 2 - 1 floats: - 3.3 - 2.2 - 1.1 strings: - hello world - bonjour le monde - こんにちは世界 prefixes: - 192.168.1.1/32 - 192.168.1.2/32 - 192.168.1.3/32 structs: - number: 123 string: hello world - number: 456 string: bonjour le monde - number: 789 string: こんにちは世界
Index ¶
- func Subscribe(ch chan<- uintptr)
- func Unsubscribe(ch chan<- uintptr)
- type Addr
- type AddrPort
- type AddrPorts
- type Addrs
- type Bool
- func (opt Bool) IsBoolFlag() bool
- func (opt Bool) MarshalJSON() ([]byte, error)
- func (opt Bool) MarshalText() ([]byte, error)
- func (opt Bool) MarshalYAML() (interface{}, error)
- func (opt *Bool) Set(s string) error
- func (opt *Bool) Store(v bool) error
- func (opt Bool) String() string
- func (opt *Bool) UnmarshalJSON(text []byte) error
- func (opt *Bool) UnmarshalText(text []byte) error
- func (opt Bool) Value() bool
- type Duration
- func (opt Duration) MarshalJSON() ([]byte, error)
- func (opt Duration) MarshalText() ([]byte, error)
- func (opt Duration) MarshalYAML() (interface{}, error)
- func (opt *Duration) Set(s string) error
- func (opt *Duration) Store(v time.Duration) error
- func (opt Duration) String() string
- func (opt *Duration) UnmarshalJSON(text []byte) error
- func (opt *Duration) UnmarshalText(text []byte) error
- func (opt Duration) Value() time.Duration
- type Env
- type NetIP
- func (opt NetIP[T]) Format(f fmt.State, verb rune)
- func (opt NetIP[T]) MarshalJSON() ([]byte, error)
- func (opt NetIP[T]) MarshalText() ([]byte, error)
- func (opt NetIP[T]) MarshalYAML() (interface{}, error)
- func (opt *NetIP[T]) Set(s string) error
- func (opt NetIP[T]) String() string
- func (opt *NetIP[T]) UnmarshalJSON(text []byte) error
- func (opt *NetIP[T]) UnmarshalText(text []byte) error
- func (opt NetIP[T]) Value() T
- type NetIPs
- func (opt NetIPs[T]) MarshalJSON() ([]byte, error)
- func (opt NetIPs[T]) MarshalYAML() (interface{}, error)
- func (opt *NetIPs[T]) Store(v []T) error
- func (opt *NetIPs[T]) UnmarshalTOML(input interface{}) error
- func (opt *NetIPs[T]) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (opt NetIPs[T]) Value() []T
- type Number
- func (opt Number[T]) MarshalJSON() ([]byte, error)
- func (opt Number[T]) MarshalText() ([]byte, error)
- func (opt Number[T]) MarshalYAML() (interface{}, error)
- func (opt *Number[T]) Set(s string) error
- func (opt *Number[T]) Store(v T) error
- func (opt Number[T]) String() string
- func (opt *Number[T]) UnmarshalJSON(text []byte) error
- func (opt *Number[T]) UnmarshalText(text []byte) error
- func (opt Number[T]) Value() T
- type Numbers
- func (opt Numbers[T]) MarshalJSON() ([]byte, error)
- func (opt Numbers[T]) MarshalText() ([]byte, error)
- func (opt Numbers[T]) MarshalYAML() (interface{}, error)
- func (opt *Numbers[T]) Store(v []T) error
- func (opt Numbers[T]) String() string
- func (opt *Numbers[T]) UnmarshalTOML(input interface{}) error
- func (opt *Numbers[T]) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (opt Numbers[T]) Value() []T
- type Numeric
- type Prefix
- type Prefixes
- type String
- func (opt String[T]) MarshalJSON() ([]byte, error)
- func (opt String[T]) MarshalText() ([]byte, error)
- func (opt String[T]) MarshalYAML() (interface{}, error)
- func (opt *String[T]) Set(s string) error
- func (opt *String[T]) Store(v T) error
- func (opt String[T]) String() string
- func (opt *String[T]) UnmarshalJSON(text []byte) error
- func (opt *String[T]) UnmarshalText(text []byte) error
- func (opt String[T]) Value() T
- type Strings
- func (opt Strings[T]) MarshalJSON() ([]byte, error)
- func (opt Strings[T]) MarshalText() ([]byte, error)
- func (opt Strings[T]) MarshalYAML() (interface{}, error)
- func (opt *Strings[T]) Store(v []T) error
- func (opt Strings[T]) String() string
- func (opt *Strings[T]) UnmarshalTOML(input interface{}) error
- func (opt *Strings[T]) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (opt Strings[T]) Value() []T
- type Time
- func (opt Time) MarshalJSON() ([]byte, error)
- func (opt Time) MarshalText() ([]byte, error)
- func (opt Time) MarshalYAML() (interface{}, error)
- func (opt *Time) Set(s string) error
- func (opt *Time) Store(v time.Time) error
- func (opt Time) String() string
- func (opt *Time) UnmarshalJSON(text []byte) error
- func (opt *Time) UnmarshalText(text []byte) error
- func (opt Time) Value() time.Time
- type URL
- func (opt URL) MarshalJSON() ([]byte, error)
- func (opt URL) MarshalText() ([]byte, error)
- func (opt URL) MarshalYAML() (interface{}, error)
- func (opt *URL) Set(s string) error
- func (opt URL) String() string
- func (opt *URL) UnmarshalJSON(text []byte) error
- func (opt *URL) UnmarshalText(text []byte) error
- func (opt URL) Value() url.URL
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Subscribe ¶
func Subscribe(ch chan<- uintptr)
Subscribe to option change notifications.
Example ¶
var updates int var n Number[int] pn := uintptr(unsafe.Pointer(&n)) ch := make(chan uintptr, 4) Subscribe(ch) defer Unsubscribe(ch) go func() { defer close(ch) for _, mod := range []int{3, 2, 1} { n.Store(mod) } }() for p := range ch { if p != pn { fmt.Println("mismatch") } else { updates += 1 } } fmt.Print(updates)
Output: 3
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
func (Bool) IsBoolFlag ¶
func (Bool) MarshalJSON ¶
func (Bool) MarshalText ¶
func (Bool) MarshalYAML ¶
func (*Bool) UnmarshalJSON ¶
func (*Bool) UnmarshalText ¶
type Duration ¶
type Duration struct {
// contains filtered or unexported fields
}
func LimitedDuration ¶
Example ¶
fmt.Print(LimitedDuration(3*time.Second, 1*time.Second, 5*time.Second). Store(10 * time.Second))
Output: 10s > max{5s}
func MustParseDuration ¶
func NewDuration ¶
func (Duration) MarshalJSON ¶
func (Duration) MarshalText ¶
func (Duration) MarshalYAML ¶
func (*Duration) UnmarshalJSON ¶
func (*Duration) UnmarshalText ¶
type Env ¶
Associate environment variable names with Opt[T].Set()
Example ¶
var x StructExample env := Env{ "BOOL": x.Scalar.Bool.Set, "STRING": x.Scalar.String.Set, "INT": x.Scalar.Int.Set, "FLOAT": x.Scalar.Float.Set, "DURATION": x.Scalar.Duration.Set, "ADDR": x.Scalar.Addr.Set, "ADDR_PORT": x.Scalar.AddrPort.Set, "PREFIX": x.Scalar.Prefix.Set, "URL": x.Scalar.URL.Set, } err := env.Set( "BOOL", "STRING=hello world", "INT=321", "FLOAT=3.21", "ADDR=10.1.1.1", "ADDR_PORT=10.1.1.1:80", "PREFIX=10.1.1.1/24", "DURATION=5m", "URL=https://golang.org/pkg/flag", ) if err != nil { fmt.Println(err) } else { fmt.Println(x.Scalar.Bool) fmt.Println(x.Scalar.String) fmt.Println(x.Scalar.Int) fmt.Println(x.Scalar.Float) fmt.Println(x.Scalar.Duration) fmt.Println(x.Scalar.Addr) fmt.Println(x.Scalar.AddrPort) fmt.Println(x.Scalar.Prefix) fmt.Println(x.Scalar.URL) }
Output: true hello world 321 3.21 5m0s 10.1.1.1 10.1.1.1:80 10.1.1.1/24 https://golang.org/pkg/flag
type NetIP ¶
type NetIP[T netip.Addr | netip.AddrPort | netip.Prefix] struct { // contains filtered or unexported fields }
func (NetIP[T]) MarshalJSON ¶
func (NetIP[T]) MarshalText ¶
func (NetIP[T]) MarshalYAML ¶
func (*NetIP[T]) UnmarshalJSON ¶
func (*NetIP[T]) UnmarshalText ¶
type NetIPs ¶
type NetIPs[T netip.Addr | netip.AddrPort | netip.Prefix] struct { // contains filtered or unexported fields }
func (NetIPs[T]) MarshalJSON ¶
func (NetIPs[T]) MarshalYAML ¶
func (*NetIPs[T]) UnmarshalTOML ¶
func (*NetIPs[T]) UnmarshalYAML ¶
type Number ¶
type Number[T Numeric] struct { // contains filtered or unexported fields }
func LimitedNumber ¶
Example ¶
fmt.Print(LimitedNumber[uint](100, 100, 200). Store(201))
Output: 201 > max{200}
func (Number[T]) MarshalJSON ¶
func (Number[T]) MarshalText ¶
func (Number[T]) MarshalYAML ¶
func (*Number[T]) UnmarshalJSON ¶
func (*Number[T]) UnmarshalText ¶
type Numbers ¶
type Numbers[T Numeric] struct { // contains filtered or unexported fields }
func NewNumbers ¶
func (Numbers[T]) MarshalJSON ¶
func (Numbers[T]) MarshalText ¶
func (Numbers[T]) MarshalYAML ¶
func (*Numbers[T]) UnmarshalTOML ¶
func (*Numbers[T]) UnmarshalYAML ¶
type String ¶
type String[T ~string] struct { // contains filtered or unexported fields }
func Alias ¶
Alias returns a string option that assures a new string matches either primary name or one of the aliasws before it's update.
Example ¶
fmt.Print(Alias[string]("Thomas", "Tom", "Tommy").Set("Tommey"))
Output: "Tommey" invalid
func (String[T]) MarshalJSON ¶
func (String[T]) MarshalText ¶
func (String[T]) MarshalYAML ¶
func (*String[T]) UnmarshalJSON ¶
func (*String[T]) UnmarshalText ¶
type Strings ¶
type Strings[T ~string] struct { // contains filtered or unexported fields }
func NewStrings ¶
func (Strings[T]) MarshalJSON ¶
func (Strings[T]) MarshalText ¶
func (Strings[T]) MarshalYAML ¶
func (*Strings[T]) UnmarshalTOML ¶
func (*Strings[T]) UnmarshalYAML ¶
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
func LimitedTime ¶
Example ¶
fmt.Print(MustParseLimitedTime( "2006-01-02T15:04:05Z", "2006-01-02T12:00:00Z", "2006-01-02T17:00:00Z", ).UnmarshalText([]byte( "2006-01-02T17:00:01Z")))
Output: too late
func MustParseLimitedTime ¶
func MustParseTime ¶
func (Time) MarshalJSON ¶
func (Time) MarshalText ¶
func (Time) MarshalYAML ¶
func (*Time) UnmarshalJSON ¶
func (*Time) UnmarshalText ¶
type URL ¶
type URL struct {
// contains filtered or unexported fields
}
func MustParseURL ¶
func (URL) MarshalJSON ¶
func (URL) MarshalText ¶
func (URL) MarshalYAML ¶
func (*URL) UnmarshalJSON ¶
func (*URL) UnmarshalText ¶
Click to show internal directories.
Click to hide internal directories.