envflagset

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

envflagset

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).

Basic Usage

Just keep it SIMPLE and SIMPLE and SIMPLE!

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

See example folder for complete examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse() error

Parse parses the command-line flags from env and os.Args[1:].

func SetPrefix

func SetPrefix(p string)

SetPrefix sets prefix on default EnvFlagSet instance

Types

type EnvFlagSet

type EnvFlagSet struct {
	// FlagSet defines flag.FlagSet to operate on. If not provided, flag.CommandLine will be used.
	FlagSet *flag.FlagSet

	// Prefix defines environment variable prefix to use
	Prefix string

	// MinLength defines minimal flag name length to use in mapping.
	MinLength int

	// Map defines custom flag name to environment variable mappings. Prefix or MinLength are not taken into account.
	Map map[string]string

	// UpdateUsage switches environment variable names in usage message.
	UpdateUsage bool

	// Env defines environment lookup method. If not defined, syscall environment lookup will be used which is probably
	// the best option for the majority of use cases.
	Env EnvGetter
	// contains filtered or unexported fields
}

EnvFlagSet represents a envflag object that contains several settings.

func (*EnvFlagSet) Parse

func (ef *EnvFlagSet) Parse(arguments []string) error

Parse parses flag definitions from env and the argument list.

func (*EnvFlagSet) Process

func (ef *EnvFlagSet) Process() error

Process updates FlagSet with values from the environment. NOTICE: flag.Parse() will not be called by this function.

type EnvGetter

type EnvGetter interface {
	GetEnv(string) (string, bool)
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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