summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorIllia Volochii <[email protected]>2018-04-26 21:10:44 +0300
committerIllia Volochii <[email protected]>2018-04-26 21:10:44 +0300
commit75bf1a1525e860418b617b6440255fddd2eed205 (patch)
tree7d0e517277a2f398fecaa7dfb4ad874274277c8d /parse.go
parent074ee5f759999d103724b5594e33901adeb28e73 (diff)
Fix providing multiple values via environment variables
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/parse.go b/parse.go
index 1416223..03b6c07 100644
--- a/parse.go
+++ b/parse.go
@@ -2,6 +2,7 @@ package arg
import (
"encoding"
+ "encoding/json"
"errors"
"fmt"
"os"
@@ -275,7 +276,23 @@ func process(specs []*spec, args []string) error {
}
if spec.env != "" {
if value, found := os.LookupEnv(spec.env); found {
- err := scalar.ParseValue(spec.dest, value)
+ var err error
+ if spec.multiple {
+ // expect a JSON array of strings in an environment
+ // variable in the case of multiple values
+ var values []string
+ err = json.Unmarshal([]byte(value), &values)
+ if err != nil {
+ return fmt.Errorf(
+ "error processing environment variable %s (it should be a JSON array of strings):\n%v",
+ spec.env,
+ err,
+ )
+ }
+ err = setSlice(spec.dest, values, !spec.separate)
+ } else {
+ err = scalar.ParseValue(spec.dest, value)
+ }
if err != nil {
return fmt.Errorf("error processing environment variable %s: %v", spec.env, err)
}