Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileStringSliceOption read the cli input as rawstring if not file #101

Merged
merged 6 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions slice_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ func ToString(slice []string) string {
}

type Options struct {
// IsFromFile determines if the values are from file
IsFromFile func(string) bool
IsEmpty func(string) bool
Normalize func(string) string
// IsEmpty determines if the values are empty
IsEmpty func(string) bool
// Normalize the value (eg. removing trailing spaces)
Normalize func(string) string
// RawString determines if the value should be considered as is
RawString bool
}

// ToStringSlice converts a value to string slice based on options
Expand All @@ -74,6 +79,8 @@ func ToStringSlice(value string, options Options) ([]string, error) {
for line := range linesChan {
addPartToResult(line)
}
} else if options.RawString {
addPartToResult(value)
} else {
index := 0
for index < len(value) {
Expand Down Expand Up @@ -101,7 +108,6 @@ func ToStringSlice(value string, options Options) ([]string, error) {
}
}
}

return result, nil
}

Expand Down
1 change: 1 addition & 0 deletions string_slice_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var FileStringSliceOptions = Options{
IsEmpty: isEmpty,
Normalize: normalizeTrailingParts,
IsFromFile: isFromFile,
RawString: true,
}

// NormalizedStringSliceOptions represents a list of items
Expand Down
7 changes: 5 additions & 2 deletions string_slice_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goflags

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -46,7 +45,6 @@ func TestNormalizedStringSlicePositive(t *testing.T) {

for value, expected := range slices {
result, err := ToStringSlice(value, NormalizedStringSliceOptions)
fmt.Println(result)
assert.Nil(t, err)
assert.Equal(t, result, expected)
}
Expand Down Expand Up @@ -96,6 +94,11 @@ func TestFileStringSliceOptions(t *testing.T) {
result, err := ToStringSlice(filename, FileStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"value1,value2", "value3"}, result, "could not get correct path")

// command line input value
result, err = ToStringSlice("string:\"contains, comma and quotes.\"", FileStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"string:\"contains, comma and quotes.\""}, result, "could not get correct path")
}

func TestFileNormalizedOriginalStringSliceOptions(t *testing.T) {
Expand Down