Documentation ¶
Overview ¶
Package flagext implements extensions to the standard flag package in the form of types that implement flag.Value
Index ¶
Examples ¶
Constants ¶
const (
// StdIO can be passed to File to set Stdin as the default
StdIO = "-"
)
Variables ¶
This section is empty.
Functions ¶
func Choice ¶
Choice implements flag.Value. Pass directly into flag.Var. flag.Var sets selection to the value of a command line flag if it is among the choices. If the flag value is not among the choices, it returns an error.
Example (BadFlag) ¶
package main import ( "flag" "fmt" "io/ioutil" "github.com/carlmjohnson/flagext" ) func main() { fs := flag.NewFlagSet("ExampleChoice", flag.ContinueOnError) fs.SetOutput(ioutil.Discard) var mode string fs.Var(flagext.Choice(&mode, "a", "b"), "mode", "mode to run") err := fs.Parse([]string{"-mode", "c"}) fmt.Println(err) }
Output: invalid value "c" for flag -mode: "c" not in a, b
Example (DefaultValue) ¶
package main import ( "flag" "fmt" "github.com/carlmjohnson/flagext" ) func main() { fs := flag.NewFlagSet("ExampleChoice", flag.ContinueOnError) mode := "none" fs.Var(flagext.Choice(&mode, "yes", "no"), "mode", "mode to run") fs.Parse([]string{}) fmt.Println(mode) }
Output: none
Example (GoodFlag) ¶
package main import ( "flag" "fmt" "github.com/carlmjohnson/flagext" ) func main() { fs := flag.NewFlagSet("ExampleChoice", flag.ContinueOnError) var mode string fs.Var(flagext.Choice(&mode, "x", "y"), "mode", "mode to run") fs.Parse([]string{"-mode", "x"}) fmt.Println(mode) }
Output: x
Types ¶
type Reader ¶ added in v0.0.2
type Reader interface { io.ReadCloser flag.Getter }
Reader is an io.ReadCloser that can be set as a flag.Value
func File ¶ added in v0.0.2
File returns an io.Reader that lazily loads a file set as a flag.Value. Pass StdIO ("-") to read from standard input.
type Writer ¶ added in v0.0.3
type Writer interface { io.WriteCloser flag.Getter }
Writer is an io.WriteCloser that can be set as a flag.Value
func FileWriter ¶ added in v0.0.3
FileWriter returns an io.WriteCloser that lazily os.Creates a file set as a flag.Value. Pass StdIO ("-") to write to standard output.