From 2a334387836af7d87e97d661c4f4f41186228385 Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Mon, 19 Feb 2024 18:20:48 -0700 Subject: [PATCH] Fix "add --project-name" and check for exactly one positional argument #252 --- add.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/add.go b/add.go index 5f4932a..825f6ba 100644 --- a/add.go +++ b/add.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "strings" "github.com/sachaos/todoist/lib" @@ -19,16 +20,25 @@ func Add(c *cli.Context) error { client := GetClient(c) item := todoist.Item{} - if !c.Args().Present() { - return CommandFailed + if c.Args().Len() != 1 { + return fmt.Errorf("add command requires 1 positional argument for the task title, but got %v.", c.Args().Len()) } item.Content = c.Args().First() + item.Priority = priorityMapping[c.Int("priority")] - item.ProjectID = c.String("project-id") - if item.ProjectID == "" { - item.ProjectID = client.Store.Projects.GetIDByName(c.String("project-name")) + + projectName := c.String("project-name") + if projectName != "" { + projectId := client.Store.Projects.GetIDByName(projectName) + if projectId == "" { + return fmt.Errorf("Did not find a project named '%v'", projectName) + } + item.ProjectID = projectId + } else { + item.ProjectID = c.String("project-id") } + item.LabelNames = func(str string) []string { stringNames := strings.Split(str, ",") names := []string{}