Skip to content

Commit

Permalink
Change handling of spaces and empty list values (#10)
Browse files Browse the repository at this point in the history
Change handling of spaces and empty list values:

- add `keep-spaces` and `keep-empty` options to struct tags to restore prior behavior
- add `string.TrimSpaces()` before parsing argument value if `keep-spaces` is not specified
- add `string.TrimSpaces()` on individual list values if `keep-spaces` is not specified
- drop empty list values if `keep-empty` is not specified
- Update `README.md` to describe new behavior
- Update `RELEASES.md`  with new spaces behavior for upcoming v0.5.0
- Update go.mod packages to latest
  • Loading branch information
maargenton authored Sep 16, 2023
1 parent 2090d7e commit 9f6ceb7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 74 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# v0.5.0
# v0.5.0-rc

## Key Features

Expand All @@ -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

Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
49 changes: 34 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
18 changes: 18 additions & 0 deletions pkg/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -161,13 +163,19 @@ 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 {
err = opt.setSliceValue(fv, s)
} else {
err = value.Parse(fv.Addr().Interface(), s)
}

if err != nil {
err = fmt.Errorf("failed to set value for '%v': %w", opt.Name(), err)
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit 9f6ceb7

Please sign in to comment.