Skip to content

Commit

Permalink
Fix error for list value - returns one empty item for a list when dat…
Browse files Browse the repository at this point in the history
…a is not passed to the command line
  • Loading branch information
goloop committed Jun 9, 2022
1 parent 2af0e6b commit 54b3330
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 6 additions & 4 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ func (am argMap) posValues() []string {
}

// The flagValue returns the value for the specified flag by
// long and/or short name. If the value is not found - returns defValue.
// long and/or short name with true as second param.
// Returns defValue with false as second param if the value
// is not found.
func (am argMap) flagValue(
shortFlag string,
longFlag string,
defValue string,
sepList string,
) []string {
) ([]string, bool) {
var result []string

// Join all values from all types of flags (long/short).
Expand All @@ -296,7 +298,7 @@ func (am argMap) flagValue(

// Return default value.
if len(tmp) == 0 {
return []string{defValue}
return []string{defValue}, false
}

// Be sure to set the sequence of values that was in the command line.
Expand All @@ -313,5 +315,5 @@ func (am argMap) flagValue(
result = append(result, item.value)
}

return result
return result, true
}
8 changes: 4 additions & 4 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestPositional(t *testing.T) {
}

// Check app name.
value := am.flagValue("0", "", "", "")
value, _ := am.flagValue("0", "", "", "")
if r := strings.Join(value, ""); r != tests[0][0] {
t.Errorf("%d test, expected %v but %v", i, tests[0][0], r)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestValue(t *testing.T) {
t.Error(err)
}

r := am.flagValue("U", "users", "Jan,Bob", "")
r, _ := am.flagValue("U", "users", "Jan,Bob", "")
if !reflect.DeepEqual(r, expected) {
t.Errorf("expected %v but %v", expected, r)
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestSplit(t *testing.T) {
t.Error(err)
}

r := am.flagValue("U", "users", "Jan,Bob,Roy", "")
r, _ := am.flagValue("U", "users", "Jan,Bob,Roy", "")
if !equal(r, expected) {
t.Errorf("test %d, expected %v but %v", i, expected, r)
}
Expand All @@ -201,7 +201,7 @@ func TestSplitFolding(t *testing.T) {
t.Error(err)
}

r := am.flagValue("U", "users", "Jan,Bob,Roy", "")
r, _ := am.flagValue("U", "users", "Jan,Bob,Roy", "")
if !reflect.DeepEqual(r, expected) {
t.Errorf("test %d, expected %v but %v", i, expected, r)
}
Expand Down
20 changes: 18 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func unmarshalOpt(obj interface{}, args []string) []error {
)
arg = strings.Trim(arg, " or/and ")

value, kind := []string{}, fc.item.Kind()
value, kind, ok := []string{}, fc.item.Kind(), false
switch f := fc.tagGroup.shortFlag; {
case f == "[]":
// Get positional arguments.
value = am.posValues()
default:
// Get the values of the argument.
value = am.flagValue(
value, ok = am.flagValue(
fc.tagGroup.shortFlag,
fc.tagGroup.longFlag,
fc.tagGroup.defValue,
Expand All @@ -106,6 +106,14 @@ func unmarshalOpt(obj interface{}, args []string) []error {
// If a separator is specified, the elements must be separated.
var result []string

// If the argMap.flagValue hasn't value it's returns
// []string{defValue} where defValue can be like "" -
// this is not valid for the list because if the command line
// argument has no data for the list this list must be empty!
if !ok && len(value) == 1 && value[0] == "" {
break
}

if sep := fc.tagGroup.sepList; sep != "" {
for _, item := range value {
tmp := strings.Split(item, sep)
Expand Down Expand Up @@ -134,6 +142,14 @@ func unmarshalOpt(obj interface{}, args []string) []error {
// If a separator is specified, the elements must be separated.
var result []string

// If the argMap.flagValue hasn't value it's returns
// []string{defValue} where defValue can be like "" -
// this is not valid for the list because if the command line
// argument has no data for the list this list must be empty!
if !ok && len(value) == 1 && value[0] == "" {
break
}

if sep := fc.tagGroup.sepList; sep != "" {
for _, item := range value {
tmp := strings.Split(item, sep)
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package opt implements methods for manage arguments of the command-line.
package opt

const version = "1.0.1"
const version = "1.0.2"

// Version returns the version of the module.
func Version() string {
Expand Down

0 comments on commit 54b3330

Please sign in to comment.