From 7b1d9ef23f852b0f770072be69df83b5a1bbee8f Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 14 Apr 2019 17:30:53 -0700 Subject: [PATCH] simplify processing of positionals a little --- parse.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/parse.go b/parse.go index 3c682f5..c4afda2 100644 --- a/parse.go +++ b/parse.go @@ -383,25 +383,24 @@ func process(specs []*spec, args []string) error { // process positionals for _, spec := range specs { - if spec.positional { - if spec.multiple { - if spec.required && len(positionals) == 0 { - return fmt.Errorf("%s is required", spec.long) - } - err := setSlice(spec.dest, positionals, true) - if err != nil { - return fmt.Errorf("error processing %s: %v", spec.long, err) - } - positionals = nil - } else if len(positionals) > 0 { - err := scalar.ParseValue(spec.dest, positionals[0]) - if err != nil { - return fmt.Errorf("error processing %s: %v", spec.long, err) - } - positionals = positionals[1:] - } else if spec.required { - return fmt.Errorf("%s is required", spec.long) + if !spec.positional { + continue + } + if spec.required && len(positionals) == 0 { + return fmt.Errorf("%s is required", spec.long) + } + if spec.multiple { + err := setSlice(spec.dest, positionals, true) + if err != nil { + return fmt.Errorf("error processing %s: %v", spec.long, err) } + positionals = nil + } else if len(positionals) > 0 { + err := scalar.ParseValue(spec.dest, positionals[0]) + if err != nil { + return fmt.Errorf("error processing %s: %v", spec.long, err) + } + positionals = positionals[1:] } } if len(positionals) > 0 {