diff --git a/README.md b/README.md index 91da9a7..856e4fd 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ python task.py list python task.py done 1 ``` +### JSON output + +Every command supports `--json` for scripting and automation: + +```bash +python task.py add "Buy groceries" --json +python task.py list --json +python task.py done 1 --json +``` + ## Testing ```bash diff --git a/commands/add.py b/commands/add.py index 1b1a943..16db6a0 100644 --- a/commands/add.py +++ b/commands/add.py @@ -19,7 +19,7 @@ def validate_description(description): return description.strip() -def add_task(description): +def add_task(description, json_output=False): """Add a new task.""" description = validate_description(description) @@ -31,7 +31,11 @@ def add_task(description): tasks = json.loads(tasks_file.read_text()) task_id = len(tasks) + 1 - tasks.append({"id": task_id, "description": description, "done": False}) + task = {"id": task_id, "description": description, "done": False} + tasks.append(task) tasks_file.write_text(json.dumps(tasks, indent=2)) - print(f"Added task {task_id}: {description}") + if json_output: + print(json.dumps({"status": "added", "task": task})) + else: + print(f"Added task {task_id}: {description}") diff --git a/commands/done.py b/commands/done.py index c9dfd42..c9a9c8c 100644 --- a/commands/done.py +++ b/commands/done.py @@ -17,11 +17,14 @@ def validate_task_id(tasks, task_id): return task_id -def mark_done(task_id): +def mark_done(task_id, json_output=False): """Mark a task as complete.""" tasks_file = get_tasks_file() if not tasks_file.exists(): - print("No tasks found!") + if json_output: + print(json.dumps({"status": "error", "error": "No tasks found!"})) + else: + print("No tasks found!") return tasks = json.loads(tasks_file.read_text()) @@ -31,7 +34,13 @@ def mark_done(task_id): if task["id"] == task_id: task["done"] = True tasks_file.write_text(json.dumps(tasks, indent=2)) - print(f"Marked task {task_id} as done: {task['description']}") + if json_output: + print(json.dumps({"status": "done", "task": task})) + else: + print(f"Marked task {task_id} as done: {task['description']}") return - print(f"Task {task_id} not found") + if json_output: + print(json.dumps({"status": "error", "error": f"Task {task_id} not found"})) + else: + print(f"Task {task_id} not found") diff --git a/commands/list.py b/commands/list.py index 714315d..8407b9c 100644 --- a/commands/list.py +++ b/commands/list.py @@ -18,15 +18,17 @@ def validate_task_file(): return tasks_file -def list_tasks(): +def list_tasks(json_output=False): """List all tasks.""" - # NOTE: No --json flag support yet (feature bounty) tasks_file = validate_task_file() if not tasks_file: - print("No tasks yet!") - return + tasks = [] + else: + tasks = json.loads(tasks_file.read_text()) - tasks = json.loads(tasks_file.read_text()) + if json_output: + print(json.dumps({"tasks": tasks})) + return if not tasks: print("No tasks yet!") diff --git a/task.py b/task.py index 53cc8ed..d115071 100644 --- a/task.py +++ b/task.py @@ -25,22 +25,25 @@ def main(): # Add command add_parser = subparsers.add_parser("add", help="Add a new task") add_parser.add_argument("description", help="Task description") + add_parser.add_argument("--json", action="store_true", help="Output JSON") # List command list_parser = subparsers.add_parser("list", help="List all tasks") + list_parser.add_argument("--json", action="store_true", help="Output JSON") # Done command done_parser = subparsers.add_parser("done", help="Mark task as complete") done_parser.add_argument("task_id", type=int, help="Task ID to mark done") + done_parser.add_argument("--json", action="store_true", help="Output JSON") args = parser.parse_args() if args.command == "add": - add_task(args.description) + add_task(args.description, json_output=args.json) elif args.command == "list": - list_tasks() + list_tasks(json_output=args.json) elif args.command == "done": - mark_done(args.task_id) + mark_done(args.task_id, json_output=args.json) else: parser.print_help() diff --git a/test_task.py b/test_task.py index ba98e43..147fc5c 100644 --- a/test_task.py +++ b/test_task.py @@ -28,3 +28,47 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_add_task_json_output(monkeypatch, tmp_path, capsys): + """Add command can emit parseable JSON.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + + add_task("Write tests", json_output=True) + + output = json.loads(capsys.readouterr().out) + assert output == { + "status": "added", + "task": {"id": 1, "description": "Write tests", "done": False}, + } + + +def test_list_tasks_json_output(monkeypatch, tmp_path, capsys): + """List command can emit parseable JSON.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + add_task("Ship JSON") + capsys.readouterr() + + from commands.list import list_tasks + + list_tasks(json_output=True) + + output = json.loads(capsys.readouterr().out) + assert output == {"tasks": [{"id": 1, "description": "Ship JSON", "done": False}]} + + +def test_done_task_json_output(monkeypatch, tmp_path, capsys): + """Done command can emit parseable JSON.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + add_task("Complete me") + capsys.readouterr() + + from commands.done import mark_done + + mark_done(1, json_output=True) + + output = json.loads(capsys.readouterr().out) + assert output == { + "status": "done", + "task": {"id": 1, "description": "Complete me", "done": True}, + }