diff options
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 26 |
1 files changed, 23 insertions, 3 deletions
@@ -2,6 +2,7 @@ package arg import ( "encoding" + "encoding/csv" "errors" "fmt" "os" @@ -275,9 +276,28 @@ func process(specs []*spec, args []string) error { } if spec.env != "" { if value, found := os.LookupEnv(spec.env); found { - err := scalar.ParseValue(spec.dest, value) - if err != nil { - return fmt.Errorf("error processing environment variable %s: %v", spec.env, err) + if spec.multiple { + // expect a CSV string in an environment + // variable in the case of multiple values + values, err := csv.NewReader(strings.NewReader(value)).Read() + if err != nil { + return fmt.Errorf( + "error reading a CSV string from environment variable %s with multiple values: %v", + spec.env, + err, + ) + } + if err = setSlice(spec.dest, values, !spec.separate); err != nil { + return fmt.Errorf( + "error processing environment variable %s with multiple values: %v", + spec.env, + err, + ) + } + } else { + if err := scalar.ParseValue(spec.dest, value); err != nil { + return fmt.Errorf("error processing environment variable %s: %v", spec.env, err) + } } spec.wasPresent = true } |
