Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")},
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down