flagvar

package module
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2020 License: Unlicense Imports: 15 Imported by: 13

README

flagvar

Test

A collection of CLI argument types for the flag package.

import "github.com/sgreben/flagvar"

Or just copy & paste what you need. It's public domain.

Example

package main

import (
	"flag"
	"fmt"
	"github.com/sgreben/flagvar"
)

var (
	fruit    = flagvar.Enum{Choices: []string{"apple", "banana"}}
	urls     flagvar.URLs
	settings flagvar.Assignments
)

func main() {
	flag.Var(&fruit, "fruit", fmt.Sprintf("set a fruit (%s)", fruit.Help()))
	flag.Var(&urls, "url", "add a URL")
	flag.Var(&settings, "set", fmt.Sprintf("specify a setting (%s)", settings.Help()))
	flag.Parse()
}
$ go run main.go -set abc=xyz -url https://github.com
# no error

$ go run main.go -set abc=xyz -url ://github.com
invalid value "://github.com" for flag -url: parse ://github.com: missing protocol scheme

$ go run main.go -fruit kiwi
invalid value "kiwi" for flag -fruit: "kiwi" must be one of [apple banana]

$ go run main.go -h
Usage:
  -fruit value
        set a fruit (one of [apple banana])
  -set value
        specify a setting (a key/value pair KEY=VALUE)
  -url value
        add a URL

Conventions

  • Pluralized argument types (e.g. Strings, Assignments) can be specified repeatedly, the values are collected in a slice.
  • The resulting value is stored in .Value for singular types and in .Values for plural types
  • The original argument string is stored in .Text for singular types and in .Texts for plural types
  • -Set types (EnumSet, StringSet) de-duplicate provided values.
  • -CSV types (IntsCSV, EnumsCSV) accept comma-separated values and accumulate values across flag instances if their .Accumulate field is set to true.
  • Most types implement interface{ Help() string }, which produces a string suitable for inclusion in a help message.

Types

Here's a compact overview:

