diff options
| author | brettlangdon <[email protected]> | 2016-01-18 13:42:04 -0500 |
|---|---|---|
| committer | brettlangdon <[email protected]> | 2016-01-18 13:42:04 -0500 |
| commit | 8dd29d34bf0186945d53ba1ca0cde2324952a6e9 (patch) | |
| tree | 8c0aca1232c96f25124120005d7e32ce938a3396 /parse.go | |
| parent | c9155bb0c3e57557bae7d7c54b60ab8fe2e95211 (diff) | |
Add support for environment variables
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -18,6 +18,7 @@ type spec struct { required bool positional bool help string + env string wasPresent bool isBool bool } @@ -130,6 +131,13 @@ func NewParser(dests ...interface{}) (*Parser, error) { spec.positional = true case key == "help": spec.help = value + case key == "env": + // Use override name if provided + if value != "" { + spec.env = value + } else { + spec.env = strings.ToUpper(field.Name) + } default: return nil, fmt.Errorf("unrecognized tag '%s' on field %s", key, tag) } @@ -179,6 +187,15 @@ func process(specs []*spec, args []string) error { if spec.short != "" { optionMap[spec.short] = spec } + if spec.env != "" { + if value, found := os.LookupEnv(spec.env); found { + err := setScalar(spec.dest, value) + if err != nil { + return fmt.Errorf("error processing environment variable %s: %v", spec.env, err) + } + spec.wasPresent = true + } + } } // process each string from the command line |
