Repository: projectdiscovery/goflags
Pull request #37 (2022-02-23 12:24:03)

Added non-lowercasing normalization helpers


Patch:
diff --git a/file_normalized_string_slice.go b/file_normalized_string_slice.go
index ff8e474..ad0761c 100644
--- a/file_normalized_string_slice.go
+++ b/file_normalized_string_slice.go
@@ -17,12 +17,39 @@ func (fileNormalizedStringSlice FileNormalizedStringSlice) String() string {
 	return ToString(fileNormalizedStringSlice)
 }
 
+// FileOriginalNormalizedStringSlice is a slice of strings without normalization
+type FileOriginalNormalizedStringSlice []string
+
+// Set appends a value to the string slice.
+func (fileNormalizedStringSlice *FileOriginalNormalizedStringSlice) Set(value string) error {
+	slice, err := ToFileOriginalNormalizedStringSlice(value)
+	if err != nil {
+		return err
+	}
+	*fileNormalizedStringSlice = append(*fileNormalizedStringSlice, slice...)
+	return nil
+}
+
+func (fileNormalizedStringSlice FileOriginalNormalizedStringSlice) String() string {
+	return ToString(fileNormalizedStringSlice)
+}
+
+var DefaultFileNormalizedStringSliceOptions = Options{
+	IsEmpty:    isEmpty,
+	Normalize:  normalizeLowercase,
+	IsFromFile: func(s string) bool { return true },
+}
+
 func ToFileNormalizedStringSlice(value string) ([]string, error) {
 	return toStringSlice(value, DefaultFileNormalizedStringSliceOptions)
 }
 
