Skip to content

Commit

Permalink
Add opt.SetCalled ModifyFn to mark an option as called from a wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Nov 5, 2023
1 parent dc1f638 commit 50f75c4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
30 changes: 29 additions & 1 deletion changelog.adoc
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 22 additions & 0 deletions examples/complex/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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
}
35 changes: 35 additions & 0 deletions public_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
}
8 changes: 8 additions & 0 deletions user_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 50f75c4

Please sign in to comment.