envflag

package module
v0.0.0-...-0e9a860 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2015 License: Apache-2.0 Imports: 6 Imported by: 0

README

envflag

A simple golang tool to set flag via environment variables inspired by Go: Best Practices for Production Environments

Features

  • Set flag via environment variables.
  • Auto mapping environment variables to flag. (e.g. DATABASE_PORT to -database-port)
  • Customizable env - flag mapping support.
  • Min length (default is 3) support in order to avoid parsing short flag.
  • Show environment variable key in usage (-h).
  • Show environment variable value in usage as default value (-h) in order to confirm enviroment settings.

Basic Usage

Just keep it SIMPLE and SIMPLE and SIMPLE!

Use envflag.Parse() instead of flag.Parse().

Here is an example.

package main

import (
    "flag"
    "fmt"

    "github.com/kouhin/envflag"
)

func main() {
    var (
        databaseMasterHost = flag.String("database-master-host", "localhost", "Database master host")
        databaseMasterPort = flag.Int("database-master-port", 3306, "Database master port")
    )
    if err := envflag.Parse(); err != nil {
	    panic(err)
	}
    fmt.Println("RESULT: ", *databaseMasterHost, ":", *databaseMasterPort)
}

Run DATABASE_MASTER_HOST=192.168.0.2 go run main.go -h you will get the following usage:

Usage of XXXX
  -database-master-host="192.168.0.2": [DATABASE_MASTER_HOST] Database master host
  -database-master-port=3306: [DATABASE_MASTER_PORT] Database master port
$go run main.go
RESULT:  localhost : 3306

$export DATABASE_MASTER_HOST=192.168.0.2
$go run main.go
RESULT:  192.168.0.2 : 3306
$go run main.go -database-master-host=192.168.0.3
RESULT:  192.168.0.3 : 3306

Advanced Usage

You can customize envflag [Optional].

func main() {
    ef := envflag.NewEnvFlag(
	    flag.CommandLine, // which FlagSet to parse
		2, // min length
		map[string]string{ // User-defined env-flag map
            "MY_APP_ENV": "app-env",
        },
		true, // show env variable key in usage
		true, // show env variable value in usage
    )
    var (
        appEnv = flag.String("app-env", "dev", "Application env")
    )
    if err := ef.Parse(os.Args[1:]); err != nil {
	    panic(err)
	}
    fmt.Println("appEnv:", appEnv)
}

Enable debug info

Use envflag.SetDebugEnabled(true).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugEnabled

func DebugEnabled() bool

DebugEnabled returns whether the debug is enabled or not

func Parse

func Parse() error

Parse parses the command-line flags from env and os.Args[1:]. Value from env can be overrided by os.Args[1:]. This function also add ENVIRONMENT VARIABLE to usage. It is extremely recommended to call this function in main() after all flags are defined and before flags are accessed by the program. NOTICE: flag.Parse() will be called by this function.

func ProcessFlagWithEnv

func ProcessFlagWithEnv() error

ProcessFlagWithEnv parses flag from env. This function also add ENVIRONMENT VARIABLE to usage. NOTICE: flag.Parse() will not be called by this function.

func SetDebugEnabled

func SetDebugEnabled(enabled bool)

SetDebugEnabled enables debug info

func SetEnvFlagDict

func SetEnvFlagDict(v map[string]string)

SetEnvFlagDict sets a user-defined env-flag map for standard envflag.

func SetMinLength

func SetMinLength(v int)

SetMinLength sets the min length for standard envflag. EnvFlag only parses the environment variables that is longer than min length and modify usage that is longer than min length.

func SetShowEnvKeyInUsage

func SetShowEnvKeyInUsage(v bool)

SetShowEnvKeyInUsage sets whether show env key in usage for standard envflag.

func SetShowEnvValInUsage

func SetShowEnvValInUsage(v bool)

SetShowEnvValInUsage sets whether show env value in usage for standard envflag. This is useful for confirming the environment variables.

Types

type EnvFlag

type EnvFlag struct {
	// contains filtered or unexported fields
}

EnvFlag represents a envflag object that contains several settings.

func NewEnvFlag

func NewEnvFlag(
	flagSet *flag.FlagSet,
	minLength int,
	envFlagDict map[string]string,
	showEnvKeyInUsage bool,
	showEnvValInUsage bool) *EnvFlag

NewEnvFlag returns a new EnvFlag.

func (EnvFlag) Parse

func (ef EnvFlag) Parse(arguments []string) error

Parse parses flag definitions from env and the argument list. Value from env can be overrided by the argument list. This function also add ENVIRONMENT VARIABLE to usage. It is extremely recommended to call this function in main() after all flags are defined and before flags are accessed by the program. NOTICE: flag.Parse() will be called by this function.

func (EnvFlag) ProcessFlagWithEnv

func (ef EnvFlag) ProcessFlagWithEnv() error

ProcessFlagWithEnv parses flag from env. This function also add ENVIRONMENT VARIABLE to usage. NOTICE: flag.Parse() will not be called by this function.

func (*EnvFlag) SetEnvFlagDict

func (ef *EnvFlag) SetEnvFlagDict(v map[string]string)

SetEnvFlagDict sets a user-defined env-flag map.

func (*EnvFlag) SetMinLength

func (ef *EnvFlag) SetMinLength(v int)

SetMinLength sets the min length. EnvFlag only parses the environment variables that is longer than min length and modify usage that is longer than min length.

func (*EnvFlag) SetShowEnvKeyInUsage

func (ef *EnvFlag) SetShowEnvKeyInUsage(v bool)

SetShowEnvKeyInUsage sets whether show env key in usage.

func (*EnvFlag) SetShowEnvValInUsage

func (ef *EnvFlag) SetShowEnvValInUsage(v bool)

SetShowEnvValInUsage sets whether show env value in usage. This is useful for confirming the environment variables.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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