diff options
| author | Alex Flint <[email protected]> | 2023-07-02 10:07:10 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-02 10:07:10 -0400 |
| commit | 463902ef7d1219df0c6306a3838f4e003da92f91 (patch) | |
| tree | 2116619d5cabf6e1c43c698f502da42dad1e4df6 /parse.go | |
| parent | e25b4707a7d6c63ff6910c1e1bcb416cb8debfb2 (diff) | |
| parent | 259c83fd5aeb44fbb67a183cf09b4ca16d9b30e2 (diff) | |
Merge pull request #222 from IljaN/env-only-args
Support for parameters which can only be passed via env
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 11 |
1 files changed, 9 insertions, 2 deletions
@@ -343,6 +343,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { // Look at the tag var isSubcommand bool // tracks whether this field is a subcommand + for _, key := range strings.Split(tag, ",") { if key == "" { continue @@ -360,7 +361,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { case strings.HasPrefix(key, "--"): spec.long = key[2:] case strings.HasPrefix(key, "-"): - if len(key) != 2 { + if len(key) > 2 { errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only", t.Name(), field.Name)) return false @@ -661,7 +662,7 @@ func (p *Parser) process(args []string) error { // lookup the spec for this option (note that the "specs" slice changes as // we expand subcommands so it is better not to use a map) spec := findOption(specs, opt) - if spec == nil { + if spec == nil || opt == "" { return fmt.Errorf("unknown argument %s", arg) } wasPresent[spec] = true @@ -750,10 +751,16 @@ func (p *Parser) process(args []string) error { } if spec.required { + if spec.short == "" && spec.long == "" { + 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) } |
