Skip to content

Commit

Permalink
Will now include all mandatory args that are missing instead of first…
Browse files Browse the repository at this point in the history
… only (#7)

Co-authored-by: Itay Porezky <itay@email>
  • Loading branch information
itayporezky and Itay Porezky committed Mar 15, 2024
1 parent 2dae1a7 commit ff1c811
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clap/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ func argsToFields(args []string, fieldDescs map[string]*fieldDescription, cfg an
name = desc.ShortName
}
results.Mandatory = append(results.Mandatory, name)
return results, fmt.Errorf("mandatory argument '%s' not found: %w", name, ErrMandatoryArgument)
}
}
if len(results.Mandatory) != 0 {
return results, fmt.Errorf("mandatory argument/s: '%v' not found: %w", strings.Join(results.Mandatory, ","), ErrMandatoryArgument)
}

return results, nil
}

Expand Down
25 changes: 25 additions & 0 deletions clap/clap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ func TestMandatoryNotFound(t *testing.T) {
t.Logf("t: %v\n", results)
}

func TestMultipleMandatoryNotFound(t *testing.T) {
type config struct {
Image int `clap:"--image,mandatory"`
Num int `clap:"--number,mandatory"`
}
wanted := []string{"image", "number"}

cfg := &config{}
var err error
var results *clap.Results

if results, err = clap.Parse([]string{"-i", "photo.png"}, cfg); err != nil {
if !errors.Is(err, clap.ErrMandatoryArgument) {
t.Errorf("parsing error: %s", err)
return
}
if !reflect.DeepEqual(results.Mandatory, wanted) {
t.Errorf("wanted: '%v', got '%v'", wanted, results.Mandatory)
return
}
t.Logf("t: %v\n", err)
}
t.Logf("t: %v\n", results)
}

func TestInvalidTag(t *testing.T) {
type config struct {
Test string `clap:"--test,-wrongtag"`
Expand Down

0 comments on commit ff1c811

Please sign in to comment.