envflag

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: Unlicense Imports: 4 Imported by: 0

README

envflag GoDoc Report card

Minimalist approach to wiring up the standard flag package to the environment.

Usage

func main() {
    listenAddr := flag.String("addr", ":8080", "server listen address")
    flag.Parse()

    envflag.SetPrefix("myapp")
    envflag.Parse()

    fmt.Printf("listenAddr is %q\n", *listenAddr)
}

Rationale

This package connects the standard flag package to the environment with a minimum of ceremony.

12-factor apps store their configuration in the environment. It's also useful, however, to accept command-line flags: they make a program easier to use, and serve as documentation.

Example

package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/dcowgill/envflag"
)

func main() {
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	listenAddr := fs.String("addr", ":8888", "server listen address")
	timeout := fs.Int("timeout", 10, "client timeout in seconds")
	fs.Parse(nil) // simulate no command-line args

	// Normally this would happen outside the program.
	os.Setenv("MYAPP_LISTEN_ADDR", ":9999")
	os.Setenv("MYAPP_TIMEOUT", "42")

	vs := envflag.NewVarSet(fs)
	vs.SetPrefix("myapp")
	vs.RenameFlag("addr", "listen-addr")
	vs.Parse()

	fmt.Println(*listenAddr) // prints ":9999"
	fmt.Println(*timeout)    // prints "42"
}

Documentation

Overview

Package envflag parses command-line flags defined by package flag from the environment.

Usage:

package main

import (
	"flag"
	"fmt"

	"github.com/dcowgill/envflag"
)

func main() {
	listenAddr := flag.String("listen-addr", ":8080", "server listen address")
	flag.Parse()
	envflag.SetPrefix("myapp")
	envflag.Parse()
	fmt.Printf("listenAddr is %q\n", *listenAddr)
}

An environment variable will not override a flag, but will override its default value:

$ go run example.go
listenAddr is ":8080"
$ MYAPP_LISTEN_ADDR=:7070 go run example.go
listenAddr is ":7070"
$ MYAPP_LISTEN_ADDR=:7070 go run example.go -listen-addr=:9090
listenAddr is ":9090"

Flag names are automatically converted to environment variable keys according to the following rules:

  • Non-ASCII runes are omitted.
  • Uppercase letters, digits, and underscores are preserved.
  • Lowercase letters are changed to uppercase.
  • Hyphens are changed to underscores.
  • All other runes are omitted.
  • Prepend an underscore if a variable name would otherwise begin with a digit.

Index

Constants

This section is empty.

Variables

View Source
var CommandLine = NewVarSet(flag.CommandLine)

CommandLine wraps flag.CommandLine, the default set of command-line flags. The top-level functions, such as SetPrefix and Parse, are wrappers for the methods of CommandLine.

Functions

func Parse

func Parse()

Parse sets the value of flags that were not provided on the command-line but are set in the environment.

func RenameFlag

func RenameFlag(old, new string)

RenameFlag modifies a flag name before it is converted to an environment key. The new name will be transformed by the same process as any other flag name.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix specifies a string to prepend to all environment variable keys. An underscore is automatically inserted between the prefix and variable key.

Types

type VarSet

type VarSet struct {
	// LookupEnv is an optional replacement for os.LookupEnv in this VarSet.
	LookupEnv func(key string) (string, bool)
	// contains filtered or unexported fields
}

A VarSet wraps a flag.FlagSet. The zero value of a VarSet should not be used.

A program that uses the top-level functions in package flag should also use the top-level functions in this package. Otherwise, it must wrap each flag.FlagSet with a VarSet to connect it to the environment.

func NewVarSet

func NewVarSet(fs *flag.FlagSet) *VarSet

NewVarSet creates a new VarSet with the specified flag set and error handling property.

func (*VarSet) Parse

func (vs *VarSet) Parse() error

Parse sets the value of flags that were not provided on the command-line but are set in the environment.

func (*VarSet) RenameFlag

func (vs *VarSet) RenameFlag(old, new string)

RenameFlag modifies a flag name before it is converted to an environment key. The new name will be transformed by the same process as any other flag name.

func (*VarSet) SetPrefix

func (vs *VarSet) SetPrefix(prefix string)

SetPrefix specifies a string to prepend to all environment variable keys. An underscore is automatically inserted between the prefix and variable key.

Jump to

Keyboard shortcuts

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