Skip to content

Commit

Permalink
Merge pull request #101 from projectdiscovery/check_comma_in_quotes
Browse files Browse the repository at this point in the history
FileStringSliceOption read the cli input as rawstring if not file
  • Loading branch information
Mzack9999 committed Nov 24, 2022
2 parents 7cb1210 + 76b9370 commit 4a427c3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 9 additions & 3 deletions slice_common.go
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
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
@@ -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

0 comments on commit 4a427c3

Please sign in to comment.