-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
118 lines (90 loc) · 2.95 KB
/
Copy pathcli.py
File metadata and controls
118 lines (90 loc) · 2.95 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
from argparse import ArgumentParser, RawTextHelpFormatter
from table import make_table
from task_manager import (
add_task,
delete_task,
list_tasks,
set_status,
update_task,
)
from file_utils import dump_json
# 1. Create parser object
parser = ArgumentParser(description="CLI to track and manage your tasks")
# 2. Add Subparser Container
subparsers = parser.add_subparsers(
dest="subcommand", required=True, help="Sub-command help"
)
# 3. Define 'add' subcommand
parsers_add = subparsers.add_parser("add", help="Adds task with description given")
# argument specific to 'add' subcommand
parsers_add.add_argument("description", type=str, help="Short description of task")
# 4. Define 'update' subcommand
parser_update = subparsers.add_parser(
"update", help="Update task with ID and/or description"
)
# arguments specific to 'update' subcommand
parser_update.add_argument("id", type=str, help="Task ID")
parser_update.add_argument("description", type=str, help="Description update")
# 5. Define 'delete' subcommand
parser_delete = subparsers.add_parser("delete", help="Deletes task with known id")
parser_delete.add_argument("id", type=str, help="Task ID")
# 6. Define 'mark' subcommand
parser_mark = subparsers.add_parser(
"mark",
formatter_class=RawTextHelpFormatter,
help="Mark task as 'done' or 'in-progress' Usage: -d, --done or -i, --inprog",
# argument_default="todo"
)
# Add mutually exclusive group for marking progress
mark_group = parser_mark.add_mutually_exclusive_group()
mark_group.add_argument(
"-d",
"--done",
action="store_const",
dest="status",
const="done",
help="Marks task as done",
)
mark_group.add_argument(
"-i",
"--inprog",
action="store_const",
dest="status",
const="in-progress",
help="Marks task as in-progress",
)
# mark ID
parser_mark.add_argument("id", type=str, help="Task ID")
# 7. Define 'list' subcommands
parser_list = subparsers.add_parser(
"list", help="Lists tasks with optional return task progress filters"
)
parser_list.add_argument(
"status",
nargs="?",
default="ALL",
choices=["ALL", "todo", "done", "in-progress"],
type=str,
help="Task status (optional: todo, done or in-progress). Default shows all.",
)
# defining main
def main():
# parse arguments
args = parser.parse_args()
# Use commands
if args.subcommand == "add":
data_json = add_task(args.description)
dump_json("tasks.json", data_json)
elif args.subcommand == "update":
data_json = update_task(args.id, args.description)
dump_json("tasks.json", data_json)
elif args.subcommand == "delete":
data_json = delete_task(args.id)
dump_json("tasks.json", data_json)
elif args.subcommand == "mark":
data_json = set_status(args.id, args.status)
dump_json("tasks.json", data_json)
elif args.subcommand == "list":
make_table(args.status)
if __name__ == "__main__":
main()