-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: command action & context & regexp
- Loading branch information
Showing
11 changed files
with
174 additions
and
31 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
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
package commander | ||
|
||
import "fmt" | ||
|
||
type Context struct { | ||
Doc DocoptMap | ||
Args ContextArgs | ||
Doc DocoptMap | ||
} | ||
|
||
func newContext(d DocoptMap) *Context { | ||
func newContext(args []string, d DocoptMap) *Context { | ||
return &Context{ | ||
Doc: d, | ||
Args: ContextArgs(args), | ||
Doc: d, | ||
} | ||
} | ||
|
||
func (c Context) String() string { | ||
return fmt.Sprintf("%#v", c) | ||
} |
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,26 @@ | ||
package commander | ||
|
||
import "strings" | ||
|
||
type ContextArgs []string | ||
|
||
func (c ContextArgs) Get(index int) string { | ||
if index >= 0 && index < len(c) { | ||
return c[index] | ||
} | ||
return "" | ||
} | ||
|
||
func (c ContextArgs) String(offsets ...int) string { | ||
var offset int = 0 | ||
if len(offsets) != 0 { | ||
offset = offsets[0] | ||
} | ||
if len(c) <= offset { | ||
offset = len(c) - 1 | ||
} | ||
if offset < 0 { | ||
offset = 0 | ||
} | ||
return strings.Join(c[offset:], "") | ||
} |
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,17 @@ | ||
package commander | ||
|
||
import "regexp" | ||
|
||
func RegexpCommand(str string) []string { | ||
return regexp.MustCompile(`[A-Za-z0-9_-]+`).FindAllString( | ||
regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str), -1) | ||
} | ||
func RegexpArgument(str string) []string { | ||
return regexp.MustCompile(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`). | ||
FindAllString(str, -1) | ||
} | ||
func RegexpOption(str string) []string { | ||
return regexp.MustCompile(`-{1,2}[A-Za-z0-9_-]+\b`). | ||
FindAllString(regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`). | ||
ReplaceAllString(str, ""), -1) | ||
} |
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,63 @@ | ||
package commander | ||
|
||
import ( | ||
"github.com/WindomZ/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRegexpCommand(t *testing.T) { | ||
assert.Equal(t, | ||
RegexpCommand("new <name>"), | ||
[]string{"new"}, | ||
) | ||
assert.Equal(t, | ||
RegexpCommand("ship <name> move <x> <y>"), | ||
[]string{"ship"}, | ||
) | ||
assert.Equal(t, | ||
RegexpCommand("(set|remove) <x> <y> [--moored|--drifting]"), | ||
[]string{"set", "remove"}, | ||
) | ||
} | ||
|
||
func TestRegexpArgument(t *testing.T) { | ||
assert.Equal(t, | ||
RegexpArgument("new <name>"), | ||
[]string{"<name>"}, | ||
) | ||
assert.Equal(t, | ||
RegexpArgument("ship <name> move <x> <y>"), | ||
[]string{"<name>", "<x>", "<y>"}, | ||
) | ||
assert.Equal(t, | ||
RegexpArgument("(set|remove) <x> <y> [--moored|--drifting]"), | ||
[]string{"<x>", "<y>"}, | ||
) | ||
} | ||
|
||
func TestRegexpOption(t *testing.T) { | ||
assert.Equal(t, | ||
RegexpOption("new <name>"), | ||
[]string(nil), | ||
) | ||
assert.Equal(t, | ||
RegexpOption("-p <x-y>"), | ||
[]string{"-p"}, | ||
) | ||
assert.Equal(t, | ||
RegexpOption("-p"), | ||
[]string{"-p"}, | ||
) | ||
assert.Equal(t, | ||
RegexpOption("-p, --pepper"), | ||
[]string{"-p", "--pepper"}, | ||
) | ||
assert.Equal(t, | ||
RegexpOption("--pepper"), | ||
[]string{"--pepper"}, | ||
) | ||
assert.Equal(t, | ||
RegexpOption("(set|remove) <x> <y> [--not-ss | -a | --moored|--drifting]"), | ||
[]string{"--not-ss", "-a", "--moored", "--drifting"}, | ||
) | ||
} |
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,17 +1,13 @@ | ||
package tests | ||
|
||
import "regexp" | ||
import "github.com/WindomZ/go-commander" | ||
|
||
func RegexpCommand(str string) []string { | ||
return regexp.MustCompile(`[A-Za-z0-9_-]+\b`).FindAllString( | ||
regexp.MustCompile(`^[A-Za-z0-9_|\(\)\s-]+`).FindString(str), -1) | ||
return commander.RegexpCommand(str) | ||
} | ||
func RegexpArgument(str string) []string { | ||
return regexp.MustCompile(`(?i:<|\[)[A-Za-z0-9_\[\]<>-]+\b(?i:>|])`). | ||
FindAllString(str, -1) | ||
return commander.RegexpArgument(str) | ||
} | ||
func RegexpOption(str string) []string { | ||
return regexp.MustCompile(`-{1,2}[A-Za-z0-9_-]+\b`). | ||
FindAllString(regexp.MustCompile(`(<|\[)[A-Za-z0-9_\[\]<>-]+\b(>|])`). | ||
ReplaceAllString(str, ""), -1) | ||
return commander.RegexpOption(str) | ||
} |