Repository navigation
Expand file tree
/
Copy pathtask_cli.py
More file actions
211 lines (158 loc) · 4.82 KB
/
Copy pathtask_cli.py
File metadata and controls
211 lines (158 loc) · 4.82 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import json
import os
import sys
from datetime import datetime, timezone
TASK_DIR = "task"
TASK_FILE = os.path.join(TASK_DIR, "task.json")
VALID_STATUSES = {"todo", "in-progress", "done"}
def now_iso():
return datetime.now(timezone.utc).isoformat()
def ensure_task_file():
os.makedirs(TASK_DIR, exist_ok=True)
if not os.path.exists(TASK_FILE):
with open(TASK_FILE, "w", encoding="utf-8") as f:
json.dump([], f, indent=2)
def load_tasks():
ensure_task_file()
try:
with open(TASK_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
return data
except json.JSONDecodeError:
pass
return []
def save_tasks(tasks):
with open(TASK_FILE, "w", encoding="utf-8") as f:
json.dump(tasks, f, indent=2)
def next_id(tasks):
return max((task.get("id", 0) for task in tasks), default=0) + 1
def find_task(tasks, task_id):
for task in tasks:
if task.get("id") == task_id:
return task
return None
def add_task(description):
tasks = load_tasks()
task = {
"id": next_id(tasks),
"description": description,
"status": "todo",
"createdAt": now_iso(),
"updatedAt": now_iso(),
}
tasks.append(task)
save_tasks(tasks)
print(f"Task added successfully (ID: {task['id']})")
def update_task(task_id, description):
tasks = load_tasks()
task = find_task(tasks, task_id)
if not task:
print(f"Task with ID {task_id} not found.")
return
task["description"] = description
task["updatedAt"] = now_iso()
save_tasks(tasks)
print(f"Task updated successfully (ID: {task_id})")
def delete_task(task_id):
tasks = load_tasks()
updated_tasks = [task for task in tasks if task.get("id") != task_id]
if len(updated_tasks) == len(tasks):
print(f"Task with ID {task_id} not found.")
return
save_tasks(updated_tasks)
print(f"Task deleted successfully (ID: {task_id})")
def mark_task(task_id, status):
tasks = load_tasks()
task = find_task(tasks, task_id)
if not task:
print(f"Task with ID {task_id} not found.")
return
task["status"] = status
task["updatedAt"] = now_iso()
save_tasks(tasks)
if status == "done":
print(f"Task marked as done (ID: {task_id})")
else:
print(f"Task marked as in progress (ID: {task_id})")
def list_tasks(status_filter=None):
tasks = load_tasks()
if status_filter:
tasks = [task for task in tasks if task.get("status") == status_filter]
if not tasks:
print("No tasks found.")
return
for task in tasks:
print(
f"[{task.get('id')}] {task.get('description')} "
f"({task.get('status')})"
)
def parse_task_id(value):
try:
return int(value)
except ValueError:
print("Task ID must be a number.")
return None
def main():
if len(sys.argv) < 2:
print(
"Usage: task-cli add|update|delete|mark-in-progress|mark-done|list ..."
)
return
command = sys.argv[1]
if command == "add":
if len(sys.argv) < 3:
print("Usage: task-cli add \"description\"")
return
add_task(" ".join(sys.argv[2:]))
return
if command == "update":
if len(sys.argv) < 4:
print('Usage: task-cli update <id> "new description"')
return
task_id = parse_task_id(sys.argv[2])
if task_id is None:
return
update_task(task_id, " ".join(sys.argv[3:]))
return
if command == "delete":
if len(sys.argv) < 3:
print("Usage: task-cli delete <id>")
return
task_id = parse_task_id(sys.argv[2])
if task_id is None:
return
delete_task(task_id)
return
if command == "mark-in-progress":
if len(sys.argv) < 3:
print("Usage: task-cli mark-in-progress <id>")
return
task_id = parse_task_id(sys.argv[2])
if task_id is None:
return
mark_task(task_id, "in-progress")
return
if command == "mark-done":
if len(sys.argv) < 3:
print("Usage: task-cli mark-done <id>")
return
task_id = parse_task_id(sys.argv[2])
if task_id is None:
return
mark_task(task_id, "done")
return
if command == "list":
if len(sys.argv) == 2:
list_tasks()
return
status = sys.argv[2]
if status not in VALID_STATUSES:
print("Usage: task-cli list [todo|in-progress|done]")
return
list_tasks(status)
return
print(f"Unknown command: {command}")
if __name__ == "__main__":
main()