Documentation
¶
Overview ¶
Package flagvar provides flag argument types for the standard `flag` package. All types implement `flag.Value`, and can be passed to `flag.Var()`.
Example ¶
package main
import (
"flag"
"fmt"
"github.com/sgreben/flagvar"
)
func main() {
var (
fruit = flagvar.Enum{Choices: []string{"apple", "banana"}}
urls flagvar.URLs
settings flagvar.AssignmentsMap
)
fs := flag.FlagSet{}
fs.Var(&fruit, "fruit", "a fruit")
fs.Var(&urls, "url", "a URL")
fs.Var(&settings, "set", "set key=value")
fs.Parse([]string{
"-fruit", "apple",
"-url", "https://github.com/sgreben/flagvar",
"-set", "hello=world",
})
fmt.Println("fruit:", fruit.Value)
fmt.Println("urls:", urls.Values)
for key, value := range settings.Values {
fmt.Printf("settings: '%s' is set to '%s'\n", key, value)
}
}
Output: fruit: apple urls: [https://github.com/sgreben/flagvar] settings: 'hello' is set to 'world'
Index ¶
- type Alternative
- type Assignment
- type Assignments
- type AssignmentsMap
- type CIDR
- type CIDRs
- type CIDRsCSV
- type Enum
- type EnumSet
- type EnumSetCSV
- type Enums
- type EnumsCSV
- type File
- type Files
- type Float
- type Floats
- type FloatsCSV
- type Glob
- type Globs
- type IP
- type IPs
- type IPsCSV
- type Ints
- type IntsCSV
- type JSON
- type JSONs
- type Regexp
- type Regexps
- type StringSet
- type StringSetCSV
- type Strings
- type TCPAddr
- type TCPAddrs
- type TCPAddrsCSV
- type Template
- type TemplateFile
- type Templates
- type Time
- type TimeFormat
- type Times
- type UDPAddr
- type UDPAddrs
- type UDPAddrsCSV
- type URL
- type URLs
- type UnixAddr
- type UnixAddrs
- type UnixAddrsCSV
- type Wrap
- type WrapCSV
- type WrapFunc
- type WrapPointer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Alternative ¶
Alternative tries to parse the argument using `Either`, and if that fails, using `Or`. `EitherOk` is true if the first attempt succeed.
func (*Alternative) Help ¶
func (fv *Alternative) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*Alternative) String ¶
func (fv *Alternative) String() string
type Assignment ¶
Assignment is a `flag.Value` for `KEY=VALUE` arguments. The value of the `Separator` field is used instead of `"="` when set.
func (*Assignment) Help ¶
func (fv *Assignment) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*Assignment) String ¶
func (fv *Assignment) String() string
type Assignments ¶
type Assignments struct {
Separator string
Values []struct {
Key string
Value string
}
Texts []string
}
Assignments is a `flag.Value` for `KEY=VALUE` arguments. The value of the `Separator` field is used instead of `"="` when set.
func (*Assignments) Help ¶
func (fv *Assignments) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*Assignments) String ¶
func (fv *Assignments) String() string
type AssignmentsMap ¶
AssignmentsMap is a `flag.Value` for `KEY=VALUE` arguments. The value of the `Separator` field is used instead of `"="` when set.
func (*AssignmentsMap) Help ¶
func (fv *AssignmentsMap) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*AssignmentsMap) String ¶
func (fv *AssignmentsMap) String() string
type CIDR ¶
CIDR is a `flag.Value` for CIDR notation IP address and prefix length arguments.
type CIDRs ¶
CIDRs is a `flag.Value` for CIDR notation IP address and prefix length arguments.
type CIDRsCSV ¶
type CIDRsCSV struct {
Separator string
Accumulate bool
Values []struct {
IPNet *net.IPNet
IP net.IP
}
Texts []string
}
CIDRsCSV is a `flag.Value` for CIDR notation IP address and prefix length arguments. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set.
type Enum ¶
Enum is a `flag.Value` for one-of-a-fixed-set string arguments. The value of the `Choices` field defines the valid choices. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
Example ¶
package main
import (
"flag"
"fmt"
"github.com/sgreben/flagvar"
)
func main() {
mode := flagvar.Enum{Choices: []string{"fast", "exact"}}
fruit := flagvar.Enums{Choices: []string{"apple", "pear"}}
letters := flagvar.EnumsCSV{
Choices: []string{"a", "A", "b", "B"},
CaseSensitive: true,
}
levels := flagvar.EnumSetCSV{
Choices: []string{"debug", "info", "warn", "error"},
Accumulate: true,
}
var fs flag.FlagSet
fs.Var(&mode, "mode", "set a mode")
fs.Var(&fruit, "fruit", "add a fruit")
fs.Var(&letters, "letters", "select some letters")
fs.Var(&levels, "levels", "enable log levels")
fs.Parse([]string{
"-fruit", "Apple",
"-fruit", "apple",
"-fruit", "PEAR",
"-letters", "b,A,a",
"-levels", "debug",
"-levels", "debug,info,error",
"-mode", "fast",
"-mode", "exact",
})
fmt.Println("fruit:", fruit.Values)
fmt.Println("letters:", letters.Values)
fmt.Println("levels:", levels.Values())
fmt.Println("mode:", mode.Value)
}
Output: fruit: [apple apple pear] letters: [b A a] levels: [debug error info] mode: exact
type EnumSet ¶
EnumSet is a `flag.Value` for one-of-a-fixed-set string arguments. Only distinct values are returned. The value of the `Choices` field defines the valid choices. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type EnumSetCSV ¶
type EnumSetCSV struct {
Choices []string
Separator string
Accumulate bool
CaseSensitive bool
Value map[string]bool
Texts []string
}
EnumSetCSV is a `flag.Value` for comma-separated enum arguments. Only distinct values are returned. The value of the `Choices` field defines the valid choices. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
func (*EnumSetCSV) Help ¶
func (fv *EnumSetCSV) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*EnumSetCSV) String ¶
func (fv *EnumSetCSV) String() string
func (*EnumSetCSV) Values ¶
func (fv *EnumSetCSV) Values() (out []string)
Values returns a string slice of specified values.
type Enums ¶
Enums is a `flag.Value` for one-of-a-fixed-set string arguments. The value of the `Choices` field defines the valid choices. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type EnumsCSV ¶
type EnumsCSV struct {
Choices []string
Separator string
Accumulate bool
CaseSensitive bool
Values []string
Texts []string
}
EnumsCSV is a `flag.Value` for comma-separated enum arguments. The value of the `Choices` field defines the valid choices. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type File ¶
File is a `flag.Value` for file path arguments. By default, any errors from os.Stat are returned. Alternatively, the value of the `Validate` field is used as a validator when specified.
type Files ¶
Files is a `flag.Value` for file path arguments. By default, any errors from os.Stat are returned. Alternatively, the value of the `Validate` field is used as a validator when specified.
type Float ¶
Float is a `flag.Value` for a float argument. The `BitSize` field is used for parsing when set.
type Floats ¶
Floats is a `flag.Value` for float arguments. The `BitSize` field is used for parsing when set.
type FloatsCSV ¶
type FloatsCSV struct {
BitSize int
Separator string
Accumulate bool
Values []float64
Texts []string
}
FloatsCSV is a `flag.Value` for comma-separated `float` arguments. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `BitSize` fields are used for parsing when set. The `Separator` field is used instead of the comma when set.
type Glob ¶
Glob is a `flag.Value` for glob syntax arguments. By default, `filepath.Separator` is used as a separator. If `Separators` is non-nil, its elements are used as separators. To have no separators, set `Separators` to a (non-nil) pointer to an empty slice.
type Globs ¶
Globs is a `flag.Value` for glob syntax arguments. By default, `filepath.Separator` is used as a separator. If `Separators` is non-nil, its elements are used as separators. To have no separators, set `Separators` to a (non-nil) pointer to an empty slice.
type IP ¶
IP is a `flag.Value` for IP addresses.
type IPs ¶
IPs is a `flag.Value` for IP addresses.
type IPsCSV ¶
IPsCSV is a `flag.Value` for IP addresses. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set.
type Ints ¶
Ints is a `flag.Value` for `int` arguments. The `Base` and `BitSize` fields are used for parsing when set.
type IntsCSV ¶
type IntsCSV struct {
Base int
BitSize int
Separator string
Accumulate bool
Values []int64
Texts []string
}
IntsCSV is a `flag.Value` for comma-separated `int` arguments. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Base` and `BitSize` fields are used for parsing when set. The `Separator` field is used instead of the comma when set.
type JSON ¶
type JSON struct {
Value interface{}
Text string
}
JSON is a `flag.Value` for JSON arguments.
type JSONs ¶
type JSONs struct {
Value func() interface{}
Values []interface{}
Texts []string
}
JSONs is a `flag.Value` for JSON arguments. If non-nil, the `Value` field is used to generate template values.
type Regexp ¶
Regexp is a `flag.Value` for regular expression arguments. If `POSIX` is set to true, `regexp.CompilePOSIX` is used instead of `regexp.Compile`.
type Regexps ¶
Regexps is a `flag.Value` for regular expression arguments.
type StringSet ¶
StringSet is a `flag.Value` for `string` arguments. Only distinct values are returned.
type StringSetCSV ¶
StringSetCSV is a `flag.Value` for comma-separated string arguments. If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set. If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
func (*StringSetCSV) Help ¶
func (fv *StringSetCSV) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*StringSetCSV) String ¶
func (fv *StringSetCSV) String() string
type Strings ¶
type Strings struct {
Values []string
}
Strings is a `flag.Value` for `string` arguments.
type TCPAddr ¶
TCPAddr is a `flag.Value` for TCP addresses. The `Network` field is used if set, otherwise "tcp".
type TCPAddrs ¶
TCPAddrs is a `flag.Value` for TCPAddr addresses. The `Network` field is used if set, otherwise "tcp".
type TCPAddrsCSV ¶
type TCPAddrsCSV struct {
Network string
Separator string
Accumulate bool
Values []*net.TCPAddr
Texts []string
}
TCPAddrsCSV is a `flag.Value` for TCPAddr addresses. The `Network` field is used if set, otherwise "tcp". If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set.
func (*TCPAddrsCSV) Help ¶
func (fv *TCPAddrsCSV) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*TCPAddrsCSV) String ¶
func (fv *TCPAddrsCSV) String() string
type Template ¶
Template is a `flag.Value` for `text.Template` arguments. The value of the `Root` field is used as a root template when specified.
Example ¶
package main
import (
"flag"
"os"
"strings"
"text/template"
"github.com/sgreben/flagvar"
)
func main() {
fv := flagvar.Template{
Root: template.New("example").Funcs(template.FuncMap{
"toUpper": func(s string) string {
return strings.ToUpper(s)
},
}),
}
var fs flag.FlagSet
fs.Var(&fv, "template", "")
fs.Parse([]string{"-template", `{{ toUpper "hello, world!" }}`})
fv.Value.Execute(os.Stdout, nil)
}
Output: HELLO, WORLD!
type TemplateFile ¶
Template is a `flag.Value` for `text.Template` arguments. The value of the `Root` field is used as a root template when specified. The value specified on the command line is the path to the template
func (*TemplateFile) Help ¶
func (fv *TemplateFile) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*TemplateFile) String ¶
func (fv *TemplateFile) String() string
type Templates ¶
Templates is a `flag.Value` for `text.Template` arguments. The value of the `Root` field is used as a root template when specified.
type Time ¶
Time is a `flag.Value` for `time.Time` arguments. The value of the `Layout` field is used for parsing when specified. Otherwise, `time.RFC3339` is used.
type TimeFormat ¶
TimeFormat is a `flag.Value` for `time.Time` formats.
func (*TimeFormat) Help ¶
func (fv *TimeFormat) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*TimeFormat) String ¶
func (fv *TimeFormat) String() string
type Times ¶
Times is a `flag.Value` for `time.Time` arguments. The value of the `Layout` field is used for parsing when specified. Otherwise, `time.RFC3339` is used.
type UDPAddr ¶
UDPAddr is a `flag.Value` for UDP addresses. The `Network` field is used if set, otherwise "udp".
type UDPAddrs ¶
UDPAddrs is a `flag.Value` for UDPAddr addresses. The `Network` field is used if set, otherwise "udp".
type UDPAddrsCSV ¶
type UDPAddrsCSV struct {
Network string
Separator string
Accumulate bool
Values []*net.UDPAddr
Texts []string
}
UDPAddrsCSV is a `flag.Value` for UDPAddr addresses. The `Network` field is used if set, otherwise "udp". If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set.
func (*UDPAddrsCSV) Help ¶
func (fv *UDPAddrsCSV) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*UDPAddrsCSV) String ¶
func (fv *UDPAddrsCSV) String() string
type URL ¶
URL is a `flag.Value` for `url.URL` arguments.
type URLs ¶
URLs is a `flag.Value` for `url.URL` arguments.
type UnixAddr ¶
UnixAddr is a `flag.Value` for Unix addresses. The `Network` field is used if set, otherwise "unix".
type UnixAddrs ¶
UnixAddrs is a `flag.Value` for UnixAddr addresses. The `Network` field is used if set, otherwise "unix".
type UnixAddrsCSV ¶
type UnixAddrsCSV struct {
Network string
Separator string
Accumulate bool
Values []*net.UnixAddr
Texts []string
}
UnixAddrsCSV is a `flag.Value` for UnixAddr addresses. The `Network` field is used if set, otherwise "unix". If `Accumulate` is set, the values of all instances of the flag are accumulated. The `Separator` field is used instead of the comma when set.
func (*UnixAddrsCSV) Help ¶
func (fv *UnixAddrsCSV) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (*UnixAddrsCSV) String ¶
func (fv *UnixAddrsCSV) String() string
type Wrap ¶
Wrap wraps a `flag.Value` and calls `Updated` each time the underlying value is set.
type WrapCSV ¶
type WrapCSV struct {
Value flag.Value
Separator string
UpdatedOne func()
UpdatedAll func()
StringFunc func() string
}
WrapCSV wraps a `flag.Value` and calls `UpdatedOne` after each single value and `UpdatedAll` after each CSV batch. The `Separator` field is used instead of the comma when set.
type WrapFunc ¶
WrapFunc wraps a nullary function returning a `flag.Value` This can be used to switch between different argument parsers.
type WrapPointer ¶
WrapPointer wraps a pointer to a `flag.Value` This can be used to switch between different argument parsers.
func (*WrapPointer) Help ¶
func (fv *WrapPointer) Help() string
Help returns a string suitable for inclusion in a flag help message.
func (WrapPointer) String ¶
func (fv WrapPointer) String() string