-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (76 loc) · 2.13 KB
/
Copy pathmain.py
File metadata and controls
97 lines (76 loc) · 2.13 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
#!/usr/bin/env python3
import os
import click
from utils import TaskList, error, success
VERSION = "1.4"
def get_task_list():
current_dir = os.getcwd()
todo_file = os.path.join(current_dir, ".todo.json")
return TaskList(todo_file)
@click.group()
def cli():
pass
@cli.command()
@click.argument('task')
def add(task):
"""Add a new task"""
tasks = get_task_list()
tasks.add(task)
success(f'"{task}" was added successfully!')
@cli.command()
@click.argument('task_id')
def rm(task_id):
"""Remove a task by ID"""
tasks = get_task_list()
if tasks.rm(task_id):
success("Remove was successful.")
else:
raise SystemExit(1)
@cli.command()
def ls():
"""List all tasks"""
tasks = get_task_list()
if tasks.is_empty():
error("No tasks created yet!")
raise SystemExit(1)
for id, task in tasks:
if task.checked():
success(f"- [X] {task.name} id: {id}")
else:
click.echo(f"- [ ] {task.name} id: {id}")
@cli.command()
@click.argument('task_id')
def check(task_id):
"""Mark a task as completed"""
tasks = get_task_list()
if tasks.check(task_id):
success("Checked task successfully.")
else:
raise SystemExit(1)
@cli.command()
@click.argument('task_id')
def uncheck(task_id):
"""Mark a task as open"""
tasks = get_task_list()
match tasks.uncheck(task_id):
case 0:
success("Unchecked task successfully.")
case 1:
raise SystemExit(1)
@cli.command()
def version():
"""Show version"""
click.echo(f"todo {VERSION}")
@cli.command()
def help():
"""Show this help message"""
click.echo("Usage:")
click.echo(' todo add "task" Add a new task')
click.echo(" todo rm <id> Remove a task by ID")
click.echo(" todo ls List all tasks")
click.echo(" todo check <id> Mark a task as completed")
click.echo(" todo uncheck <id> Mark a task as open")
click.echo(" todo --version, -v Show version")
click.echo(" todo --help Show this help message")
if __name__ == "__main__":
cli()