Skip to content

A tiny golang library. If any flags are not set, use environment variables to set them.

License

Notifications You must be signed in to change notification settings

cu-library/overridefromenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

overridefromenv

Go Report Card gopherbadger-tag-do-not-edit License MIT GoDoc

A Go library for setting unset flags from environment variables.

Usage

Here's an example of a small command line tool called 'scanner' with a flag which can be set on the command line or from the environment. Set flags are not overwritten.

package main

import (
        "flag"
        "fmt"
        "github.com/cu-library/overridefromenv"
        "os"
)

const (
        PREFIX = "SCANNER_"
)

func main() {
        v := flag.Int("powerlevel", 0, "power level")
        flag.Parse()
        err := overridefromenv.Override(flag.CommandLine, PREFIX)
        if err != nil {
                fmt.Println(err)
                os.Exit(1)
        }
        fmt.Printf("Power level: %v\n", *v)
}

Then, from the command line:

$ scanner
Power level: 0
$ SCANNER_POWERLEVEL=1000
$ scanner
Power level: 1000
$ scanner -powerlevel 9000
Power level: 9000
$ SCANNER_POWERLEVEL="One hundred puppies."
$ scanner
Unable to set flag powerlevel from environment variable SCANNER_POWERLEVEL, which has a value of "One hundred puppies.": parse error

About

A tiny golang library. If any flags are not set, use environment variables to set them.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages