Skip to content

Commit

Permalink
Edge case coverage, reworked regex with groups
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <derek.nola@suse.com>
  • Loading branch information
dereknola committed Nov 3, 2021
1 parent fbc0683 commit acc1c71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
20 changes: 12 additions & 8 deletions pkg/configfilearg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,16 @@ func (p *Parser) findStart(args []string) ([]string, []string, bool) {
}

afterIndex := make(map[string]int)
re, err := regexp.Compile(`:\d`)
re, err := regexp.Compile(`(.+):(\d)+`)
if err != nil {
return args, nil, false
}
// After keywords ending with ":<NUM>" will set + NUM of arguments as the split point.
// After keywords ending with ":<NUM>" can set + NUM of arguments as the split point.
// used for matching on subcommmands
for i, arg := range p.After {
if re.MatchString(arg) {
split := strings.Split(arg, ":")
p.After[i] = split[0]
afterIndex[split[0]], err = strconv.Atoi(split[1])
if match := re.FindAllStringSubmatch(arg, -1); match != nil {
p.After[i] = match[0][1]
afterIndex[match[0][1]], err = strconv.Atoi(match[0][2])
if err != nil {
return args, nil, false
}
Expand All @@ -124,8 +123,13 @@ func (p *Parser) findStart(args []string) ([]string, []string, bool) {
for i, val := range args {
for _, test := range p.After {
if val == test {
skip := afterIndex[test] + 1
return args[0 : i+skip], args[i+skip:], true
if skip := afterIndex[test]; skip != 0 {
if len(args) <= i+skip || strings.HasPrefix(args[i+skip], "-") {
return args[0 : i+1], args[i+1:], true
}
return args[0 : i+skip+1], args[i+skip+1:], true
}
return args[0 : i+1], args[i+1:], true
}
}
}
Expand Down
29 changes: 25 additions & 4 deletions pkg/configfilearg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,35 @@ func Test_UnitParser_findStart(t *testing.T) {
found: false,
},
{
name: "commands and subcommands that both match",
args: []string{"etcd-snapshot", "list", "foo", "bar"},
name: "command (with optional subcommands) but no flags",
args: []string{"etcd-snapshot"},
prefix: []string{"etcd-snapshot"},
suffix: []string{},
found: true,
},
{
name: "command (with optional subcommands) and flags",
args: []string{"etcd-snapshot", "-f"},
prefix: []string{"etcd-snapshot"},
suffix: []string{"-f"},
found: true,
},
{
name: "command and subcommand with no flags",
args: []string{"etcd-snapshot", "list"},
prefix: []string{"etcd-snapshot", "list"},
suffix: []string{"foo", "bar"},
suffix: []string{},
found: true,
},
{
name: "command and subcommand with flags",
args: []string{"etcd-snapshot", "list", "-f"},
prefix: []string{"etcd-snapshot", "list"},
suffix: []string{"-f"},
found: true,
},
{
name: "commands and too many subcommands",
name: "command and too many subcommands",
args: []string{"etcd-snapshot", "list", "delete", "foo", "bar"},
prefix: []string{"etcd-snapshot", "list"},
suffix: []string{"delete", "foo", "bar"},
Expand Down

0 comments on commit acc1c71

Please sign in to comment.