Skip to content

Commit

Permalink
Remove a further allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess committed Nov 5, 2024
1 parent 60a860c commit 5452483
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,26 @@ func New(name string, options ...Option) (*Command, error) {
argValidator: AnyArgs(),
}

// Ensure we always have at least help and version flags
defaultOptions := []Option{
Flag(&cfg.helpCalled, "help", 'h', false, fmt.Sprintf("Show help for %s", name)),
Flag(&cfg.versionCalled, "version", 'V', false, fmt.Sprintf("Show version info for %s", name)),
}

toApply := slices.Concat(options, defaultOptions)

// Apply the options, gathering up all the validation errors
// to report in one go
var errs error
for _, option := range toApply {
for _, option := range options {
if err := option.apply(&cfg); err != nil {
errs = errors.Join(errs, err)
}
}

// Ensure we always have at least help and version flags
err := Flag(&cfg.helpCalled, "help", 'h', false, fmt.Sprintf("Show help for %s", name)).apply(&cfg)
if err != nil {
errs = errors.Join(errs, err)
}

Check warning on line 67 in command.go

View check run for this annotation

Codecov / codecov/patch

command.go#L66-L67

Added lines #L66 - L67 were not covered by tests

err = Flag(&cfg.versionCalled, "version", 'V', false, fmt.Sprintf("Show version info for %s", name)).apply(&cfg)
if err != nil {
errs = errors.Join(errs, err)

Check warning on line 71 in command.go

View check run for this annotation

Codecov / codecov/patch

command.go#L71

Added line #L71 was not covered by tests
}

if errs != nil {
return nil, errs
}
Expand Down

0 comments on commit 5452483

Please sign in to comment.