env

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: Unlicense Imports: 8 Imported by: 0

Documentation

Overview

Package env provides a flagr.Parser that is able to read environment variables (and .env files) and set the appropriate flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parser

func Parser(opts ...Option) flagr.Parser

Types

type LookupFunc

type LookupFunc func(varName string) (string, bool)

LookupFunc returns the value for the given variable and whether it was found.

type Mapper

type Mapper func(flagName string) (envName string, listSplitter Splitter)

Mapper maps a flag to the corresponding env var.

func DefaultMapper

func DefaultMapper(splitter Splitter) Mapper

DefaultMapper converts flags to env vars by replacing any non alphanumeric character (in the ascii sense) with an underscore.

If splitter is not NoSplit (an empty string), it will be used to try to split env values to lists. If you need to control the splitter per flag, use a custom Mapper.

type Option

type Option func(*options)

func WithDotEnv

func WithDotEnv(path *string, optional bool) Option

WithDotEnv tells the parser to also parse the given .env file. Env vars take precedence over anything defined in it.

Parsing will fail if the file does not exist, unless ignoreMissing is true.

Path will be resolved just in time, so it may be set by other parsers up in the chain.

func WithLookupFunc

func WithLookupFunc(fn LookupFunc) Option

WithLookupFunc replaces the default lookup method (os.LookupEnv) with the given func.

func WithMapper

func WithMapper(fn Mapper) Option

WithMapper tells the parser how to map flags to env vars and, if the value is expected to be a list, how to split it.

func WithPrefix

func WithPrefix(s string) Option

WithPrefix prefixes every flag with s before mapping it to the corresponding env var. The prefix need not end in an underscore as one will be added automatically.

type Splitter

type Splitter string
const NoSplit Splitter = ""

NoSplit disables value splitting.

Jump to

Keyboard shortcuts

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