-var DefaultFileNormalizedStringSliceOptions = Options{
+var DefaultFileOriginalNormalizedStringSliceOptions = Options{
 	IsEmpty:    isEmpty,
 	Normalize:  normalize,
 	IsFromFile: func(s string) bool { return true },
 }
+
+func ToFileOriginalNormalizedStringSlice(value string) ([]string, error) {
+	return toStringSlice(value, DefaultFileOriginalNormalizedStringSliceOptions)
+}
diff --git a/goflags.go b/goflags.go
index 9cd65e5..b92240e 100644
--- a/goflags.go
+++ b/goflags.go
@@ -411,6 +411,41 @@ func (flagSet *FlagSet) NormalizedStringSliceVar(field *NormalizedStringSlice, l
 	return flagData
 }
 
+func (flagSet *FlagSet) NormalizedOriginalStringSliceVarP(field *NormalizedOriginalStringSlice, long, short string, defaultValue NormalizedStringSlice, usage string) *FlagData {
+	for _, item := range defaultValue {
+		_ = field.Set(item)
+	}
+
+	flagSet.CommandLine.Var(field, short, usage)
+	flagSet.CommandLine.Var(field, long, usage)
+
+	flagData := &FlagData{
+		usage:        usage,
+		short:        short,
+		long:         long,
+		defaultValue: defaultValue,
+	}
+	flagSet.flagKeys.Set(short, flagData)
+	flagSet.flagKeys.Set(long, flagData)
+	return flagData
+}
+
+func (flagSet *FlagSet) NormalizedOriginalStringSliceVar(field *NormalizedOriginalStringSlice, long string, defaultValue NormalizedStringSlice, usage string) *FlagData {
+	for _, item := range defaultValue {
+		_ = field.Set(item)
+	}
+
+	flagSet.CommandLine.Var(field, long, usage)
+
+	flagData := &FlagData{
+		usage:        usage,
+		long:         long,
+		defaultValue: defaultValue,
+	}
+	flagSet.flagKeys.Set(long, flagData)
+	return flagData
+}
+
 func (flagSet *FlagSet) FileNormalizedStringSliceVarP(field *FileNormalizedStringSlice, long, short string, defaultValue FileNormalizedStringSlice, usage string) *FlagData {
 	for _, item := range defaultValue {
 		_ = field.Set(item)
@@ -446,6 +481,41 @@ func (flagSet *FlagSet) FileNormalizedStringSliceVar(field *FileNormalizedString
 	return flagData
 }
 
+func (flagSet *FlagSet) FileNormalizedOriginalStringSliceVarP(field *FileOriginalNormalizedStringSlice, long, short string, defaultValue FileNormalizedStringSlice, usage string) *FlagData {
+	for _, item := range defaultValue {
+		_ = field.Set(item)
+	}
+
+	flagSet.CommandLine.Var(field, short, usage)
+	flagSet.CommandLine.Var(field, long, usage)
+
+	flagData := &FlagData{
+		usage:        usage,
+		short:        short,
+		long:         long,
+		defaultValue: defaultValue,
+	}
+	flagSet.flagKeys.Set(short, flagData)
+	flagSet.flagKeys.Set(long, flagData)
+	return flagData
+}
+
+func (flagSet *FlagSet) FileNormalizedOriginalStringSliceVar(field *FileOriginalNormalizedStringSlice, long string, defaultValue FileNormalizedStringSlice, usage string) *FlagData {
+	for _, item := range defaultValue {
+		_ = field.Set(item)
+	}
+
+	flagSet.CommandLine.Var(field, long, usage)
+
+	flagData := &FlagData{
+		usage:        usage,
+		long:         long,
+		defaultValue: defaultValue,
+	}
+	flagSet.flagKeys.Set(long, flagData)
+	return flagData
+}
+
 // StringSliceVarP adds a string slice flag with a shortname and longname
 // Supports ONE value at a time. Adding multiple values require repeating the argument (-flag value1 -flag value2)
 // No value normalization is happening.
diff --git a/normalized_string_slice.go b/normalized_string_slice.go
index aae8a6c..88e1b28 100644
--- a/normalized_string_slice.go
+++ b/normalized_string_slice.go
@@ -22,6 +22,32 @@ func ToNormalizedStringSlice(value string) ([]string, error) {
 }
 
 var DefaultNormalizedStringSliceOptions = Options{
+	IsEmpty:   isEmpty,
+	Normalize: normalizeLowercase,
+}
+
+// NormalizedOriginalStringSlice is a slice of strings
+type NormalizedOriginalStringSlice []string
+
+// Set appends a value to the string slice.
+func (normalizedStringSlice *NormalizedOriginalStringSlice) Set(value string) error {
+	slice, err := ToNormalizedOriginalStringSlice(value)
+	if err != nil {
+		return err
+	}
+	*normalizedStringSlice = append(*normalizedStringSlice, slice...)
+	return nil
+}
+
+func (normalizedStringSlice NormalizedOriginalStringSlice) String() string {
+	return ToString(normalizedStringSlice)
+}
+
+func ToNormalizedOriginalStringSlice(value string) ([]string, error) {
+	return toStringSlice(value, DefaultNormalizedOriginalStringSliceOptions)
+}
+
+var DefaultNormalizedOriginalStringSliceOptions = Options{
 	IsEmpty:   isEmpty,
 	Normalize: normalize,
 }
diff --git a/slice_common.go b/slice_common.go
index 08f8bef..78985aa 100644
--- a/slice_common.go
+++ b/slice_common.go
@@ -103,5 +103,9 @@ func isEmpty(s string) bool {
 }
 
 func normalize(s string) string {
+	return strings.TrimSpace(strings.Trim(strings.TrimSpace(s), string(quotes)))
+}
+
+func normalizeLowercase(s string) string {
 	return strings.TrimSpace(strings.Trim(strings.TrimSpace(strings.ToLower(s)), string(quotes)))
 }


Test patch:
diff --git a/normalized_string_slice_test.go b/normalized_string_slice_test.go
index 9ce98c7..08120ce 100644
--- a/normalized_string_slice_test.go
+++ b/normalized_string_slice_test.go
@@ -66,3 +66,13 @@ func TestNormalizedStringSliceNegative(t *testing.T) {
 		assert.NotNil(t, err)
 	}
 }
+
+func TestNormalizedOriginalStringSlice(t *testing.T) {
+	result, err := ToNormalizedOriginalStringSlice("/Users/Home/Test/test.yaml")
+	assert.Nil(t, err)
+	assert.Equal(t, []string{"/Users/Home/Test/test.yaml"}, result, "could not get correct path")
+
+	result, err = ToNormalizedOriginalStringSlice("'test user'")
+	assert.Nil(t, err)
+	assert.Equal(t, []string{"test user"}, result, "could not get correct path")
+}