diff options
| author | Ilja Neumann <[email protected]> | 2023-06-03 12:47:47 +0200 |
|---|---|---|
| committer | Ilja Neumann <[email protected]> | 2023-06-03 12:47:47 +0200 |
| commit | 18623d869bfb7aa7b87eda2c8dc7d1bf6149f316 (patch) | |
| tree | 47bc965aac8847cb35791ea5e79871242b9df926 /parse.go | |
| parent | b928a1839ae3502fc6fef8d7743dd78f2c772c8a (diff) | |
help,usage and error messages and tests
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -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) } |
