From fdca747e173c20f59901207c3f1887cece2a9351 Mon Sep 17 00:00:00 2001 From: Michael Strùˆder Date: Wed, 3 May 2017 15:18:13 +0200 Subject: subcommands like delete don't take a subject, so make it optional --- todolist/parser.go | 18 ++++++++++++------ todolist/parser_test.go | 10 ++++++++++ 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"} -- cgit v1.3