flagutil

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 6 Imported by: 4

README

flagutil

Collection of common custom types used with Golang's flag package.

See the documentation for details.

License

flagutil is licensed under the MIT license, see the LICENSE file.

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

func EnvOrDefault(envName, defaultVal string) string

EnvOrDefault returns the environment variable value at `envName` if not empty, otherwise it returns `defaultVal`

Types

type Duration

type Duration struct {
	time.Duration
}

func (*Duration) Set

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

func (Duration) String

func (d Duration) String() string

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

func (*Strings) Set

func (s *Strings) Set(str string) error

Set implements the flag.Value interface. It parses the string as a comma-separated string.

func (Strings) String

func (s Strings) String() string

String implements the flag.Value interface. It returns the slice as a comma-separated string.

type URL

type URL struct {
	url.URL
	// contains filtered or unexported fields
}

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

func (u URL) IsValid() bool

IsValid returns true if the URL object is valid, false otherwise. Valid here means it has correctly parsed an URL.

func (*URL) Set

func (u *URL) Set(s string) error

Set implements the flag.Value interface. It parses the string as a url.URL value.

func (URL) String

func (u URL) String() string

String implements the flag.Value interface. It returns the underlying url.URL as a string.

type URLs

type URLs []url.URL

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

func (*URLs) Set

func (u *URLs) Set(s string) error

Set implements the flag.Value interface. It parses the string as a comma-separated string.

func (URLs) String

func (u URLs) String() string

String implements the flag.Value interface. It returns the string representation of each url.URL as a comma-separated string.

Jump to

Keyboard shortcuts

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