Skip to content

Commit

Permalink
godoc + badges
Browse files Browse the repository at this point in the history
  • Loading branch information
efixler committed Feb 16, 2024
1 parent 50667ba commit ff3db8a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# envflags
[![Go Reference](https://pkg.go.dev/badge/github.com/efixler/envflags.svg)](https://pkg.go.dev/github.com/efixler/envflags)
[![Go Report Card](https://goreportcard.com/badge/github.com/efixler/envflags)](https://goreportcard.com/report/github.com/efixler/envflags)

## Description
`envflags` is for when you need to provide alternative environment variable settings for command
Expand Down
23 changes: 21 additions & 2 deletions envflags.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// envflags provides a way to set flags from environment variables,
// working in conjunction with the flag package; it adds the
// option to look for an environment variable in between the flag and
// its defsult value, and also adds environment variable names to the
// usage string.
//
// The precedence order for values is explicit flag -> environment
// variable -> default.
package envflags

import (
Expand All @@ -13,17 +21,24 @@ import (
)

var (
EnvPrefix = ""
EnvPrefix = ""
// Will be added to usage strings for flags that have an
// environment variable. The format string substitutes the
// environment variable name.
EnvUsageTemplate = "\nEnvironment: %s"
)

type Value[T any] struct {
envName string
flagValue T
converter func(string) (T, error)
isBoolFlag bool
envName string
}

// NewEnvFlagValue creates a new Value[T] with the given environment
// variable name. Pass "" to the envName to disable environment variables
// for this flag. The defaultValue is used if the environment variable or
// explicit flag are not set or cannot be converted.
func NewEnvFlagValue[T any](
envName string,
defaultValue T,
Expand All @@ -37,6 +52,8 @@ func NewEnvFlagValue[T any](
return envFlag
}

// AddTo adds the flag to the given flag.FlagSet with the given name.
// usage is appended with environment flag info if applicable.
func (p *Value[T]) AddTo(flags *flag.FlagSet, name, usage string) {
usage += p.envUsage()
flags.Var(p, name, usage)
Expand Down Expand Up @@ -78,10 +95,12 @@ func (p Value[T]) String() string {
return fmt.Sprintf("%v", p.flagValue)
}

// Get the value of the flag.
func (p Value[T]) Get() T {
return p.flagValue
}

// Implements flag.Setter
func (p *Value[T]) Set(value string) error {
if p.converter == nil {
return fmt.Errorf("no converter for type %T", p.flagValue)
Expand Down
Empty file added go.sum
Empty file.

0 comments on commit ff3db8a

Please sign in to comment.