aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/parser.go
diff options
context:
space:
mode:
authorMichael Strùˆder <mikezter@gmail.com>2017-05-03 10:42:15 +0200
committerGrant Ammons <gammons@gmail.com>2017-05-09 20:20:24 -0400
commitbb2c579f02e9d273058a6d4d55593622923aaf9f (patch)
treea524447d7095e712ffdf57af185cdc7f6894beb5 /todolist/parser.go
parentd2df88f217cef638bdca527794969fcaa835bfd7 (diff)
allow any whitespace between subcommand, id and input
Diffstat (limited to 'todolist/parser.go')
-rw-r--r--todolist/parser.go16
1 files changed, 9 insertions, 7 deletions
diff --git a/todolist/parser.go b/todolist/parser.go
index 38bdb7f..616e82f 100644
--- a/todolist/parser.go
+++ b/todolist/parser.go
@@ -30,18 +30,20 @@ func (p *Parser) ParseNewTodo(input string) *Todo {
return todo
}
-func (p Parser) Parse() (string, int, string) {
- input := p.input
- r := regexp.MustCompile(`(\w+) (\d+) (.*)`)
- matches := r.FindStringSubmatch(input)
+// 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+(.*)`)
+ matches := r.FindStringSubmatch(p.input)
if len(matches) < 4 {
fmt.Println("Could match command, id or subject")
- return "", -1, input
+ return "", -1, ""
}
+
+ // because of the regexp match, this can never fail
id, err := strconv.Atoi(matches[2])
if err != nil {
- fmt.Println("Invalid id.")
- return "", -1, input
+ panic(err)
}
return matches[1], id, matches[3]