flagvar

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Overview

Package flagvar provides support for managing flag variables by embedding them in structs. A field in a struct can be annotated with a tag that is used to identify it as a variable to be registered with a flag that contains the name of the flag, an initial default value and the usage message. This makes it convenient to colocate flags with related data structures and to avoid large numbers of global variables as are often encountered with complex, multi-level command structures.

NOTE: this package is no longer under active development, the cloudeng.io/cmdutil/flags supercedes it.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpandEnv added in v0.1.9

func ExpandEnv(e string) string

ExpandEnv is like os.ExpandEnv but supports 'pseudo' environment variables that have OS specific handling as follows:

On Windows $HOME and $PATH are replaced by and $HOMEDRIVE:\\$HOMEPATH and $Path respectively. On Windows /'s are replaced with \'s.

func ParseFlagTag

func ParseFlagTag(t string) (name, value, usage string, err error)

ParseFlagTag parses the supplied string into a flag name, default literal value and description components. It is used by CreatenAndRegisterFlagsInStruct to parse the field tags.

The tag format is:

<name>,<default-value>,<usage>

where <name> is the name of the flag, <default-value> is an optional literal default value for the flag and <usage> the detailed description for the flag. <default-value> may be left empty, but <name> and <usage> must be supplied. All fields can be quoted (with ') if they need to contain a comma.

Default values may contain shell variables as per flagvar.ExpandEnv. So $HOME/.configdir may be used for example.

func RegisterFlagsInStruct

func RegisterFlagsInStruct(fs *flag.FlagSet, tag string, structWithFlags interface{}, valueDefaults map[string]interface{}, usageDefaults map[string]string) error

RegisterFlagsInStruct will selectively register fields in the supplied struct as flags of the appropriate type with the supplied flag.FlagSet. Fields are selected if they have tag of the form `cmdline:"name::<literal>,<usage>"` associated with them, as defined by ParseFlagTag above. In addition to literal default values specified in the tag it is possible to provide computed default values via the valuesDefaults, and also defaults that will appear in the usage string for help messages that override the actual default value. The latter is useful for flags that have a default that is system dependent that is not informative in the usage statement. For example --home-dir which should default to /home/user but the usage message would more usefully say --home-dir=$HOME. Both maps are keyed by the name of the flag, not the field.

Embedded (anonymous) structs may be used provided that they are not themselves tagged. For example:

type CommonFlags struct {
  A int `cmdline:"a,,use a"`
  B int `cmdline:"b,,use b"`
}

flagSet := struct{
  CommonFlags
  C bool `cmdline:"c,,use c"`
}

will result in three flags, --a, --b and --c. Note that embedding as a pointer is not supported.

Example
package main

import (
	"flag"
	"fmt"
	"path/filepath"

	"v.io/x/lib/cmd/flagvar"
)

func main() {
	eg := struct {
		A int    `flag:"int-flag,-1,intVar flag"`
		B string `flag:"string-flag,'some,value,with,a,comma',stringVar flag"`
		O int
		H string `flag:"config,$HOME/config,config file in home directotyr"`
	}{
		O: 23,
	}
	flagSet := &flag.FlagSet{}
	err := flagvar.RegisterFlagsInStruct(flagSet, "flag", &eg, nil, nil)
	if err != nil {
		panic(err)
	}
	fmt.Println(eg.A)
	fmt.Println(eg.B)
	flagSet.Parse([]string{"--int-flag=42"})
	fmt.Println(eg.A)
	fmt.Println(eg.B)
	if got, want := eg.H, filepath.Join(flagvar.ExpandEnv("$HOME"), "config"); got != want {
		fmt.Printf("got %v, want %v", got, want)
	}
}
Output:

-1
some,value,with,a,comma
42
some,value,with,a,comma

Types

This section is empty.

Jump to

Keyboard shortcuts

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