-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo_do_CLI.py
More file actions
134 lines (116 loc) · 3.62 KB
/
To_do_CLI.py
File metadata and controls
134 lines (116 loc) · 3.62 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
import json
import os
from datetime import datetime
TODO_FILE = "todos.json"
def load_todos():
if os.path.exists(TODO_FILE):
with open(TODO_FILE, 'r') as f:
return json.load(f)
return []
def save_todos(todos):
with open(TODO_FILE, 'w') as f:
json.dump(todos, f, indent=2)
def add_todo(task):
todos = load_todos()
todo = {
'id': len(todos) + 1,
'task': task,
'completed': False,
'created': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
todos.append(todo)
save_todos(todos)
print(f"✓ Added: {task}")
def list_todos():
todos = load_todos()
if not todos:
print("No todos found. Add one with 'add <task>'")
return
print("\n📋 Your To-Do List:")
print("-" * 60)
for todo in todos:
status = "✓" if todo['completed'] else "○"
print(f"{status} [{todo['id']}] {todo['task']}")
print("-" * 60)
def complete_todo(todo_id):
todos = load_todos()
for todo in todos:
if todo['id'] == todo_id:
todo['completed'] = True
save_todos(todos)
print(f"✓ Completed: {todo['task']}")
return
print(f"Todo with ID {todo_id} not found")
def delete_todo(todo_id):
todos = load_todos()
for i, todo in enumerate(todos):
if todo['id'] == todo_id:
task = todo['task']
todos.pop(i)
# Reindex remaining todos
for j, t in enumerate(todos):
t['id'] = j + 1
save_todos(todos)
print(f"✗ Deleted: {task}")
return
print(f"Todo with ID {todo_id} not found")
def show_help():
print("""
To-Do List CLI
Commands:
add <task> - Add a new todo
list - List all todos
complete <id> - Mark a todo as complete
delete <id> - Delete a todo
help - Show this help message
exit - Exit the application
Examples:
add Buy groceries
complete 1
delete 2
""")
def main():
print("Welcome to To-Do List CLI! Type 'help' for commands.")
while True:
try:
command = input("\n> ").strip().split(' ', 1)
if not command or command[0] == '':
continue
action = command[0].lower()
if action == 'exit':
print("Goodbye!")
break
elif action == 'help':
show_help()
elif action == 'list':
list_todos()
elif action == 'add':
if len(command) < 2:
print("Usage: add <task>")
else:
add_todo(command[1])
elif action == 'complete':
if len(command) < 2:
print("Usage: complete <id>")
else:
try:
complete_todo(int(command[1]))
except ValueError:
print("Please provide a valid todo ID")
elif action == 'delete':
if len(command) < 2:
print("Usage: delete <id>")
else:
try:
delete_todo(int(command[1]))
except ValueError:
print("Please provide a valid todo ID")
else:
print(f"Unknown command: {action}. Type 'help' for commands.")
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()