argparse
module.
src/todo_cli/main.py
core.py
argparse
subparsers for clean and extendable command dispatchingsrc/todo_cli/main.py
Line / Code | Explanation |
---|---|
import argparse | Standard library to handle command-line arguments |
from . import core | Imports the task logic from core.py |
argparse.ArgumentParser(...) | Initializes the main parser |
subparsers = parser.add_subparsers(...) | Allows for multiple subcommands (add, list, etc.) |
add_parser = subparsers.add_parser(...) | Defines the add command |
add_parser.add_argument(...) | Adds a required task text argument |
args = parser.parse_args() | Parses user input from the command line |
if args.command == "add": | Checks which command is being called and dispatches to the right logic |
print(...) | CLI output/feedback to the user |
Command | Syntax | Description |
---|---|---|
add | todo add "Task" [--priority low/medium/high] [--due YYYY-MM-DD] | Add a new task with priority and due |
list | todo list [--done] [--undone] [--priority LEVEL] [--sort priority] [--verbose] | List tasks with filters, sort or full details |
complete | todo complete <id> | Mark a task as completed |
delete | todo delete <id> [id ...] | Delete one or more tasks by ID |
edit | todo edit <id> [--text TEXT] [--priority LEVEL] [--due YYYY-MM-DD] [--tags tag1,tag2] | Edit a task’s fields (text, priority, due date, or tags) |
clear | todo clear | Delete all tasks |
help
for each subcommandFlag | Description |
---|---|
--priority | Filter by or assign priority |
--due | Add a due date to a task (YYYY-MM-DD) |
--done | Show only completed tasks |
--undone | Show only uncompleted tasks |
--sort | Sort tasks (currently only by priority) |
--verbose | Show detailed task info including created |
--tags | Assign or filter tasks by comma-separated tags |
argparse
subparsers.add_parser
pattern.main()
function acts as the dispatch layer, keeping CLI modular and maintainable.