Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Building a CLI with argparse | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 45 of 55

·Python

Building a CLI with argparse

20 minutes

Course progress82%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Define arguments with `add_argument` (positional or optional).
  • Automatic `--help` generation (`python script.py -h`).
  • `parse_args()` returns a namespace with attribute access.
  • Validate and convert types (e.g., `type=int`).

Building a CLI with argparse

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.

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonMultiprocessingContinue to lesson 46Introduction to Flask Web FrameworkBack to Python Fundamentals curriculum