|
| 1 | + |
| 2 | +# Module Lab: Building a Python Command-Line Interface Tool |
| 3 | + |
| 4 | +## Learning Goals |
| 5 | + |
| 6 | +- Build modular and user-friendly command-line applications using `argparse`. |
| 7 | +- Apply object-oriented programming (OOP) to map real-world objects to CLI commands. |
| 8 | +- Validate user input and provide helpful feedback. |
| 9 | +- Structure CLI tools for maintainability and scalability. |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +In this lab, you'll design and implement a Python Command-Line Interface (CLI) tool that models real-world behavior using OOP. You'll use Python's built-in `argparse` module to define commands, and object-oriented classes to manage task-related actions. |
| 14 | + |
| 15 | +The CLI tool will allow users to: |
| 16 | + |
| 17 | +- Add tasks to a user account via `add-task` |
| 18 | +- Mark tasks as complete via `complete-task` |
| 19 | +- Display feedback directly in the terminal |
| 20 | + |
| 21 | +This lab combines CLI architecture with OOP principles to help you build intuitive and testable developer tools. |
| 22 | + |
| 23 | +## Setup Instructions |
| 24 | + |
| 25 | +### Fork and Clone the Repository |
| 26 | + |
| 27 | +1. Go to the provided GitHub repository link. |
| 28 | +2. Fork the repository to your GitHub account. |
| 29 | +3. Clone the forked repository to your local machine using: |
| 30 | + |
| 31 | +```bash |
| 32 | +git clone <repo-url> |
| 33 | +cd module-lab-python-cli-tool |
| 34 | +``` |
| 35 | + |
| 36 | +### Install Python and Dependencies |
| 37 | + |
| 38 | +Ensure Python is installed: |
| 39 | + |
| 40 | +```bash |
| 41 | +python --version |
| 42 | +``` |
| 43 | + |
| 44 | +Optionally, create and activate a virtual environment: |
| 45 | + |
| 46 | +```bash |
| 47 | +python -m venv venv |
| 48 | +source venv/bin/activate # macOS/Linux |
| 49 | +venv\Scripts\activate # Windows |
| 50 | +``` |
| 51 | + |
| 52 | +Install dependencies: |
| 53 | + |
| 54 | +```bash |
| 55 | +pip install -r requirements.txt |
| 56 | +``` |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Tasks |
| 61 | + |
| 62 | +### Task 1: Define the Problem |
| 63 | + |
| 64 | +Build a CLI tool that allows users to: |
| 65 | + |
| 66 | +- Add tasks to their name |
| 67 | +- Mark tasks as complete |
| 68 | +- See helpful feedback after actions |
| 69 | + |
| 70 | +The tool should simulate how users interact with a task manager, mapping commands to behavior using classes. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### Task 2: Determine the Design |
| 75 | + |
| 76 | +Your application will be split into: |
| 77 | + |
| 78 | +- A `Task` class to represent individual tasks |
| 79 | +- A `User` class to group tasks under a user's name |
| 80 | +- A CLI controller using `argparse` with `subparsers` to route actions |
| 81 | + |
| 82 | +This design keeps your logic modular, object-oriented, and easy to extend. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### Task 3: Develop the CLI Tool |
| 87 | + |
| 88 | +#### Step 1: Define Your Classes in `lib/models.py` |
| 89 | + |
| 90 | +```python |
| 91 | +class Task: |
| 92 | + def __init__(self, title): |
| 93 | + self.title = title |
| 94 | + self.completed = False |
| 95 | + |
| 96 | + def complete(self): |
| 97 | + self.completed = True |
| 98 | + print(f"✅ Task '{self.title}' completed.") |
| 99 | + |
| 100 | +class User: |
| 101 | + def __init__(self, name): |
| 102 | + self.name = name |
| 103 | + self.tasks = [] |
| 104 | + |
| 105 | + def add_task(self, task): |
| 106 | + self.tasks.append(task) |
| 107 | + print(f"📌 Task '{task.title}' added to {self.name}.") |
| 108 | +``` |
| 109 | + |
| 110 | +#### Step 2: Create the CLI in `lib/cli_tool.py` |
| 111 | + |
| 112 | +```python |
| 113 | +import argparse |
| 114 | +from lib.models import Task, User |
| 115 | + |
| 116 | +users = {} |
| 117 | + |
| 118 | +def add_task(args): |
| 119 | + user = users.get(args.user) or User(args.user) |
| 120 | + users[args.user] = user |
| 121 | + task = Task(args.title) |
| 122 | + user.add_task(task) |
| 123 | + |
| 124 | +def complete_task(args): |
| 125 | + user = users.get(args.user) |
| 126 | + if user: |
| 127 | + for task in user.tasks: |
| 128 | + if task.title == args.title: |
| 129 | + task.complete() |
| 130 | + return |
| 131 | + print("❌ Task not found.") |
| 132 | + else: |
| 133 | + print("❌ User not found.") |
| 134 | + |
| 135 | +def main(): |
| 136 | + parser = argparse.ArgumentParser(description="Task Manager CLI") |
| 137 | + subparsers = parser.add_subparsers() |
| 138 | + |
| 139 | + add_parser = subparsers.add_parser("add-task", help="Add a new task") |
| 140 | + add_parser.add_argument("user") |
| 141 | + add_parser.add_argument("title") |
| 142 | + add_parser.set_defaults(func=add_task) |
| 143 | + |
| 144 | + complete_parser = subparsers.add_parser("complete-task", help="Complete a task") |
| 145 | + complete_parser.add_argument("user") |
| 146 | + complete_parser.add_argument("title") |
| 147 | + complete_parser.set_defaults(func=complete_task) |
| 148 | + |
| 149 | + args = parser.parse_args() |
| 150 | + if hasattr(args, "func"): |
| 151 | + args.func(args) |
| 152 | + else: |
| 153 | + parser.print_help() |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + main() |
| 157 | +``` |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +### Task 4: Run and Test the CLI Tool |
| 162 | + |
| 163 | +```bash |
| 164 | +# Add a task |
| 165 | +python lib/cli_tool.py add-task Alice "Write unit tests" |
| 166 | + |
| 167 | +# Complete a task |
| 168 | +python lib/cli_tool.py complete-task Alice "Write unit tests" |
| 169 | +``` |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## Best Practices |
| 174 | + |
| 175 | +- Use `argparse` to guide the user experience. |
| 176 | +- Validate input with helpful error messages. |
| 177 | +- Keep CLI and OOP logic modular and separated. |
| 178 | +- Document your script and commands clearly in the README. |
| 179 | +- Use the `__main__` guard to make your CLI script reusable. |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## Conclusion |
| 184 | + |
| 185 | +After completing this lab, you will: |
| 186 | + |
| 187 | +✅ Build structured and modular CLI tools in Python |
| 188 | +✅ Map real-world entities using object-oriented design |
| 189 | +✅ Create terminal experiences with helpful input/output |
| 190 | +✅ Apply argparse and OOP to real development workflows |
| 191 | + |
| 192 | +These skills help you build maintainable CLI tools that scale with complexity and support real-world use cases. |
0 commit comments