flax

package module
v0.0.0-...-44db93b Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: BSD-3-Clause Imports: 9 Imported by: 5

README

flax

GoDoc

Package flax implements a helper for attaching flags to the fields of struct values.

Documentation

Overview

Package flax implements a helper for attaching flags to the fields of struct values.

Example
package main

import (
	"flag"
	"fmt"

	"github.com/creachadair/flax"
)

var flags struct {
	Debug   bool    `flag:"debug,default=true,Enable debugging output"`
	Text    string  `flag:"text,default=OK,Text to display"`
	Rate    float64 `flag:"rate,default=0.1,Rate of increase"`
	Ignored int
}

func init() { flax.MustBind(flag.CommandLine, &flags) }

func main() {
	flag.Parse()

	if flags.Debug {
		fmt.Println(flags.Text, flags.Rate)
	}
	fmt.Println(flags.Ignored)
}
Output:

OK 0.1
0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustBind

func MustBind(fs *flag.FlagSet, v any)

MustBind binds the flaggable fields of v to fs, or panics. The concrete type of v must be a pointer to a value of struct type. This function is intended for use in program initialization; callers who need to check errors should call Bind or Check.

func MustBindAll

func MustBindAll(fs *flag.FlagSet, vs ...any)

MustBindAll is shorthand for calling MustBind(fs, v) for each v in vs.

Types

type Field

type Field struct {
	Name, Usage string // name and usage text (required)
	// contains filtered or unexported fields
}

A Field records information about a single flaggable field in a struct type. The caller can modify the Name and Usage fields if desired before binding the flag to a FlagSet.

func (*Field) Bind

func (fi *Field) Bind(fs *flag.FlagSet)

Bind registers the field described by f in the given flag set.

type Fields

type Fields []*Field

Fields records information about the flaggable fields of a struct type. Use the Bind method to attach flags to the corresponding fields.

func Check

func Check(v any) (Fields, error)

Check constructs information about the flaggable fields of v, whose concrete type must be a pointer to a value of struct type.

Check reports an error if v has the wrong type, or if it does not define any flaggable fields. An exported field of v is flaggable if it is of a compatible type and has a struct tag with the following form:

flag:"name[,default=V],Usage string"

The name and usage string are required. Unexported fields and fields without a flag tag are ignored. If V contains commas, enclose it in 'single quotes', for example:

flag:"name,default='a, b',Usage string"`

To escape a quote, double it ("”"). If the default value begins with "$", it is interpreted as the name of an environment variable to read for the default. Double the "$" to escape this interpretation.

If the default value is "*", it means to use the existing value of the target field as the default, rather than a zero. Use "**" to escape this meaning to get a literal star.

Compatible types include bool, float64, int, int64, string, time.Duration, uint, and uint64, as well as any type implementing the flag.Value interface or the encoding.TextMarshaler and encoding.TextUnmarshaler interfaces.

func MustCheck

func MustCheck(v any) Fields

MustCheck constructs a Fields value from the flaggable fields of v, or panics. This function is intended for use in program initialization; callers who need to check errors should call Check directly.

func (Fields) Bind

func (f Fields) Bind(fs *flag.FlagSet)

Bind attaches the flags defined by f to the given flag set.

func (Fields) Flag

func (f Fields) Flag(s string) *Field

Flag returns the first entry in f whose flag name matches s, or nil if no such entry exists.

Jump to

Keyboard shortcuts

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