Building CLI Applications
Build professional command-line applications with Python. Learn argument parsing, interactive prompts, progress bars, and colored output.
15 min•By Priygop Team•Updated 2026
CLI Applications
CLI Applications
import argparse
import sys
# Professional CLI with argparse
def create_cli():
parser = argparse.ArgumentParser(
description="TaskMaster — A Python Task Manager",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
taskmaster add "Buy groceries" --priority high
taskmaster list --status pending
taskmaster done 3
"""
)
subparsers = parser.add_subparsers(dest="command")
# Add command
add_p = subparsers.add_parser("add", help="Add a new task")
add_p.add_argument("title", help="Task title")
add_p.add_argument("--priority", choices=["low", "medium", "high"], default="medium")
# List command
list_p = subparsers.add_parser("list", help="List tasks")
list_p.add_argument("--status", choices=["all", "pending", "done"], default="all")
return parser
# Demo
parser = create_cli()
# Simulated commands
demos = [
["add", "Learn Python", "--priority", "high"],
["add", "Build portfolio", "--priority", "medium"],
["list", "--status", "all"],
]
tasks = []
for demo_args in demos:
args = parser.parse_args(demo_args)
if args.command == "add":
task = {"id": len(tasks) + 1, "title": args.title, "priority": args.priority, "done": False}
tasks.append(task)
print(f"✅ Added: {task['title']} [{task['priority']}]")
elif args.command == "list":
print("\n📋 Tasks:")
for t in tasks:
status = "✅" if t["done"] else "⬜"
pri = {"high": "🔴", "medium": "🟡", "low": "🟢"}[t["priority"]]
print(f" {t['id']}. {status} {pri} {t['title']}")
# Interactive CLI patterns
print("\n=== CLI Best Practices ===")
print("1. Use argparse for complex CLIs")
print("2. Add --help with clear descriptions")
print("3. Use subcommands for multiple actions")
print("4. Support --verbose and --quiet flags")
print("5. Use exit codes (0=success, 1=error)")Tip
Tip
Start every project with a README, .gitignore, and requirements.txt. Use cookiecutter templates for consistent project scaffolding.
Diagram
Loading diagram…
Module = file, Package = folder + __init__.py, Library = pip.
Common Mistake
Warning
No project structure. Dumping everything in one directory. Use src/, tests/, docs/ folders from day one.
Quick Quiz
Practice Task
Note
(1) Create a project with proper structure. (2) Add README, .gitignore, requirements.txt. (3) Set up virtual environment.