๐Ÿ

CLI Tool with Python

๐Ÿง‘โ€๐Ÿณ Cookโฑ๏ธ 20 minutes

๐Ÿ“‹ Suggested prerequisites

  • โ€ขBasic terminal

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:


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

LibraryWhat for
argparseParse arguments (included)
clickMore elegant CLI
richNice colors and tables
requestsMake HTTP requests

If something failed

ErrorCauseSolution
python: command not foundNot installedInstall Python
ModuleNotFoundErrorMissing librarypip install library
Permission deniedNot executablechmod +x script.py

Next step

โ†’ Responsive Landing Page โ€” Modern web design