-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.go
More file actions
72 lines (61 loc) · 1.42 KB
/
Copy pathcommands.go
File metadata and controls
72 lines (61 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/fatih/color"
)
type CmdFlags struct {
Add string
Del int
DelAll bool
Edit string
Toggle int
List bool
}
func NewCmdFlags() *CmdFlags {
cf := CmdFlags{}
flag.StringVar(&cf.Add, "add", "", "Add a new task by specifying title")
flag.StringVar(&cf.Edit, "edit", "", "Edit a task by index & specify a new title. id:new_title")
flag.IntVar(&cf.Del, "del", -1, "Specify a task by index to delete")
flag.BoolVar(&cf.DelAll, "deleteAll", false, "Delete all tasks from list")
flag.IntVar(&cf.Toggle, "toggle", -1, "Specify a task by index to toggle")
flag.BoolVar(&cf.List, "list", false, "List all Tasks")
flag.Parse()
return &cf
}
func (cf *CmdFlags) Execute(t *Tasks) {
switch {
case cf.List:
t.Print()
case cf.Add != "":
t.Add(cf.Add)
t.Print()
case cf.Edit != "":
parts := strings.SplitN(cf.Edit, ":", 2)
if len(parts) != 2 {
fmt.Println("Error, invalid format for edit. Please use id:new_title")
os.Exit(1)
}
index, err := strconv.Atoi(parts[0])
if err != nil {
fmt.Println("Error: Invalid Index for Edit")
os.Exit(1)
}
t.Edit(index, parts[1])
t.Print()
case cf.Toggle != -1:
t.Toggle(cf.Toggle)
t.Print()
case cf.Del != -1:
t.Delete(cf.Del)
t.Print()
case cf.DelAll == true:
t.DeleteAll()
color.Green("Deleted all Tasks Successfully.")
default:
fmt.Println("Invalid Command!")
}
}