envflag

package module
v0.0.0-...-7eea13c Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2015 License: MIT Imports: 4 Imported by: 0

README

envflag

enflag is a lightweight Go library to allow environment variables overriding with command-line flags.

Install

go get github.com/bfontaine/envflag

Example

This example is available in example/main.go:

package main

import (
    "flag"
    "fmt"
    "os"

    "github.com/bfontaine/envflag"
)

func main() {
    envflag.AutoSetup()
    flag.Parse()

    fmt.Printf("The key is '%s'\n", os.Getenv("KEY"))
}

Once compiled, here is how to use it:

$ ./example -e KEY=42
The key is '42'

$ export KEY=43
$ ./example
The key is '43'

$ export KEY=43
$ ./example -e KEY=41
The key is '41'

$ ./example -e KEY=41 -e KEY=abcd
The key is 'abcd'

Documentation

Overview

Package envflag provides a way to override environment variables with flags

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMalformedValue = errors.New("Malformed environment key/value pair, expected <key>=<value>")

ErrMalformedValue is returned by Value.Set if the given key/value pair is not valid.

Functions

func AutoSetup

func AutoSetup()

AutoSetup is a shortcut for Setup("e", "Override environment variables"). It must be called before flag.Parse().

func Setup

func Setup(flagName, usage string)

Setup creates a flag that will be used to override environment variables. It must be called before flag.Parse(). The flag can occur multiple times in the command-line.

Example
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/bfontaine/envflag"
)

func main() {
	envflag.Setup("e", "Override env variables")
	flag.Parse()

	fmt.Printf("The key is '%s'\n", os.Getenv("KEY"))

	// This example will print:
	// - "foobar" if it’s called as ./example -e KEY=foobar regardless of the
	//   environment variable "KEY"
	// - "abc123" if it’s called as ./example AND the environment variable
	//   "KEY" is set to "abc123"
	// - an empty key if no -e flag is passed and the environment variable
	//   "KEY" is empty or doesn’t exist
}
Output:

Types

type Value

type Value struct{}

Value implements the flag.Value interface and can be used as a target for flag.Var to override environment variables. It doesn’t save any value.

func NewValue

func NewValue() *Value

NewValue returns a pointer on a Value

func (*Value) Set

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

Set implements the flag.Value interface

func (*Value) String

func (v *Value) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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