diff options
| author | Michael Strùˆder <mikezter@gmail.com> | 2017-05-03 15:18:13 +0200 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2017-05-09 20:20:24 -0400 |
| commit | fdca747e173c20f59901207c3f1887cece2a9351 (patch) | |
| tree | 7eda8ee6456d3bb89c980bbec9bcfcaacd06dfdd /todolist/parser.go | |
| parent | 2c5512341a8c87dfe4d37106a1ecc02784e84da6 (diff) | |
subcommands like delete don't take a subject, so make it optional
Diffstat (limited to 'todolist/parser.go')
| -rw-r--r-- | todolist/parser.go | 18 |
1 files changed, 12 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 { |
