-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_cli.py
More file actions
92 lines (81 loc) · 2.68 KB
/
task_cli.py
File metadata and controls
92 lines (81 loc) · 2.68 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
import sys
import json
import os
from datetime import datetime
DB_FILE = 'tasks_db.json'
def load_tasks():
if not os.path.exists(DB_FILE):
return []
try:
with open(DB_FILE, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
return []
def save_tasks(tasks):
with open(DB_FILE, 'w') as f:
json.dump(tasks, f, indent=4)
def add_task(description):
tasks = load_tasks()
now = datetime.now().isoformat()
new_task = {
'id': max([t['id'] for t in tasks], default=0) + 1,
'description': description,
'status': 'todo',
'createdAt': now,
'updatedAt': now
}
tasks.append(new_task)
save_tasks(tasks)
print(f"Task added successfully (ID: {new_task['id']})")
def update_task(task_id, description):
tasks = load_tasks()
for t in tasks:
if t['id'] == task_id:
t['description'] = description
t['updatedAt'] = datetime.now().isoformat()
save_tasks(tasks)
print(f"Task {task_id} updated.")
return
print("Task not found.")
def change_status(task_id, status):
tasks = load_tasks()
for t in tasks:
if t['id'] == task_id:
t['status'] = status
t['updatedAt'] = datetime.now().isoformat()
save_tasks(tasks)
print(f"Task {task_id} marked as {status}.")
return
print("Task not found.")
def delete_task(task_id):
tasks = load_tasks()
new_tasks = [t for t in tasks if t['id'] != task_id]
if len(new_tasks) < len(tasks):
save_tasks(new_tasks)
print(f"Task {task_id} deleted.")
else:
print("Task not found.")
def list_tasks(status_filter=None):
tasks = load_tasks()
filtered = [t for t in tasks if status_filter is None or t['status'] == status_filter]
if not filtered:
print("No tasks found.")
return
for t in filtered:
print(f"[{t['id']}] {t['description']} | Status: {t['status']}")
def main():
if len(sys.argv) < 2: return
cmd = sys.argv[1]
try:
if cmd == 'add': add_task(" ".join(sys.argv[2:]))
elif cmd == 'update': update_task(int(sys.argv[2]), " ".join(sys.argv[3:]))
elif cmd == 'delete': delete_task(int(sys.argv[2]))
elif cmd == 'mark-in-progress': change_status(int(sys.argv[2]), 'in-progress')
elif cmd == 'mark-done': change_status(int(sys.argv[2]), 'done')
elif cmd == 'list':
status = sys.argv[2] if len(sys.argv) > 2 else None
list_tasks(status)
except (IndexError, ValueError):
print("Usage error. Check your arguments.")
if __name__ == '__main__':
main()