Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 1.61 KB

README.md

File metadata and controls

53 lines (44 loc) · 1.61 KB

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