Lesson 45 of 55
PythonBuilding a CLI with argparse
20 minutes
Course progress82%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 45 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
The argparse module makes it easy to write user‑friendly command‑line interfaces. It automatically generates help messages and parses arguments for you.
import argparse
parser = argparse.ArgumentParser(description='Simple greeter')
parser.add_argument('name', help='Name to greet')
parser.add_argument('-n', '--times', type=int, default=1, help='Number of repetitions')
args = parser.parse_args()
for _ in range(args.times):
print(f'Hello, {args.name}!')
Run the script from a terminal: python greet.py Alice -n 3.