aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
Diffstat (limited to 'todolist')
-rw-r--r--todolist/parser.go18
-rw-r--r--todolist/parser_test.go10
2 files changed, 22 insertions, 6 deletions
diff --git a/todolist/parser.go b/todolist/parser.go
index 616e82f..3598e12 100644
--- a/todolist/parser.go
+++ b/todolist/parser.go
@@ -31,22 +31,28 @@ func (p *Parser) ParseNewTodo(input string) *Todo {
}
// Parse accepts user input and splits it into subcommand, the todo id to
-// work on and the input to the subcommand function.
-func (p Parser) Parse() (subcommand string, id int, input string) {
- r := regexp.MustCompile(`(\w+)\s+(\d+)\s+(.*)`)
+// work on and the subject for the subcommand function.
+func (p Parser) Parse() (subcommand string, id int, subject string) {
+ r := regexp.MustCompile(`(\w+)\s+(\d+)(\s+(.*))?`)
matches := r.FindStringSubmatch(p.input)
- if len(matches) < 4 {
- fmt.Println("Could match command, id or subject")
+ if len(matches) < 3 {
+ fmt.Println("Could match command or id")
return "", -1, ""
}
+ subcommand = matches[1]
+
// because of the regexp match, this can never fail
id, err := strconv.Atoi(matches[2])
if err != nil {
panic(err)
}
- return matches[1], id, matches[3]
+ if len(matches) == 5 {
+ subject = matches[4]
+ }
+
+ return
}
func (p *Parser) Subject(input string) string {
diff --git a/todolist/parser_test.go b/todolist/parser_test.go
index 05f390a..4466489 100644
--- a/todolist/parser_test.go
+++ b/todolist/parser_test.go
@@ -170,6 +170,16 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) {
assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime))
}
+func TestParseCommandIdSubjectOptionalSubject(t *testing.T) {
+ assert := assert.New(t)
+ parser := Parser{"d 24"}
+ command, id, subject := parser.Parse()
+
+ assert.Equal("d", command)
+ assert.Equal(24, id)
+ assert.Equal("", subject)
+}
+
func TestParseCommandIdSubjectWhitespace(t *testing.T) {
assert := assert.New(t)
parser := Parser{"es 24\t a new subject"}