What you'll build
A command-line tool (CLI) in Python for managing tasks. You'll be able to type commands like ./tasks.py add "Buy milk" or ./tasks.py list directly in your terminal. Tasks will be saved in a JSON file. It's the first step to automating any repetitive task with your own scripts.
Step 1: Verify Python
python3 --version
# or
python --version
If you don't have it:
- macOS:
brew install python - Windows: python.org
Step 2: Ask an AI for a CLI
I need a CLI in Python that:
- Uses argparse for commands
- Has commands: add, list, done, delete
- Saves tasks in a JSON file
- Shows colors in output
Give me the complete code.
Basic CLI example
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
TASKS_FILE = Path.home() / ".tasks.json"
def load_tasks():
if TASKS_FILE.exists():
return json.loads(TASKS_FILE.read_text())
return []
def save_tasks(tasks):
TASKS_FILE.write_text(json.dumps(tasks, indent=2))
def add_task(text):
tasks = load_tasks()
tasks.append({"text": text, "done": False})
save_tasks(tasks)
print(f"โ Task added: {text}")
def list_tasks():
tasks = load_tasks()
for i, task in enumerate(tasks, 1):
status = "โ" if task["done"] else "โ"
print(f"{i}. [{status}] {task['text']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Task manager")
parser.add_argument("command", choices=["add", "list"])
parser.add_argument("text", nargs="?")
args = parser.parse_args()
if args.command == "add":
add_task(args.text)
elif args.command == "list":
list_tasks()
Step 3: Make it executable
chmod +x tasks.py
./tasks.py add "Buy milk"
./tasks.py list
Useful libraries
| Library | What for |
|---|---|
argparse | Parse arguments (included) |
click | More elegant CLI |
rich | Nice colors and tables |
requests | Make HTTP requests |
If something failed
| Error | Cause | Solution |
|---|---|---|
python: command not found | Not installed | Install Python |
ModuleNotFoundError | Missing library | pip install library |
Permission denied | Not executable | chmod +x script.py |
Next step
โ Responsive Landing Page โ Modern web design