diff --git a/command_test.go b/command_test.go index 42a2684..aa6caaa 100644 --- a/command_test.go +++ b/command_test.go @@ -610,11 +610,6 @@ func TestOptionValidation(t *testing.T) { options: []cli.Option{cli.Example("comment here", "")}, errMsg: "example command cannot be empty", }, - { - name: "example both empty", - options: []cli.Option{cli.Example("", "")}, - errMsg: "example comment cannot be empty\nexample command cannot be empty", - }, { name: "empty short description", options: []cli.Option{cli.Short("")}, @@ -832,6 +827,24 @@ func BenchmarkExecuteHelp(b *testing.B) { } } +// Benchmarks calling New to build a typical CLI. +func BenchmarkNew(b *testing.B) { + for range b.N { + _, err := cli.New( + "benchy", + cli.Short("A typical CLI to benchmark calling cli.New"), + cli.Version("dev"), + cli.Commit("dfdddaf"), + cli.Example("An example", "bench --help"), + cli.Allow(cli.AnyArgs()), + cli.Run(func(cmd *cli.Command, args []string) error { return nil }), + ) + if err != nil { + b.Fatal(err) + } + } +} + // shuffle returns a randomly ordered copy of items. func shuffle[T any](items []T) []T { shuffled := slices.Clone(items) diff --git a/internal/flag/set.go b/internal/flag/set.go index fd99a69..bff5e94 100644 --- a/internal/flag/set.go +++ b/internal/flag/set.go @@ -13,6 +13,8 @@ import ( // Set is a set of command line flags. type Set struct { + // TODO(@FollowTheProcess): Figure out a way so that we don't have to store 2 maps + // as it's very memory wasteful flags map[string]Entry // The actual stored flags, can lookup by name shorthands map[rune]Entry // The flags by shorthand args []string // Arguments minus flags or flag values diff --git a/option.go b/option.go index 3c48133..b107cc9 100644 --- a/option.go +++ b/option.go @@ -212,15 +212,11 @@ func Long(long string) Option { // An arbitrary number of examples can be added to a [Command], and calls to [Example] are additive. func Example(comment, command string) Option { f := func(cfg *config) error { - errs := make([]error, 0, 2) //nolint:mnd // 2 here is because we have two arguments if comment == "" { - errs = append(errs, errors.New("example comment cannot be empty")) + return errors.New("example comment cannot be empty") } if command == "" { - errs = append(errs, errors.New("example command cannot be empty")) - } - if len(errs) > 0 { - return errors.Join(errs...) + return errors.New("example command cannot be empty") } cfg.examples = append(cfg.examples, example{comment: comment, command: command}) return nil