envflag

package module
v0.0.0-...-14c5f9a Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 3 Imported by: 2

README

package envflag

Set flags via environment variables.

Usage

Flags are still defined using the stdlib package "flag". The only change to your code is calling envflag.Parse() in place of flag.Parse().

If your flag wasn't set via command-line argument, an equivalent environment variable will be used.

Precedence is: command-line agrument, environment variable, default.

Example

package main

import (
	"flag"
	"log"

	"github.com/danott/envflag"
)

func main() {
	var i int
	flag.IntVar(&i, "port", 2112, "Run on this port.")
	envflag.Parse()
	log.Printf("port: %v", i)
}

Run your example to see the precedence in action:

go run main.go
go run main.go --port=2113
PORT=2114 go run main.go
PORT=2114 go run main.go --port=2113

Documentation

Overview

Set flags via environment variables.

Flags are still defined using the stdlib package flag. The only change to your code is calling envflag.Parse() in place of flag.Parse().

If your flag wasn't set via command-line argument, an equivalent environment variable will be used.

Precedence is: command-line agrument, environment variable, default.

As an example, you can create a new file (main.go)

package main

import (
	"flag"
	"log"

	"github.com/danott/envflag"
)

func main() {
	var i int
	flag.IntVar(&i, "port", 2112, "Run on this port.")
	envflag.Parse()
	log.Printf("port: %v", i)
}

Run your example to see the precedence in action:

go run main.go
go run main.go --port=2113
PORT=2114 go run main.go
PORT=2114 go run main.go --port=2113

Index

Constants

This section is empty.

Variables

View Source
var EnvPrefix = ""

Configure how flag names are translated to environment variable names by prefixing the flag name.

View Source
var FlagSet = flag.CommandLine

The flag.FlagSet to act on.

Functions

func Environ

func Environ() []string

Identical to os.Environ, but limited to the environment variable equivalents for the flags your program cares about.

func Parse

func Parse()

Define your flags with package flag. Call envflag.Parse() in place of flag.Parse() to set flags via environment variables (if they weren't set via command-line arguments).

Types

This section is empty.

Jump to

Keyboard shortcuts

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