Skip to content

Commit

Permalink
Expose Apply/ApplyPost to package
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Dec 6, 2024
1 parent cd71c50 commit 4744f58
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewCommand(opts ...Option) (*Command, error) {
cmd := &Command{
Section: -1,
}
if err := applyOpts(cmd, opts...); err != nil {
if err := Apply(cmd, opts...); err != nil {
return nil, err
}
switch noName := cmd.Name == ""; {
Expand All @@ -64,7 +64,7 @@ func NewCommand(opts ...Option) (*Command, error) {
case noName:
return nil, fmt.Errorf("command: %w", ErrUsageNotSet)
}
if err := applyPostOpts(cmd, opts...); err != nil {
if err := ApplyPost(cmd, opts...); err != nil {
return nil, err
}
return cmd, nil
Expand Down Expand Up @@ -497,11 +497,11 @@ func NewFlag(name, usage string, opts ...Option) (*Flag, error) {
Usage: usage,
Section: -1,
}
if err := applyOpts(g, opts...); err != nil {
if err := Apply(g, opts...); err != nil {
return nil, err
}
if extra, ok := typeFlagOpts[g.Type]; ok {
if err := applyOpts(g, extra...); err != nil {
if err := Apply(g, extra...); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func NewCommandHelp(cmd *Command, opts ...Option) (*CommandHelp, error) {
Command: cmd,
CommandSort: true,
}
if err := applyOpts(help, opts...); err != nil {
if err := Apply(help, opts...); err != nil {
return nil, err
}
return help, nil
Expand Down
12 changes: 6 additions & 6 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func Defaults(opts ...Option) CommandOption {
return option{
name: "Defaults",
cmd: func(cmd *Command) error {
return applyOpts(cmd, options...)
return Apply(cmd, options...)
},
post: func(cmd *Command) error {
return applyPostOpts(cmd, options...)
return ApplyPost(cmd, options...)
},
}
}
Expand Down Expand Up @@ -729,8 +729,8 @@ func addHelp(cmd *Command) error {
return nil
}

// applyOpts applies the options to v.
func applyOpts(v any, opts ...Option) error {
// Apply applies the options to v.
func Apply(v any, opts ...Option) error {
for _, o := range opts {
if err := o.Option().apply(v); err != nil {
return err
Expand All @@ -739,8 +739,8 @@ func applyOpts(v any, opts ...Option) error {
return nil
}

// postOpts applies post options to the command.
func applyPostOpts(cmd *Command, opts ...Option) error {
// ApplyPost applies post options to the command.
func ApplyPost(cmd *Command, opts ...Option) error {
for _, o := range opts {
if opt := o.Option(); opt.post != nil {
if err := opt.post(cmd); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ox.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func NewContext(opts ...Option) (*Context, error) {
}
return true
}
if err := applyOpts(ctx, opts...); err != nil {
if err := Apply(ctx, opts...); err != nil {
return ctx, err
}
if ctx.Vars == nil {
Expand Down
2 changes: 1 addition & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewVal[T any](opts ...Option) func() (Value, error) {
val := &anyVal[T]{
typ: typeType[T](),
}
if err := applyOpts(val, opts...); err != nil {
if err := Apply(val, opts...); err != nil {
return nil, err
}
return val, nil
Expand Down

0 comments on commit 4744f58

Please sign in to comment.