summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorbrettlangdon <[email protected]>2016-01-18 13:42:04 -0500
committerbrettlangdon <[email protected]>2016-01-18 13:42:04 -0500
commit8dd29d34bf0186945d53ba1ca0cde2324952a6e9 (patch)
tree8c0aca1232c96f25124120005d7e32ce938a3396 /parse.go
parentc9155bb0c3e57557bae7d7c54b60ab8fe2e95211 (diff)
Add support for environment variables
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/parse.go b/parse.go
index 219a947..ce3892f 100644
--- a/parse.go
+++ b/parse.go
@@ -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