enumflag

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: BSD-3-Clause Imports: 2 Imported by: 1

Documentation

Overview

Package enumflag defines a flag.Value implementation that accepts one of a specified collection of string keys. Values are compared without respect to case, so that "foo" and "Foo" are accepted as equivalent to "FOO".

Example:

import (
  "flag"

  "github.com/creachadair/goflags/enumflag"
)

// The first enumerated value is the default.
var color = enumflag.New("", "red", "orange", "yellow", "green", "blue")
func init() {
  flag.Var(color, "color", color.Help("What color to paint the bikeshed"))
}
Example
package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/creachadair/goflags/enumflag"
)

func main() {
	fs := flag.NewFlagSet("example", flag.ContinueOnError)

	var feature = enumflag.New("auto", "on", "off")
	fs.Var(feature, "feature", feature.Help("Enable the new behaviour"))

	if err := fs.Parse([]string{"-feature", "off"}); err != nil {
		log.Fatalf("Parse: %v", err)
	}

	fmt.Printf("Chose index %d (%q)\n", feature.Index(), feature.Key())
}
Output:

Chose index 2 ("off")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Value

type Value struct {
	// contains filtered or unexported fields
}

A Value represents an enumeration of string values. A pointer to a Value satisfies the flag.Value interface. Use the Key method to recover the currently-selected value of the enumeration.

func New

func New(defaultKey string, otherKeys ...string) *Value

New returns a *Value for the specified enumerators, where defaultKey is the default value and otherKeys are additional options. The index of a selected key reflects its position in the order given to this function, so that if:

v := enumflag.New("a", "b", "c", "d")

then the index of "a" is 0, "b" is 1, "c" is 2, "d" is 3. The default key is always stored at index 0.

func (Value) Get

func (v Value) Get() any

Get satisfies the flag.Getter interface. The concrete value is the the string of the current key.

func (Value) Help

func (v Value) Help(h string) string

Help concatenates a human-readable string summarizing the legal values of v to h, for use in generating a documentation string.

func (Value) Index

func (v Value) Index() int

Index returns the currently-selected index in the enumeration. The order of keys reflects the original order in which they were passed to the constructor, so index 0 is the default value.

func (Value) Key

func (v Value) Key() string

Key returns the currently-selected key in the enumeration. The original spelling of the selected value is returned, as given to the constructor, not the value as parsed.

func (*Value) Set

func (v *Value) Set(s string) error

Set satisfies part of the flag.Value interface.

func (Value) String

func (v Value) String() string

String satisfies part of the flag.Value interface.

Jump to

Keyboard shortcuts

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