Documentation
¶
Overview ¶
Package flagutil provides a collection of types implementing the flag.Value interface.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnvOrDefault ¶
EnvOrDefault returns the environment variable value at `envName` if not empty, otherwise it returns `defaultVal`
Types ¶
type NetworkAddress ¶
type NetworkAddress string
NetworkAddress is a flag.Value that represents a network address. here a network address means anything that can be parsed by `net.SplitHostPort`.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/vrischmann/flagutil" ) func main() { var addr flagutil.NetworkAddress flag.Var(&addr, "h", "Address") os.Args = append(os.Args, "-h", "localhost:4000") flag.Parse() fmt.Println(addr) }
Output: localhost:4000
func (*NetworkAddress) Set ¶
func (a *NetworkAddress) Set(s string) error
Set implements the flag.Value interface. It parses the string using `net.SplitHostPort` for validation.
func (NetworkAddress) String ¶
func (a NetworkAddress) String() string
String implements the flag.Value interface. It returns the slice as a comma-separated string.
type NetworkAddresses ¶
type NetworkAddresses []string
NetworkAddresses is a slice of string that have been validated as valid network addresses. Use it as a flag value when you want to pass a comma-separated list of strings to a flag and have it to be automatically parsed into a slice and validated as valid network addresses.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/vrischmann/flagutil" ) func main() { var addrs flagutil.NetworkAddresses flag.Var(&addrs, "H", "Addresses") os.Args = append(os.Args, "-H", "localhost:4000,localhost:5000") flag.Parse() fmt.Println(addrs[0]) fmt.Println(addrs[1]) fmt.Println(addrs) }
Output: localhost:4000 localhost:5000 localhost:4000,localhost:5000
func (*NetworkAddresses) Set ¶
func (a *NetworkAddresses) Set(s string) error
Set implements the flag.Value interface. It parses the string as a comma-separated string. Additionally, each value is then passed to net.SplitHostPort for validation that it's a correct network address.
func (NetworkAddresses) String ¶
func (a NetworkAddresses) String() string
String implements the flag.Value interface. It returns the slice as a comma-separated string.
func (NetworkAddresses) StringSlice ¶
func (a NetworkAddresses) StringSlice() []string
StringSlice returns the network addresses as a slice of string.
type Strings ¶
type Strings []string
Strings is a slice of string. Use it as a flag value when you want to pass a comma-separated list of strings to a flag and have it to be automatically parsed into a slice.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/vrischmann/flagutil" ) func main() { var strings flagutil.Strings flag.Var(&strings, "s", "Strings") os.Args = append(os.Args, "-s", "foo,bar") flag.Parse() fmt.Println(strings[0]) fmt.Println(strings[1]) fmt.Println(strings) }
Output: foo bar foo,bar
type URL ¶
URL is a wrapper around a url.URL type. Use it as a flag value when you want to validate a flag as a valid URL.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/vrischmann/flagutil" ) func main() { var url flagutil.URL flag.Var(&url, "u", "URL") os.Args = append(os.Args, "-u", "https://google.com") flag.Parse() fmt.Println(url.URL.Scheme) fmt.Println(url.URL.Host) fmt.Println(url.String()) }
Output: https google.com https://google.com
func (URL) IsValid ¶
IsValid returns true if the URL object is valid, false otherwise. Valid here means it has correctly parsed an URL.
type URLs ¶
URLs is a slice of url.URL. Use it as a flag value when you want to pass a comma-separated list of strings to a flag and have it to be automatically parsed into a slice and validated as valid url.URL.
Example ¶
package main import ( "flag" "fmt" "os" "github.com/vrischmann/flagutil" ) func main() { var urls flagutil.URLs flag.Var(&urls, "U", "URLs") os.Args = append(os.Args, "-U", "https://google.com,https://google.de") flag.Parse() fmt.Println(urls[0].String()) fmt.Println(urls[1].String()) }
Output: https://google.com https://google.de