flagvar type example CLI arg type of resulting Go value
Alternative
Assignment KEY=VALUE struct{Key,Value}
Assignments KEY=VALUE []struct{Key,Value}
AssignmentsMap KEY=VALUE map[string]string
CIDR 127.0.0.1/24 struct{IPNet,IP}
CIDRs 127.0.0.1/24 []struct{IPNet,IP}
CIDRsCSV 127.0.0.1/16,10.1.2.3/8 []struct{IPNet,IP}
Enum apple string
Enums apple []string
EnumsCSV apple,banana []string
EnumSet apple []string
EnumSetCSV apple,banana []string
File ./README.md string
Files ./README.md []string
Floats 1.234 []float64
FloatsCSV 1.234,5.0 []float64
Glob src/**.js glob.Glob
Globs src/**.js []glob.Glob
Ints 1002 []int64
IntsCSV 123,1002 []int64
IP 127.0.0.1 net.IP
IPs 127.0.0.1 []net.IP
IPsCSV 127.0.0.1,10.1.2.3 []net.IP
JSON '{"a":1}' interface{}
JSONs '{"a":1}' []interface{}
Regexp [a-z]+ *regexp.Regexp
Regexps [a-z]+ []*regexp.Regexp
Strings "xyxy" []string
StringSet "xyxy" []string
StringSetCSV y,x,y []string
TCPAddr 127.0.0.1:10 net.TCPAddr
TCPAddrs 127.0.0.1:10 []net.TCPAddr
TCPAddrsCSV 127.0.0.1:10,:123 []net.TCPAddr
Template "{{.Size}}" *template.Template
Templates "{{.Size}}" []*template.Template
TemplateFile "/path/to/template.file" string
Time "10:30 AM" time.Time
Times "10:30 AM" []time.Time
TimeFormat "RFC3339" string
UDPAddr 127.0.0.1:10 net.UDPAddr
UDPAddrs 127.0.0.1:10 []net.UDPAddr
UDPAddrsCSV 127.0.0.1:10,:123 []net.UDPAddr
UnixAddr /example.sock net.UnixAddr
UnixAddrs /example.sock []net.UnixAddr
UnixAddrsCSV /example.sock,/other.sock []net.UnixAddr
URL https://github.com *url.URL
URLs https://github.com []*url.URL
Wrap
WrapCSV
WrapFunc
WrapPointer

Goals / design principles

  • Help avoid dependencies
    • Self-contained > DRY
    • Explicitly support copy & paste workflow
    • Copyable units should be easy to determine
    • Anonymous structs > shared types
  • "Code-you-own" feeling, even when imported as a package
    • No private fields / methods
    • No magic
    • Simple built-in types used wherever possible
    • Avoid introducing new concepts
  • Support "blind" usage
    • Zero values should be useful
    • Avoid introducing failure cases, handle any combination of parameters gracefully.
    • All "obvious things to try" should work.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alternative

type Alternative struct {
	Either   flag.Value
	Or       flag.Value
	EitherOk bool
}

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) Set

func (fv *Alternative) Set(v string) error

Set is flag.Value.Set

func (*Alternative) String

func (fv *Alternative) String() string

type Assignment

type Assignment struct {
	Separator string

	Value struct {
		Key   string
		Value string
	}
	Text string
}

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) Set

func (fv *Assignment) Set(v string) error

Set is flag.Value.Set

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) Set

func (fv *Assignments) Set(v string) error

Set is flag.Value.Set

func (*Assignments) String

func (fv *Assignments) String() string

type AssignmentsMap

type AssignmentsMap struct {
	Separator string

	Values map[string]string
	Texts  []string
}

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) Set

func (fv *AssignmentsMap) Set(v string) error

Set is flag.Value.Set

func (*AssignmentsMap) String

func (fv *AssignmentsMap) String() string

type CIDR

type CIDR struct {
	Value struct {
		IPNet *net.IPNet
		IP    net.IP
	}
	Text string
}

CIDR is a `flag.Value` for CIDR notation IP address and prefix length arguments.

func (*CIDR) Help

func (fv *CIDR) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*CIDR) Set

func (fv *CIDR) Set(v string) error

Set is flag.Value.Set

func (*CIDR) String

func (fv *CIDR) String() string

type CIDRs

type CIDRs struct {
	Values []struct {
		IPNet *net.IPNet
		IP    net.IP
	}
	Texts []string
}

CIDRs is a `flag.Value` for CIDR notation IP address and prefix length arguments.

func (*CIDRs) Help

func (fv *CIDRs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*CIDRs) Set

func (fv *CIDRs) Set(v string) error

Set is flag.Value.Set

func (*CIDRs) String

func (fv *CIDRs) String() string

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.

func (*CIDRsCSV) Help

func (fv *CIDRsCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*CIDRsCSV) Set

func (fv *CIDRsCSV) Set(v string) error

Set is flag.Value.Set

func (*CIDRsCSV) String

func (fv *CIDRsCSV) String() string

type Enum

type Enum struct {
	Choices       []string
	CaseSensitive bool

	Value string
	Text  string
}

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

func (*Enum) Help

func (fv *Enum) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Enum) Set

func (fv *Enum) Set(v string) error

Set is flag.Value.Set

func (*Enum) String

func (fv *Enum) String() string

type EnumSet

type EnumSet struct {
	Choices       []string
	CaseSensitive bool

	Value map[string]bool
	Texts []string
}

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.

func (*EnumSet) Help

func (fv *EnumSet) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*EnumSet) Set

func (fv *EnumSet) Set(v string) error

Set is flag.Value.Set

func (*EnumSet) String

func (fv *EnumSet) String() string

func (*EnumSet) Values

func (fv *EnumSet) Values() (out []string)

Values returns a string slice of specified values.

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) Set

func (fv *EnumSetCSV) Set(v string) error

Set is flag.Value.Set

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

type Enums struct {
	Choices       []string
	CaseSensitive bool

	Values []string
	Texts  []string
}

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.

func (*Enums) Help

func (fv *Enums) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Enums) Set

func (fv *Enums) Set(v string) error

Set is flag.Value.Set

func (*Enums) String

func (fv *Enums) String() string

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.

func (*EnumsCSV) Help

func (fv *EnumsCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*EnumsCSV) Set

func (fv *EnumsCSV) Set(v string) error

Set is flag.Value.Set

func (*EnumsCSV) String

func (fv *EnumsCSV) String() string

type File

type File struct {
	Validate func(os.FileInfo, error) error

	Value string
}

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.

func (*File) Set

func (fv *File) Set(v string) error

Set is flag.Value.Set

func (*File) String

func (fv *File) String() string

type Files

type Files struct {
	Validate func(os.FileInfo, error) error

	Values []string
}

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.

func (*Files) Set

func (fv *Files) Set(v string) error

Set is flag.Value.Set

func (*Files) String

func (fv *Files) String() string

type Float

type Float struct {
	BitSize int

	Value float64
	Text  string
}

Float is a `flag.Value` for a float argument. The `BitSize` field is used for parsing when set.

func (*Float) Help

func (fv *Float) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Float) Set

func (fv *Float) Set(v string) error

Set is flag.Value.Set

func (*Float) String

func (fv *Float) String() string

type Floats

type Floats struct {
	BitSize int

	Values []float64
	Texts  []string
}

Floats is a `flag.Value` for float arguments. The `BitSize` field is used for parsing when set.

func (*Floats) Help

func (fv *Floats) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Floats) Set

func (fv *Floats) Set(v string) error

Set is flag.Value.Set

func (*Floats) String

func (fv *Floats) String() string

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.

func (*FloatsCSV) Help

func (fv *FloatsCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*FloatsCSV) Set

func (fv *FloatsCSV) Set(v string) error

Set is flag.Value.Set

func (*FloatsCSV) String

func (fv *FloatsCSV) String() string

type Glob

type Glob struct {
	Separators *[]rune

	Value glob.Glob
	Text  string
}

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.

func (*Glob) Help

func (fv *Glob) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Glob) Set

func (fv *Glob) Set(v string) error

Set is flag.Value.Set

func (*Glob) String

func (fv *Glob) String() string

type Globs

type Globs struct {
	Separators *[]rune

	Values []glob.Glob
	Texts  []string
}

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.

func (*Globs) Help

func (fv *Globs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Globs) Set

func (fv *Globs) Set(v string) error

Set is flag.Value.Set

func (*Globs) String

func (fv *Globs) String() string

type IP

type IP struct {
	Value net.IP
	Text  string
}

IP is a `flag.Value` for IP addresses.

func (*IP) Help

func (fv *IP) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*IP) Set

func (fv *IP) Set(v string) error

Set is flag.Value.Set

func (*IP) String

func (fv *IP) String() string

type IPs

type IPs struct {
	Values []net.IP
	Texts  []string
}

IPs is a `flag.Value` for IP addresses.

func (*IPs) Help

func (fv *IPs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*IPs) Set

func (fv *IPs) Set(v string) error

Set is flag.Value.Set

func (*IPs) String

func (fv *IPs) String() string

type IPsCSV

type IPsCSV struct {
	Separator  string
	Accumulate bool

	Values []net.IP
	Texts  []string
}

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.

func (*IPsCSV) Help

func (fv *IPsCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*IPsCSV) Set

func (fv *IPsCSV) Set(v string) error

Set is flag.Value.Set

func (*IPsCSV) String

func (fv *IPsCSV) String() string

type Ints

type Ints struct {
	Base    int
	BitSize int

	Values []int64
	Texts  []string
}

Ints is a `flag.Value` for `int` arguments. The `Base` and `BitSize` fields are used for parsing when set.

func (*Ints) Help

func (fv *Ints) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Ints) Set

func (fv *Ints) Set(v string) error

Set is flag.Value.Set

func (*Ints) String

func (fv *Ints) String() string

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.

func (*IntsCSV) Help

func (fv *IntsCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*IntsCSV) Set

func (fv *IntsCSV) Set(v string) error

Set is flag.Value.Set

func (*IntsCSV) String

func (fv *IntsCSV) String() string

type JSON

type JSON struct {
	Value interface{}
	Text  string
}

JSON is a `flag.Value` for JSON arguments.

func (*JSON) Help

func (fv *JSON) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*JSON) Set

func (fv *JSON) Set(v string) error

Set is flag.Value.Set

func (*JSON) String

func (fv *JSON) String() string

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.

func (*JSONs) Help

func (fv *JSONs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*JSONs) Set

func (fv *JSONs) Set(v string) (err error)

Set is flag.Value.Set

func (*JSONs) String

func (fv *JSONs) String() string

type Regexp

type Regexp struct {
	POSIX bool

	Value *regexp.Regexp
	Text  string
}

Regexp is a `flag.Value` for regular expression arguments. If `POSIX` is set to true, `regexp.CompilePOSIX` is used instead of `regexp.Compile`.

func (*Regexp) Help

func (fv *Regexp) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Regexp) Set

func (fv *Regexp) Set(v string) error

Set is flag.Value.Set

func (*Regexp) String

func (fv *Regexp) String() string

type Regexps

type Regexps struct {
	POSIX bool

	Values []*regexp.Regexp
	Texts  []string
}

Regexps is a `flag.Value` for regular expression arguments.

func (*Regexps) Help

func (fv *Regexps) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Regexps) Set

func (fv *Regexps) Set(v string) error

Set is flag.Value.Set

func (*Regexps) String

func (fv *Regexps) String() string

type StringSet

type StringSet struct {
	Value map[string]bool
}

StringSet is a `flag.Value` for `string` arguments. Only distinct values are returned.

func (*StringSet) Set

func (fv *StringSet) Set(v string) error

Set is flag.Value.Set

func (*StringSet) String

func (fv *StringSet) String() string

func (*StringSet) Values

func (fv *StringSet) Values() (out []string)

Values returns a string slice of specified values.

type StringSetCSV

type StringSetCSV struct {
	Separator  string
	Accumulate bool

	Value  map[string]bool
	Values []string
}

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) Set

func (fv *StringSetCSV) Set(v string) error

Set is flag.Value.Set

func (*StringSetCSV) String

func (fv *StringSetCSV) String() string

type Strings

type Strings struct {
	Values []string
}

Strings is a `flag.Value` for `string` arguments.

func (*Strings) Set

func (fv *Strings) Set(v string) error

Set is flag.Value.Set

func (*Strings) String

func (fv *Strings) String() string

type TCPAddr

type TCPAddr struct {
	Network string

	Value *net.TCPAddr
	Text  string
}

TCPAddr is a `flag.Value` for TCP addresses. The `Network` field is used if set, otherwise "tcp".

func (*TCPAddr) Help

func (fv *TCPAddr) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*TCPAddr) Set

func (fv *TCPAddr) Set(v string) error

Set is flag.Value.Set

func (*TCPAddr) String

func (fv *TCPAddr) String() string

type TCPAddrs

type TCPAddrs struct {
	Network string

	Values []*net.TCPAddr
	Texts  []string
}

TCPAddrs is a `flag.Value` for TCPAddr addresses. The `Network` field is used if set, otherwise "tcp".

func (*TCPAddrs) Help

func (fv *TCPAddrs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*TCPAddrs) Set

func (fv *TCPAddrs) Set(v string) error

Set is flag.Value.Set

func (*TCPAddrs) String

func (fv *TCPAddrs) String() string

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) Set

func (fv *TCPAddrsCSV) Set(v string) error

Set is flag.Value.Set

func (*TCPAddrsCSV) String

func (fv *TCPAddrsCSV) String() string

type Template

type Template struct {
	Root *template.Template

	Value *template.Template
	Text  string
}

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!

func (*Template) Help

func (fv *Template) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Template) Set

func (fv *Template) Set(v string) error

Set is flag.Value.Set

func (*Template) String

func (fv *Template) String() string

type TemplateFile

type TemplateFile struct {
	Root *template.Template

	Value *template.Template
	Text  string
}

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) Set

func (fv *TemplateFile) Set(v string) error

Set is flag.Value.Set

func (*TemplateFile) String

func (fv *TemplateFile) String() string

type Templates

type Templates struct {
	Root *template.Template

	Values []*template.Template
	Texts  []string
}

Templates is a `flag.Value` for `text.Template` arguments. The value of the `Root` field is used as a root template when specified.

func (*Templates) Help

func (fv *Templates) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Templates) Set

func (fv *Templates) Set(v string) error

Set is flag.Value.Set

func (*Templates) String

func (fv *Templates) String() string

type Time

type Time struct {
	Layout string

	Value time.Time
	Text  string
}

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.

func (*Time) Help

func (fv *Time) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Time) Set

func (fv *Time) Set(v string) error

Set is flag.Value.Set

func (*Time) String

func (fv *Time) String() string

type TimeFormat

type TimeFormat struct {
	Value string
	Text  string
}

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) Set

func (fv *TimeFormat) Set(v string) error

Set is flag.Value.Set

func (*TimeFormat) String

func (fv *TimeFormat) String() string

type Times

type Times struct {
	Layout string

	Values []time.Time
	Texts  []string
}

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.

func (*Times) Help

func (fv *Times) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Times) Set

func (fv *Times) Set(v string) error

Set is flag.Value.Set

func (*Times) String

func (fv *Times) String() string

type UDPAddr

type UDPAddr struct {
	Network string

	Value *net.UDPAddr
	Text  string
}

UDPAddr is a `flag.Value` for UDP addresses. The `Network` field is used if set, otherwise "udp".

func (*UDPAddr) Help

func (fv *UDPAddr) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*UDPAddr) Set

func (fv *UDPAddr) Set(v string) error

Set is flag.Value.Set

func (*UDPAddr) String

func (fv *UDPAddr) String() string

type UDPAddrs

type UDPAddrs struct {
	Network string

	Values []*net.UDPAddr
	Texts  []string
}

UDPAddrs is a `flag.Value` for UDPAddr addresses. The `Network` field is used if set, otherwise "udp".

func (*UDPAddrs) Help

func (fv *UDPAddrs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*UDPAddrs) Set

func (fv *UDPAddrs) Set(v string) error

Set is flag.Value.Set

func (*UDPAddrs) String

func (fv *UDPAddrs) String() string

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) Set

func (fv *UDPAddrsCSV) Set(v string) error

Set is flag.Value.Set

func (*UDPAddrsCSV) String

func (fv *UDPAddrsCSV) String() string

type URL

type URL struct {
	Value *url.URL
	Text  string
}

URL is a `flag.Value` for `url.URL` arguments.

func (*URL) Help

func (fv *URL) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*URL) Set

func (fv *URL) Set(v string) error

Set is flag.Value.Set

func (*URL) String

func (fv *URL) String() string

type URLs

type URLs struct {
	Values []*url.URL
	Texts  []string
}

URLs is a `flag.Value` for `url.URL` arguments.

func (*URLs) Help

func (fv *URLs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*URLs) Set

func (fv *URLs) Set(v string) error

Set is flag.Value.Set

func (*URLs) String

func (fv *URLs) String() string

type UnixAddr

type UnixAddr struct {
	Network string

	Value *net.UnixAddr
	Text  string
}

UnixAddr is a `flag.Value` for Unix addresses. The `Network` field is used if set, otherwise "unix".

func (*UnixAddr) Help

func (fv *UnixAddr) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*UnixAddr) Set

func (fv *UnixAddr) Set(v string) error

Set is flag.Value.Set

func (*UnixAddr) String

func (fv *UnixAddr) String() string

type UnixAddrs

type UnixAddrs struct {
	Network string

	Values []*net.UnixAddr
	Texts  []string
}

UnixAddrs is a `flag.Value` for UnixAddr addresses. The `Network` field is used if set, otherwise "unix".

func (*UnixAddrs) Help

func (fv *UnixAddrs) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*UnixAddrs) Set

func (fv *UnixAddrs) Set(v string) error

Set is flag.Value.Set

func (*UnixAddrs) String

func (fv *UnixAddrs) String() string

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) Set

func (fv *UnixAddrsCSV) Set(v string) error

Set is flag.Value.Set

func (*UnixAddrsCSV) String

func (fv *UnixAddrsCSV) String() string

type Wrap

type Wrap struct {
	Value   flag.Value
	Updated func()
}

Wrap wraps a `flag.Value` and calls `Updated` each time the underlying value is set.

func (*Wrap) Help

func (fv *Wrap) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*Wrap) Set

func (fv *Wrap) Set(v string) error

Set is flag.Value.Set

func (*Wrap) String

func (fv *Wrap) String() string

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.

func (*WrapCSV) Help

func (fv *WrapCSV) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (*WrapCSV) Set

func (fv *WrapCSV) Set(v string) error

Set is flag.Value.Set

func (*WrapCSV) String

func (fv *WrapCSV) String() string

type WrapFunc

type WrapFunc func() flag.Value

WrapFunc wraps a nullary function returning a `flag.Value` This can be used to switch between different argument parsers.

func (WrapFunc) Help

func (fv WrapFunc) Help() string

Help returns a string suitable for inclusion in a flag help message.

func (WrapFunc) Set

func (fv WrapFunc) Set(v string) error

Set is flag.Value.Set

func (WrapFunc) String

func (fv WrapFunc) String() string

type WrapPointer

type WrapPointer struct {
	Value *flag.Value
}

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) Set

func (fv *WrapPointer) Set(v string) error

Set is flag.Value.Set

func (WrapPointer) String

func (fv WrapPointer) String() string

Jump to

Keyboard shortcuts

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