diff --git a/changelog.adoc b/changelog.adoc index e871dda..bad38fe 100644 --- a/changelog.adoc +++ b/changelog.adoc @@ -1,10 +1,38 @@ = Changelog :toc: +== wip v0.29.0: New Features + +As the releases before, this release has 100% test coverage. +Tested with Go 1.16, 1.17, 1.18, 1.19, 1.20 and 1.21. + +=== New Features + +* Add `opt.SetCalled` ModifyFn to allow setting an option as called. ++ +Useful when calling `CommandFn` directly and not through `opt.Dispatch` and without a call to `opt.Parse`. +It allows building a `opt` object using defaults and marking the options as called. ++ +For example: ++ +[source,go] +---- + +func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + password := opt.Value("password").(string) + + nopt := getoptions.New() + nopt.String("password", password, opt.SetCalled(opt.Called("password"))) + nopt.Int("number", 123, opt.SetCalled(true)) + nopt.Float64("float", 3.14) // opt.Called("float") is false but its value is set to 3.14 + + err := CommandFn(ctx, nopt, []string{}) +---- + == v0.28.0: Extend ZSHELL support As the releases before, this release has 100% test coverage. -Tested with 1.16, 1.17, 1.18, 1.19, 1.20 and 1.21. +Tested with Go 1.16, 1.17, 1.18, 1.19, 1.20 and 1.21. === Breaking diff --git a/examples/complex/show/show.go b/examples/complex/show/show.go index b278b60..eb1080f 100644 --- a/examples/complex/show/show.go +++ b/examples/complex/show/show.go @@ -22,6 +22,23 @@ func NewCommand(parent *getoptions.GetOpt) *getoptions.GetOpt { // Run - Command entry point func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + showOption := opt.Value("show-option").(bool) + password := opt.Value("password").(string) + + nopt := getoptions.New() + nopt.Bool("show-option", showOption, opt.SetCalled(opt.Called("show-option"))) + nopt.String("password", password, opt.SetCalled(opt.Called("password"))) + nopt.Int("number", 123, opt.SetCalled(true)) + nopt.Float64("float", 3.14) + + err := CommandFn(ctx, nopt, []string{}) + if err != nil { + return err + } + return nil +} + +func CommandFn(ctx context.Context, opt *getoptions.GetOpt, args []string) error { Logger.Printf("args to show: %v\n", args) fmt.Printf("show output... %v\n", args) if opt.Called("show-option") { @@ -30,5 +47,10 @@ func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { if opt.Called("password") { fmt.Printf("The secret was... %s\n", opt.Value("password")) } + if opt.Called("number") { + fmt.Printf("show number: %d\n", opt.Value("number")) + } + fmt.Printf("show float: %f\n", opt.Value("float")) + return nil } diff --git a/public_api_test.go b/public_api_test.go index 35bb598..34f00c4 100644 --- a/public_api_test.go +++ b/public_api_test.go @@ -3751,3 +3751,38 @@ SYNOPSIS: } }) } + +func TestSetCalled(t *testing.T) { + t.Run("unset", func(t *testing.T) { + opt := getoptions.New() + opt.Bool("bool", false) + opt.String("string", "hello") + opt.Int("int", 123, opt.SetCalled(true)) + opt.Int("int2", 456, opt.SetCalled(false)) + + if opt.Value("bool") != false { + t.Errorf("wrong bool value") + } + if opt.Called("bool") { + t.Errorf("wrong bool called value") + } + if opt.Value("string") != "hello" { + t.Errorf("wrong string value") + } + if opt.Called("string") { + t.Errorf("wrong string called value") + } + if opt.Value("int") != 123 { + t.Errorf("wrong int value") + } + if !opt.Called("int") { + t.Errorf("wrong int called value") + } + if opt.Value("int2") != 456 { + t.Errorf("wrong int2 value") + } + if opt.Called("int2") { + t.Errorf("wrong int2 called value") + } + }) +} diff --git a/user_options.go b/user_options.go index 29ba435..831b485 100644 --- a/user_options.go +++ b/user_options.go @@ -42,6 +42,14 @@ func (gopt *GetOpt) Description(msg string) ModifyFn { } } +// SetCalled - Mark the option as called using the option name. +// Useful when adding options to a CommandFn call from a wrapper function. +func (gopt *GetOpt) SetCalled(called bool) ModifyFn { + return func(parent *GetOpt, opt *option.Option) { + opt.Called = called + } +} + // Required - Automatically return an error if the option is not called. // Optionally provide a custom error message, a default error message will be used otherwise. func (gopt *GetOpt) Required(msg ...string) ModifyFn {