Python (or Go) for Automation
20 min
Day 29: Building a small CLI tool
From script to tool
A script you run with hardcoded values becomes a real tool once it accepts arguments, prints help text, and handles bad input gracefully. Python's argparse gets you there without extra dependencies.
A minimal CLI with argparse
import argparse
parser = argparse.ArgumentParser(description='Check service health')
parser.add_argument('url', help='URL to check')
parser.add_argument('--timeout', type=int, default=5)
args = parser.parse_args()
print(f'Checking {args.url} with {args.timeout}s timeout...')Using it
python healthcheck.py https://example.com --timeout 10
python healthcheck.py --help # argparse generates this for freeKey terms
- argparse
- Python's standard library module for parsing command-line arguments and auto-generating --help text.
What does argparse give you for free that hand-parsing sys.argv does not?