-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_cli.py
More file actions
297 lines (233 loc) · 7.47 KB
/
Copy pathtask_cli.py
File metadata and controls
297 lines (233 loc) · 7.47 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
"""
Task Tracker CLI (no external libraries)
Usage examples:
python task_cli.py add "Buy groceries"
python task_cli.py update 1 "Buy groceries and cook dinner"
python task_cli.py delete 1
python task_cli.py mark-in-progress 1
python task_cli.py mark-done 1
python task_cli.py list
python task_cli.py list done
python task_cli.py list todo
python task_cli.py list in-progress
"""
import json
import os
import sys
from datetime import datetime
DB_FILE = "tasks.json"
VALID_STATUSES = {"todo", "in-progress", "done"}
def now_iso() -> str:
# ISO 8601 with seconds, local time
return datetime.now().replace(microsecond=0).isoformat()
def print_usage() -> None:
print(
"Task Tracker CLI\n"
"Commands:\n"
" add <description>\n"
" update <id> <new_description>\n"
" delete <id>\n"
" mark-in-progress <id>\n"
" mark-done <id>\n"
" list [todo|in-progress|done]\n"
"\n"
"Examples:\n"
' python task_cli.py add "Buy groceries"\n'
' python task_cli.py update 1 "Buy groceries and cook dinner"\n'
" python task_cli.py delete 1\n"
" python task_cli.py mark-in-progress 1\n"
" python task_cli.py mark-done 1\n"
" python task_cli.py list\n"
" python task_cli.py list done\n"
)
def ensure_db_exists() -> None:
if not os.path.exists(DB_FILE):
try:
with open(DB_FILE, "w", encoding="utf-8") as f:
json.dump([], f, indent=2)
except OSError as e:
print(f"Error: could not create {DB_FILE}: {e}")
sys.exit(1)
def load_tasks() -> list[dict]:
ensure_db_exists()
try:
with open(DB_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
# Validate basic structure
if not isinstance(data, list):
raise ValueError("Database format invalid (expected a list).")
# Filter to only dict tasks (defensive)
tasks = [t for t in data if isinstance(t, dict)]
return tasks
except json.JSONDecodeError:
print(f"Error: {DB_FILE} is not valid JSON. Fix it or delete it and rerun.")
sys.exit(1)
except (OSError, ValueError) as e:
print(f"Error: could not read tasks: {e}")
sys.exit(1)
def save_tasks(tasks: list[dict]) -> None:
try:
with open(DB_FILE, "w", encoding="utf-8") as f:
json.dump(tasks, f, indent=2)
except OSError as e:
print(f"Error: could not write to {DB_FILE}: {e}")
sys.exit(1)
def next_id(tasks: list[dict]) -> int:
max_id = 0
for t in tasks:
tid = t.get("id")
if isinstance(tid, int) and tid > max_id:
max_id = tid
return max_id + 1
def parse_id(s: str) -> int:
try:
i = int(s)
if i <= 0:
raise ValueError
return i
except ValueError:
print("Error: id must be a positive integer.")
sys.exit(1)
def find_task(tasks: list[dict], task_id: int) -> dict | None:
for t in tasks:
if t.get("id") == task_id:
return t
return None
def cmd_add(args: list[str]) -> None:
if len(args) < 1:
print("Error: missing description.")
print_usage()
sys.exit(1)
description = " ".join(args).strip()
if not description:
print("Error: description cannot be empty.")
sys.exit(1)
tasks = load_tasks()
tid = next_id(tasks)
ts = now_iso()
task = {
"id": tid,
"description": description,
"status": "todo",
"createdAt": ts,
"updatedAt": ts,
}
tasks.append(task)
save_tasks(tasks)
print(f"Task added successfully (ID: {tid})")
def cmd_update(args: list[str]) -> None:
if len(args) < 2:
print("Error: update requires <id> <new_description>.")
print_usage()
sys.exit(1)
task_id = parse_id(args[0])
new_description = " ".join(args[1:]).strip()
if not new_description:
print("Error: new description cannot be empty.")
sys.exit(1)
tasks = load_tasks()
task = find_task(tasks, task_id)
if not task:
print(f"Error: task with ID {task_id} not found.")
sys.exit(1)
task["description"] = new_description
task["updatedAt"] = now_iso()
save_tasks(tasks)
print(f"Task {task_id} updated successfully.")
def cmd_delete(args: list[str]) -> None:
if len(args) != 1:
print("Error: delete requires <id>.")
print_usage()
sys.exit(1)
task_id = parse_id(args[0])
tasks = load_tasks()
before = len(tasks)
tasks = [t for t in tasks if t.get("id") != task_id]
after = len(tasks)
if after == before:
print(f"Error: task with ID {task_id} not found.")
sys.exit(1)
save_tasks(tasks)
print(f"Task {task_id} deleted successfully.")
def set_status(task: dict, status: str) -> None:
task["status"] = status
task["updatedAt"] = now_iso()
def cmd_mark_in_progress(args: list[str]) -> None:
if len(args) != 1:
print("Error: mark-in-progress requires <id>.")
print_usage()
sys.exit(1)
task_id = parse_id(args[0])
tasks = load_tasks()
task = find_task(tasks, task_id)
if not task:
print(f"Error: task with ID {task_id} not found.")
sys.exit(1)
set_status(task, "in-progress")
save_tasks(tasks)
print(f"Task {task_id} marked as in-progress.")
def cmd_mark_done(args: list[str]) -> None:
if len(args) != 1:
print("Error: mark-done requires <id>.")
print_usage()
sys.exit(1)
task_id = parse_id(args[0])
tasks = load_tasks()
task = find_task(tasks, task_id)
if not task:
print(f"Error: task with ID {task_id} not found.")
sys.exit(1)
set_status(task, "done")
save_tasks(tasks)
print(f"Task {task_id} marked as done.")
def format_task(t: dict) -> str:
# Defensive gets
tid = t.get("id", "?")
status = t.get("status", "?")
desc = t.get("description", "")
created = t.get("createdAt", "")
updated = t.get("updatedAt", "")
return f"[{tid}] ({status}) {desc}\n created: {created}\n updated: {updated}"
def cmd_list(args: list[str]) -> None:
tasks = load_tasks()
status_filter = None
if len(args) == 1:
status_filter = args[0].strip().lower()
if status_filter not in VALID_STATUSES:
print("Error: list status must be one of: todo, in-progress, done")
sys.exit(1)
elif len(args) > 1:
print("Error: list takes at most one optional status.")
print_usage()
sys.exit(1)
if status_filter:
tasks = [t for t in tasks if t.get("status") == status_filter]
if not tasks:
print("No tasks found.")
return
# Sort by id for stable display
tasks_sorted = sorted(tasks, key=lambda x: x.get("id", 10**9))
for t in tasks_sorted:
print(format_task(t))
def main() -> None:
if len(sys.argv) < 2:
print_usage()
sys.exit(1)
command = sys.argv[1].strip().lower()
args = sys.argv[2:]
commands = {
"add": cmd_add,
"update": cmd_update,
"delete": cmd_delete,
"mark-in-progress": cmd_mark_in_progress,
"mark-done": cmd_mark_done,
"list": cmd_list,
}
if command not in commands:
print(f"Error: unknown command '{command}'.")
print_usage()
sys.exit(1)
commands[command](args)
if __name__ == "__main__":
main()