-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (60 loc) · 1.87 KB
/
app.py
File metadata and controls
72 lines (60 loc) · 1.87 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
import argparse
import json
import os
TASKS_FILE = "tasks.json"
def load_tasks():
if not os.path.exists(TASKS_FILE):
return []
with open(TASKS_FILE, "r") as file:
return json.load(file)
def save_tasks(tasks):
with open(TASKS_FILE, "w") as file:
json.dump(tasks, file, indent=4)
def add_task(title):
tasks = load_tasks()
tasks.append({"title": title, "done": False})
save_tasks(tasks)
print("Task added successfully.")
def list_tasks():
tasks = load_tasks()
if not tasks:
print("No tasks found.")
return
for index, task in enumerate(tasks):
status = "✔" if task["done"] else "✖"
print(f"{index} - [{status}] {task['title']}")
def complete_task(index):
tasks = load_tasks()
try:
tasks[index]["done"] = True
save_tasks(tasks)
print("Task marked as completed.")
except IndexError:
print("Invalid task index.")
def remove_task(index):
tasks = load_tasks()
try:
tasks.pop(index)
save_tasks(tasks)
print("Task removed.")
except IndexError:
print("Invalid task index.")
def main():
parser = argparse.ArgumentParser(description="CLI Task Manager")
parser.add_argument("--add", help="Add a new task")
parser.add_argument("--list", action="store_true", help="List tasks")
parser.add_argument("--done", type=int, help="Mark task as completed")
parser.add_argument("--remove", type=int, help="Remove a task")
args = parser.parse_args()
if args.add:
add_task(args.add)
elif args.list:
list_tasks()
elif args.done is not None:
complete_task(args.done)
elif args.remove is not None:
remove_task(args.remove)
else:
parser.print_help()
if __name__ == "__main__":
main()