-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
137 lines (106 loc) · 3.67 KB
/
task_manager.py
File metadata and controls
137 lines (106 loc) · 3.67 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
#!/usr/bin/env python3
"""
Task Manager - A simple CLI task management application.
Supports adding, listing, completing, and deleting tasks with persistence via JSON.
"""
import json
import os
import sys
from datetime import datetime
DATA_FILE = os.path.join(os.path.dirname(__file__), "tasks.json")
def load_tasks() -> list[dict]:
if not os.path.exists(DATA_FILE):
return []
with open(DATA_FILE, "r") as f:
return json.load(f)
def save_tasks(tasks: list[dict]) -> None:
with open(DATA_FILE, "w") as f:
json.dump(tasks, f, indent=2)
def add_task(title: str) -> None:
tasks = load_tasks()
task = {
"id": (max((t["id"] for t in tasks), default=0) + 1),
"title": title,
"done": False,
"created_at": datetime.now().isoformat(),
}
tasks.append(task)
save_tasks(tasks)
print(f"[+] Task {task['id']} added: {title}")
def list_tasks(show_all: bool = False) -> None:
tasks = load_tasks()
if not tasks:
print("No tasks found. Add one with: python task_manager.py add <title>")
return
filtered = tasks if show_all else [t for t in tasks if not t["done"]]
if not filtered:
print("All tasks are done! Great job.")
return
print(f"\n{'ID':<5} {'Status':<10} {'Title'}")
print("-" * 50)
for t in filtered:
status = "✓ done" if t["done"] else "○ todo"
print(f"{t['id']:<5} {status:<10} {t['title']}")
print()
def complete_task(task_id: int) -> None:
tasks = load_tasks()
for task in tasks:
if task["id"] == task_id:
if task["done"]:
print(f"Task {task_id} is already marked as done.")
else:
task["done"] = True
save_tasks(tasks)
print(f"[✓] Task {task_id} marked as complete: {task['title']}")
return
print(f"Task {task_id} not found.")
def delete_task(task_id: int) -> None:
tasks = load_tasks()
remaining = [t for t in tasks if t["id"] != task_id]
if len(remaining) == len(tasks):
print(f"Task {task_id} not found.")
return
save_tasks(remaining)
print(f"[-] Task {task_id} deleted.")
def print_usage() -> None:
usage = """
Task Manager - Simple CLI Todo App
Usage:
python task_manager.py add <title> Add a new task
python task_manager.py list List pending tasks
python task_manager.py list --all List all tasks (including done)
python task_manager.py done <id> Mark a task as complete
python task_manager.py delete <id> Delete a task
python task_manager.py help Show this help message
"""
print(usage)
def main() -> None:
args = sys.argv[1:]
if not args or args[0] == "help":
print_usage()
return
command = args[0]
if command == "add":
if len(args) < 2:
print("Error: provide a task title. Example: python task_manager.py add 'Buy milk'")
sys.exit(1)
add_task(" ".join(args[1:]))
elif command == "list":
show_all = "--all" in args
list_tasks(show_all)
elif command == "done":
if len(args) < 2 or not args[1].isdigit():
print("Error: provide a valid task ID. Example: python task_manager.py done 1")
sys.exit(1)
complete_task(int(args[1]))
elif command == "delete":
if len(args) < 2 or not args[1].isdigit():
print("Error: provide a valid task ID. Example: python task_manager.py delete 1")
sys.exit(1)
delete_task(int(args[1]))
else:
print(f"Unknown command: {command}")
print_usage()
sys.exit(1)
if __name__ == "__main__":
main()