summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go12
1 files changed, 11 insertions, 1 deletions
diff --git a/parse.go b/parse.go
index 2eb14e3..a16485b 100644
--- a/parse.go
+++ b/parse.go
@@ -418,10 +418,14 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
spec.placeholder = strings.ToUpper(spec.field.Name)
}
- if spec.env == "" && emptyLongAndShort(kvPairs) {
+ noFormSpecs := emptyLongAndShort(kvPairs)
+
+ if spec.env == "" && noFormSpecs {
errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only",
t.Name(), field.Name))
return false
+ } else if spec.env != "" && noFormSpecs {
+ spec.envOnly = true
}
// if this is a subcommand then we've done everything we need to do
@@ -759,10 +763,16 @@ func (p *Parser) process(args []string) error {
}
if spec.required {
+ if spec.envOnly {
+ msg := fmt.Sprintf("environment variable %s is required", spec.env)
+ return errors.New(msg)
+ }
+
msg := fmt.Sprintf("%s is required", name)
if spec.env != "" {
msg += " (or environment variable " + spec.env + ")"
}
+
return errors.New(msg)
}