-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support sorting member lags by value (#39)
* Update cli parsing * Update command parsing * Check flags more strictly * Add checkArgs tests * Bump version
- Loading branch information
1 parent
5936120
commit d7f7c32
Showing
6 changed files
with
237 additions
and
58 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type replCommand struct { | ||
args []string | ||
flags map[string]string | ||
} | ||
|
||
func (r replCommand) getBoolValue(key string) bool { | ||
value, ok := r.flags[key] | ||
|
||
if value == "true" { | ||
return true | ||
} else if value == "" && ok { | ||
// If key is set but value is not, treat this as "true" | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
func (r replCommand) checkArgs( | ||
minArgs int, | ||
maxArgs int, | ||
allowedFlags map[string]struct{}, | ||
) error { | ||
if minArgs == maxArgs { | ||
if len(r.args) != minArgs { | ||
return fmt.Errorf("Expected %d args", minArgs) | ||
} | ||
} else { | ||
if len(r.args) < minArgs || len(r.args) > maxArgs { | ||
return fmt.Errorf("Expected between %d and %d args", minArgs, maxArgs) | ||
} | ||
} | ||
|
||
for key := range r.flags { | ||
if allowedFlags == nil { | ||
return fmt.Errorf("Flag %s not recognized", key) | ||
} | ||
if _, ok := allowedFlags[key]; !ok { | ||
return fmt.Errorf("Flag %s not recognized", key) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseReplInputs(input string) replCommand { | ||
args := []string{} | ||
flags := map[string]string{} | ||
|
||
components := strings.Split(input, " ") | ||
|
||
for c, component := range components { | ||
if component == "" { | ||
continue | ||
} else if c > 0 && strings.HasPrefix(component, "--") { | ||
subcomponents := strings.SplitN(component, "=", 2) | ||
key := subcomponents[0][2:] | ||
var value string | ||
if len(subcomponents) > 1 { | ||
value = subcomponents[1] | ||
} | ||
flags[key] = value | ||
} else { | ||
args = append(args, component) | ||
} | ||
} | ||
|
||
return replCommand{ | ||
args: args, | ||
flags: flags, | ||
} | ||
} |
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,71 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseReplInputs(t *testing.T) { | ||
assert.Equal( | ||
t, | ||
replCommand{ | ||
args: []string{"arg1", "arg2"}, | ||
flags: map[string]string{}, | ||
}, | ||
parseReplInputs("arg1 arg2"), | ||
) | ||
assert.Equal( | ||
t, | ||
replCommand{ | ||
args: []string{"--flag1=value1", "arg1", "arg2"}, | ||
flags: map[string]string{}, | ||
}, | ||
parseReplInputs("--flag1=value1 arg1 arg2"), | ||
) | ||
assert.Equal( | ||
t, | ||
replCommand{ | ||
args: []string{"arg1", "arg2", "arg3"}, | ||
flags: map[string]string{ | ||
"flag1": "value1", | ||
"flag2": "value2", | ||
}, | ||
}, | ||
parseReplInputs("arg1 arg2 --flag1=value1 arg3 --flag2=value2"), | ||
) | ||
} | ||
|
||
func TestGetBoolValue(t *testing.T) { | ||
command := replCommand{ | ||
flags: map[string]string{ | ||
"key1": "", | ||
"key2": "true", | ||
"key3": "false", | ||
}, | ||
} | ||
assert.True(t, command.getBoolValue("key1")) | ||
assert.True(t, command.getBoolValue("key2")) | ||
assert.False(t, command.getBoolValue("key3")) | ||
assert.False(t, command.getBoolValue("non-existent-key")) | ||
} | ||
|
||
func TestCheckArgs(t *testing.T) { | ||
command := replCommand{ | ||
args: []string{ | ||
"arg1", | ||
"arg2", | ||
}, | ||
flags: map[string]string{ | ||
"key1": "value1", | ||
}, | ||
} | ||
assert.NoError(t, command.checkArgs(2, 2, map[string]struct{}{"key1": {}})) | ||
assert.NoError(t, command.checkArgs(2, 3, map[string]struct{}{"key1": {}})) | ||
assert.NoError(t, command.checkArgs(1, 2, map[string]struct{}{"key1": {}})) | ||
assert.NoError(t, command.checkArgs(1, 2, map[string]struct{}{"key1": {}, "key2": {}})) | ||
assert.Error(t, command.checkArgs(3, 3, map[string]struct{}{"key1": {}})) | ||
assert.Error(t, command.checkArgs(3, 5, map[string]struct{}{"key1": {}})) | ||
assert.Error(t, command.checkArgs(2, 2, map[string]struct{}{"key2": {}})) | ||
assert.Error(t, command.checkArgs(2, 2, nil)) | ||
} |
Oops, something went wrong.