-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from projectdiscovery/add_enumslicevar_flag
add EnumSliceVar flag
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# IDE Settings | ||
/.idea | ||
/.vscode | ||
/.vs | ||
/.vs | ||
|
||
examples/basic/basic | ||
examples/basic/basic.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package goflags | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type EnumSliceVar struct { | ||
allowedTypes AllowdTypes | ||
value *[]string | ||
} | ||
|
||
func (e *EnumSliceVar) String() string { | ||
if e.value != nil { | ||
return strings.Join(*e.value, ",") | ||
} | ||
return "" | ||
} | ||
|
||
func (e *EnumSliceVar) Set(value string) error { | ||
values := strings.Split(value, ",") | ||
for _, v := range values { | ||
_, ok := e.allowedTypes[v] | ||
if !ok { | ||
return fmt.Errorf("allowed values are %v", e.allowedTypes.String()) | ||
} | ||
} | ||
*e.value = values | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package goflags | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var enumSliceData []string | ||
|
||
func TestEnumSliceVar(t *testing.T) { | ||
t.Run("Test with single value", func(t *testing.T) { | ||
flagSet := NewFlagSet() | ||
flagSet.EnumSliceVar(&enumSliceData, "enum", []EnumVariable{Type1}, "enum", AllowdTypes{"type1": Type1, "type2": Type2}) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"--enum", "type1", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"type1"}, enumSliceData) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("Test with multiple value", func(t *testing.T) { | ||
flagSet := NewFlagSet() | ||
flagSet.EnumSliceVar(&enumSliceData, "enum", []EnumVariable{Type1}, "enum", AllowdTypes{"type1": Type1, "type2": Type2}) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"--enum", "type1,type2", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"type1", "type2"}, enumSliceData) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("Test with invalid value", func(t *testing.T) { | ||
if os.Getenv("IS_SUB_PROCESS") == "1" { | ||
flagSet := NewFlagSet() | ||
|
||
flagSet.EnumSliceVar(&enumSliceData, "enum", []EnumVariable{Nil}, "enum", AllowdTypes{"type1": Type1, "type2": Type2}) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"--enum", "type3", | ||
} | ||
_ = flagSet.Parse() | ||
return | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestFailEnumVar") | ||
cmd.Env = append(os.Environ(), "IS_SUB_PROCESS=1") | ||
err := cmd.Run() | ||
if e, ok := err.(*exec.ExitError); ok && !e.Success() { | ||
return | ||
} | ||
t.Fatalf("process ran with err %v, want exit error", err) | ||
tearDown(t.Name()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters