flagext

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2019 License: MIT Imports: 8 Imported by: 43

README

flagext GoDoc Go Report Card

Implementations of the flag.Value interface to extend the flag package

Documentation

Overview

Package flagext implements extensions to the standard flag package in the form of types that implement flag.Value

Index

Examples

Constants

View Source
const (
	// StdIO can be passed to File to set Stdin as the default
	StdIO = "-"
)

Variables

This section is empty.

Functions

func Choice

func Choice(selection *string, choices ...string) flag.Value

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

func File(defaultPath string) Reader

File returns an io.Reader that lazily loads a file set as a flag.Value. Pass StdIO ("-") to read from standard input.

func FileOrURL added in v0.0.2

func FileOrURL(defaultPath string, client *http.Client) Reader

FileOrURL is an io.Reader that lazily opens a URL or file path set as a flag.Value.

func URL added in v0.0.2

func URL(defaultPath string, client *http.Client) Reader

URL returns an io.Reader that lazily loads an HTTP(S) URL set as a flag.Value.

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

func FileWriter(defaultPath string) Writer

FileWriter returns an io.WriteCloser that lazily os.Creates a file set as a flag.Value. Pass StdIO ("-") to write to standard output.

Jump to

Keyboard shortcuts

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