diff --git a/README.md b/README.md index cc1e9e2..9d0b97a 100644 --- a/README.md +++ b/README.md @@ -133,8 +133,17 @@ The `opts` tag can consist of: - `default:` : a default value for the field if not specified on the command-line - `env:` : the name of an environment variable that can override the default -- `sep:`: a separator for fields that can accept multiple values. By default, - multiple values must be specified by repeating the option flag multiple times. +- `sep:`: a list separator characters for fields that can accept multiple + values. To use a comma or colon as separator, those characters must be escaped + with a double-backslash (`\\,` or `\\:`). To use spaces and newlines as + separator, the special value `\\s` can be used. Unless a separator is + specified with this option, additional values must be specified by repeating + the option flag multiple times. +- `keep-spaces` : when specified with no value, this option preserves the spaces + around the argument values, that would otherwise be trimmed by default. +- `keep-empty` : for fields thar accept multiple values using a separator, this + option preserve empty values after splitting and trimming, that would + otherwise be dropped by default. - `name:`: the display name for the value, used when printing out description of the field. @@ -160,10 +169,24 @@ standard interface, and includes: Unless otherwise initialized, all pointer fields are initialized to `nil`, all slice fields are initialized to an empty slice, and all scalar fields are -initialized to their built-in zero value. If the built-in zero value is a valid -value and if the command needs to determine if an option has been provided, a +initialized to their built-in zero value. If the command needs to differentiate +between the built-in zero value of a scalar field and a specified zero value, a pointer type should be used and checked against `nil`. +By default, slice field values must be provided by repeating the option flag +multiple times, once of each value. If a separator is defined (`sep:`), multiple +or all values can be provided with one command-line argument. To provide multiple values through an environment variable, a separated must be defined. + +> New in v0.5.0: A breaking change has been introduced to better handle lists of +> values and spaces around values. Prior behavior can be restores with the +> `keep-spaces` option for all fields and `keep-empty` for lists. With this new +> behavior, spaces around the values are automatically trimmed, and empty values +> after trimming are dropped from value lists. In effect, this allows for +> trailing separators to be ignored, and for list values to be specified with +> additional spaces around the separators, including newlines. Note that, even +> with `keep-empty`, if the last character is a separator, the last empty values +> is always drop, as was the case in prior versions. + In a command-line interface, all option flags are optional. Required options should use positional arguments. Positional arguments are required unless defined with a pointer type, and only if all subsequent positional arguments are diff --git a/RELEASES.md b/RELEASES.md index b02b204..6d8284b 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,4 +1,4 @@ -# v0.5.0 +# v0.5.0-rc ## Key Features @@ -11,6 +11,12 @@ custom completion handler - `DefaultFilenameCompletion()` matching default shell completion - `MatchingFilenameCompletion()` for pattern based filename matching +- Surrounding spaces are automatically trimmed from values unless `keep-spaces` + option is specified on the argument +- List values automatically drop empty values unless `keep-empty` option is + specified on the argument. Note that even with `keep-empty`, if the last + character is a separator, the last empty values is always drop, as was the + case in the prior versions. ## Code changes diff --git a/go.mod b/go.mod index c6de827..9b536f9 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,13 @@ go 1.17 require ( github.com/maargenton/go-errors v1.0.0 - github.com/maargenton/go-fileutils v0.6.2 + github.com/maargenton/go-fileutils v0.6.4 github.com/maargenton/go-testpredicate v1.3.0 - golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b - golang.org/x/tools v0.1.10 + golang.org/x/term v0.10.0 + golang.org/x/tools v0.11.0 ) require ( - golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect - golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect ) diff --git a/go.sum b/go.sum index 5e0a1fe..5704c83 100644 --- a/go.sum +++ b/go.sum @@ -2,51 +2,70 @@ github.com/maargenton/fileutil v0.4.1/go.mod h1:+GxNHyNo3uqVv2QJPfmdzIByRSLf5qKK github.com/maargenton/go-errors v0.0.0-20200720205202-f0b27f4dc001/go.mod h1:qw42L2So9gQZM8ZrnBoBc4deYTaL89dyE+2wuNGELMg= github.com/maargenton/go-errors v1.0.0 h1:gNDwTfN3VHwLog2w/BfeGlCTjwjJ6H0NeMKS+4xCNE4= github.com/maargenton/go-errors v1.0.0/go.mod h1:yVvhNp6mywh/FFoTYbi2WPH7oP0U9AZXtvGa8SDIxOQ= -github.com/maargenton/go-fileutils v0.6.2 h1:D2MdHYUTFr+KQdq3aQfkUqpcgkDOUQNZ3bGY7URuBAU= -github.com/maargenton/go-fileutils v0.6.2/go.mod h1:9gClkdrb1nDcnC2w1vCvClhJESGjK9wQJ2RusLAxOSk= +github.com/maargenton/go-fileutils v0.6.4 h1:4rtH1Bwcv3C+ps72kjlAAQeJ36fYg8WSHPCIyfZqqjU= +github.com/maargenton/go-fileutils v0.6.4/go.mod h1:9gClkdrb1nDcnC2w1vCvClhJESGjK9wQJ2RusLAxOSk= github.com/maargenton/go-testpredicate v0.4.4/go.mod h1:sn267JpJ65uvLQ7VtYzPLvXQiw0tHHw+KbCFzxRPOww= github.com/maargenton/go-testpredicate v0.6.4/go.mod h1:lUPR99Ipl1o49oRb5wEUwb/7KXrwc1FAVR6aZmCmoS8= github.com/maargenton/go-testpredicate v1.3.0 h1:uy9g71epeAmI2CXHnEYKFDwueXppvLHZMoUfAF82K9o= github.com/maargenton/go-testpredicate v1.3.0/go.mod h1:ZscqHa6PT35rm0psZd9y1KV6ygPL1C5NfDFBDFTkGu8= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE= -golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= -golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= +golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/option/option.go b/pkg/option/option.go index 7f361d7..45580c1 100644 --- a/pkg/option/option.go +++ b/pkg/option/option.go @@ -32,6 +32,8 @@ type T struct { Default string Env string Sep string // optional separator + KeepSpaces bool + KeepEmpty bool Description string ValueName string // optional name for the value Position int // set to non-zero for fields capturing positional arguments @@ -161,6 +163,11 @@ func (opt *T) SetBool() { func (opt *T) SetValue(s string) error { var fv = opt.opts.target.FieldByIndex(opt.Index) var err error + + if !opt.KeepSpaces { + s = strings.TrimSpace(s) + } + if opt.Type == Ptr { err = opt.setPtrValue(fv, s) } else if opt.Type == Slice { @@ -168,6 +175,7 @@ func (opt *T) SetValue(s string) error { } else { err = value.Parse(fv.Addr().Interface(), s) } + if err != nil { err = fmt.Errorf("failed to set value for '%v': %w", opt.Name(), err) } @@ -190,6 +198,12 @@ func (opt *T) setSliceValue(fv reflect.Value, s string) error { } else if opt.Sep != "" { var updatedSlice = fv for _, vs := range splitSliceValues(s, opt.Sep) { + if !opt.KeepSpaces { + vs = strings.TrimSpace(vs) + } + if !opt.KeepEmpty && vs == "" { + continue + } var v = reflect.New(fv.Type().Elem()) if err := value.Parse(v.Interface(), vs); err != nil { return err @@ -264,6 +278,10 @@ func (opt *T) parseOptsTag(tag string) error { opt.Sep = v } else if k == "name" { opt.ValueName = v + } else if k == "keep-spaces" { + opt.KeepSpaces = true + } else if k == "keep-empty" { + opt.KeepEmpty = true } else { return fmt.Errorf("invalid tag in opts: '%v'", k) } diff --git a/pkg/option/option_test.go b/pkg/option/option_test.go index 5692abb..03807c4 100644 --- a/pkg/option/option_test.go +++ b/pkg/option/option_test.go @@ -1,13 +1,13 @@ package option_test import ( - "fmt" "net/url" "reflect" "testing" "time" "github.com/maargenton/go-errors" + "github.com/maargenton/go-testpredicate/pkg/bdd" "github.com/maargenton/go-testpredicate/pkg/require" "github.com/maargenton/go-testpredicate/pkg/verify" @@ -20,7 +20,7 @@ import ( // --------------------------------------------------------------------------- func TestOptionName(t *testing.T) { - t.Run("Given a struct with opts tags", func(t *testing.T) { + bdd.Given(t, "a struct with opts tags", func(t *bdd.T) { type options struct { Period time.Duration `opts:"-d,--duration"` Count int `opts:"-c"` @@ -34,51 +34,51 @@ func TestOptionName(t *testing.T) { require.That(t, err).IsNil() require.That(t, optionSet).IsNotNil() - t.Run("when getting the name of a field with long flag", func(t *testing.T) { + t.When("getting the name of a field with long flag", func(t *bdd.T) { var option = optionSet.GetOption("duration") require.That(t, option).IsNotNil() - t.Run("the name includes the long flag", func(t *testing.T) { + t.Then("the name includes the long flag", func(t *bdd.T) { require.That(t, option.Name()).Eq("--duration") }) }) - t.Run("when getting the name of a field with only short flag", func(t *testing.T) { + t.When("getting the name of a field with only short flag", func(t *bdd.T) { var option = optionSet.GetOption("c") require.That(t, option).IsNotNil() - t.Run("the name includes the short flag", func(t *testing.T) { + t.Then("the name includes the short flag", func(t *bdd.T) { require.That(t, option.Name()).Eq("-c") }) }) - t.Run("when getting the name of a positional argument field", func(t *testing.T) { + t.When("getting the name of a positional argument field", func(t *bdd.T) { var option = optionSet.Positional[0] require.That(t, option).IsNotNil() - t.Run("the name includes the value name", func(t *testing.T) { + t.Then("the name includes the value name", func(t *bdd.T) { require.That(t, option.Name()).Eq("") }) }) - t.Run("when getting the name of a positional argument field", func(t *testing.T) { + t.When("getting the name of a positional argument field", func(t *bdd.T) { var option = optionSet.Positional[1] require.That(t, option).IsNotNil() - t.Run("the name includes the argument position", func(t *testing.T) { + t.Then("the name includes the argument position", func(t *bdd.T) { require.That(t, option.Name()).Eq("") }) }) - t.Run("when getting the name of an extra arguments field", func(t *testing.T) { + t.When("getting the name of an extra arguments field", func(t *bdd.T) { var option = optionSet.Args require.That(t, option).IsNotNil() - t.Run("the name includes the value name", func(t *testing.T) { + t.Then("the name includes the value name", func(t *bdd.T) { require.That(t, option.Name()).Eq("...") }) }) }) - t.Run("Given another struct with opts tags", func(t *testing.T) { + bdd.Given(t, "another struct with opts tags", func(t *bdd.T) { type options struct { Inputs []string `opts:"args"` } @@ -88,11 +88,11 @@ func TestOptionName(t *testing.T) { require.That(t, err).IsNil() require.That(t, optionSet).IsNotNil() - t.Run("when getting the name of an extra arguments field", func(t *testing.T) { + t.When("getting the name of an extra arguments field", func(t *bdd.T) { var option = optionSet.Args require.That(t, option).IsNotNil() - t.Run("the name includes default name and ellipsis", func(t *testing.T) { + t.Then("the name includes default name and ellipsis", func(t *bdd.T) { require.That(t, option.Name()).Eq("...") }) }) @@ -193,9 +193,9 @@ func TestOptionDescriptionUsage(t *testing.T) { } for _, tc := range tcs { - t.Run(fmt.Sprintf("Given %v", tc.name), func(t *testing.T) { - t.Run("when calling Usage()", func(t *testing.T) { - t.Run("then it returns formatted usage", func(t *testing.T) { + bdd.Given(t, tc.name, func(t *bdd.T) { + t.When("calling Usage()", func(t *bdd.T) { + t.Then("it returns formatted usage", func(t *bdd.T) { require.That(t, tc.opt.GetUsage()).Field("Option").Eq(tc.usage) }) }) @@ -241,9 +241,9 @@ func TestOptionDescriptionDescription(t *testing.T) { } for _, tc := range tcs { - t.Run(fmt.Sprintf("Given %v", tc.name), func(t *testing.T) { - t.Run("when calling Usage()", func(t *testing.T) { - t.Run("then it returns formatted description", func(t *testing.T) { + bdd.Given(t, tc.name, func(t *bdd.T) { + t.When("calling Usage()", func(t *bdd.T) { + t.Then("it returns formatted description", func(t *bdd.T) { require.That(t, tc.opt.GetUsage()).Field("Description").Eq(tc.desc) }) }) @@ -256,7 +256,7 @@ func TestOptionDescriptionDescription(t *testing.T) { // --------------------------------------------------------------------------- func TestOption_SetBool(t *testing.T) { - t.Run("Given a struct with bool options", func(t *testing.T) { + bdd.Given(t, "a struct with bool options", func(t *bdd.T) { args := struct { A bool `opts:"-a,--aaa"` Ptr *bool `opts:"-p,--ppp"` @@ -265,25 +265,25 @@ func TestOption_SetBool(t *testing.T) { optionSet, err := option.NewOptionSet(&args) require.That(t, err).IsNil() - t.Run("when calling Set() on a bool field", func(t *testing.T) { + t.When("calling Set() on a bool field", func(t *bdd.T) { optionSet.GetOption("a").SetBool() - t.Run("then the field is set to true", func(t *testing.T) { + t.Then("the field is set to true", func(t *bdd.T) { require.That(t, args.A).IsTrue() }) }) - t.Run("when calling Set() on a bool pointer field", func(t *testing.T) { + t.When("calling Set() on a bool pointer field", func(t *bdd.T) { optionSet.GetOption("p").SetBool() - t.Run("then the field is set to point to a true value", func(t *testing.T) { + t.Then("the field is set to point to a true value", func(t *bdd.T) { require.That(t, args.Ptr).IsNotNil() require.That(t, *args.Ptr).IsTrue() }) }) - t.Run("when calling Set() on a non-bool field", func(t *testing.T) { - t.Run("then it panics", func(t *testing.T) { + t.When("calling Set() on a non-bool field", func(t *bdd.T) { + t.Then("it panics", func(t *bdd.T) { require.That(t, func() { optionSet.GetOption("n").SetBool() @@ -300,7 +300,7 @@ func TestOption_SetBool(t *testing.T) { // --------------------------------------------------------------------------- func TestOption_SetValue(t *testing.T) { - t.Run("Given a struct with `opts` tags", func(t *testing.T) { + bdd.Given(t, "a struct with `opts` tags", func(t *bdd.T) { args := struct { Period time.Duration `opts:"-p,--period"` @@ -311,71 +311,71 @@ func TestOption_SetValue(t *testing.T) { require.That(t, err).IsNil() require.That(t, optionSet).IsNotNil() - t.Run("when setting the value of a scalar field", func(t *testing.T) { + t.When("setting the value of a scalar field", func(t *bdd.T) { option := optionSet.GetOption("period") require.That(t, option).IsNotNil() - t.Run("with a valid value", func(t *testing.T) { + t.Run("with a valid value", func(t *bdd.T) { args.Period = 0 * time.Second err := option.SetValue("5m30s") expected := 5*time.Minute + 30*time.Second - t.Run("then the field is set accordingly", func(t *testing.T) { + t.Then("the field is set accordingly", func(t *bdd.T) { require.That(t, err).IsNil() require.That(t, args.Period).Eq(expected) }) }) - t.Run("with an invalid value", func(t *testing.T) { + t.Run("with an invalid value", func(t *bdd.T) { args.Period = 0 * time.Second err := option.SetValue("5m30p") - t.Run("then an error is returned and the value is not changed", func(t *testing.T) { + t.Then("an error is returned and the value is not changed", func(t *bdd.T) { require.That(t, err).IsNotNil() require.That(t, args.Period).Eq(0 * time.Second) }) }) }) - t.Run("when setting the value of a pointer field", func(t *testing.T) { + t.When("setting the value of a pointer field", func(t *bdd.T) { option := optionSet.GetOption("duration") require.That(t, option).IsNotNil() - t.Run("with a valid value", func(t *testing.T) { + t.Run("with a valid value", func(t *bdd.T) { args.Duration = nil err := option.SetValue("5m30s") expected := 5*time.Minute + 30*time.Second - t.Run("then the field is set accordingly", func(t *testing.T) { + t.Then("the field is set accordingly", func(t *bdd.T) { require.That(t, err).IsNil() require.That(t, args.Duration).IsNotNil() require.That(t, *args.Duration).Eq(expected) }) }) - t.Run("with an invalid value", func(t *testing.T) { + t.Run("with an invalid value", func(t *bdd.T) { args.Duration = nil err := option.SetValue("5m30p") - t.Run("then an error is returned and the value is not changed", func(t *testing.T) { + t.Then("an error is returned and the value is not changed", func(t *bdd.T) { require.That(t, err).IsNotNil() require.That(t, args.Duration).IsNil() }) }) }) - t.Run("when setting the value of a slice field", func(t *testing.T) { + t.When("setting the value of a slice field", func(t *bdd.T) { option := optionSet.GetOption("intervals") require.That(t, option).IsNotNil() - t.Run("with a single valid value", func(t *testing.T) { + t.Run("with a single valid value", func(t *bdd.T) { args.Intervals = nil err := option.SetValue("5m30s") expected := []time.Duration{ 5*time.Minute + 30*time.Second, } - t.Run("then the field is set accordingly", func(t *testing.T) { + t.Then("the field is set accordingly", func(t *bdd.T) { require.That(t, err).IsNil() require.That(t, args.Intervals).IsNotNil() require.That(t, args.Intervals).Length().Eq(1) @@ -383,7 +383,7 @@ func TestOption_SetValue(t *testing.T) { }) }) - t.Run("with multiple delimited values", func(t *testing.T) { + t.Run("with multiple delimited values", func(t *bdd.T) { args.Intervals = nil err1 := option.SetValue("1m,2m,3m") err2 := option.SetValue("4m,5m") @@ -395,14 +395,14 @@ func TestOption_SetValue(t *testing.T) { 5 * time.Minute, } - t.Run("then all values are recorded", func(t *testing.T) { + t.Then("all values are recorded", func(t *bdd.T) { require.That(t, err1).IsNil() require.That(t, err2).IsNil() require.That(t, args.Intervals).Eq(expected) }) }) - t.Run("with empty value", func(t *testing.T) { + t.Run("with empty value", func(t *bdd.T) { args.Intervals = []time.Duration{ 1 * time.Minute, 2 * time.Minute, @@ -412,17 +412,17 @@ func TestOption_SetValue(t *testing.T) { } err := option.SetValue("") - t.Run("then all previous values are deleted", func(t *testing.T) { + t.Then("all previous values are deleted", func(t *bdd.T) { require.That(t, err).IsNil() require.That(t, args.Intervals).IsEmpty() }) }) - t.Run("with invalid value", func(t *testing.T) { + t.Run("with invalid value", func(t *bdd.T) { args.Intervals = nil err := option.SetValue("1m,2p,3m") - t.Run("then all values are recorded", func(t *testing.T) { + t.Then("all values are recorded", func(t *bdd.T) { require.That(t, err).IsNotNil() require.That(t, args.Intervals).IsEmpty() }) @@ -455,3 +455,58 @@ func TestOption_SetValue_Ptr(t *testing.T) { verify.That(t, opt.ValueType).Eq(urlType) verify.That(t, opt.Type).Eq(option.Value) } + +// --------------------------------------------------------------------------- +// Option.SetValue() -- slice type +// --------------------------------------------------------------------------- + +func TestOption_SetValue_Slices(t *testing.T) { + bdd.Given(t, "an option set with a string-slice field", func(t *bdd.T) { + args := struct { + Default []string `opts:"-v, --values, sep:\\,, env:DEFAULT"` + KeepSpaces []string `opts:"-v, --values, sep:\\,, keep-spaces, env:KEEP_SPACES"` + KeepEmpty []string `opts:"-v, --values, sep:\\,, keep-empty, env:KEEP_EMPTY"` + KeepBoth []string `opts:"-v, --values, sep:\\,, keep-spaces,keep-empty, env:KEEP_BOTH"` + }{} + opts, err := option.NewOptionSet(&args) + require.That(t, err).IsNil() + require.That(t, opts).IsNotNil() + + t.When("using field with default options (trim spaces, discard empty)", func(t *bdd.T) { + opt := opts.Options[0] + opt.SetValue("aaa, bbb, ,") + + t.Then("spaces are trimmed from the individual values", func(t *bdd.T) { + verify.That(t, args.Default).Length().Eq(2) + verify.That(t, args.Default).Eq([]string{"aaa", "bbb"}) + }) + }) + t.When("using field with keep-spaces", func(t *bdd.T) { + opt := opts.Options[1] + opt.SetValue("aaa, bbb, ") + + t.Then("spaces are preserved for the individual values", func(t *bdd.T) { + verify.That(t, args.KeepSpaces).Length().Eq(3) + verify.That(t, args.KeepSpaces).Eq([]string{"aaa", " bbb", " "}) + }) + }) + t.When("using field with keep-empty", func(t *bdd.T) { + opt := opts.Options[2] + opt.SetValue("aaa, bbb,,") + + t.Then("spaces are trimmed but empty values are preserved", func(t *bdd.T) { + verify.That(t, args.KeepEmpty).Length().Eq(3) + verify.That(t, args.KeepEmpty).Eq([]string{"aaa", "bbb", ""}) + }) + }) + t.When("using field with keep-spaces and keep-empty", func(t *bdd.T) { + opt := opts.Options[3] + opt.SetValue("aaa, bbb,, ") + + t.Then("all values are preserved with their spaces", func(t *bdd.T) { + verify.That(t, args.KeepBoth).Length().Eq(4) + verify.That(t, args.KeepBoth).Eq([]string{"aaa", " bbb", "", " "}) + }) + }) + }) +}