0Pricing
Python Academy · Lesson

argparse Basics

Parse command-line arguments.

argparse Basics is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why argparse?

The argparse module (standard library) builds friendly command-line interfaces. It parses arguments, validates types, and generates help text automatically.

import argparse

parser = argparse.ArgumentParser(description='A demo tool')
print(type(parser).__name__)

Creating a Parser

Start by building an ArgumentParser. The description shows up in the auto-generated help.

import argparse

parser = argparse.ArgumentParser(description='Greet a user')
print('parser ready')

Positional Arguments

add_argument('name') defines a required positional argument. We pass a list to parse_args here so it runs without a real command line.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('name')
args = parser.parse_args(['Alice'])
print('Hello,', args.name)

Optional Arguments

Names starting with -- are optional flags. Provide a default for when the user omits them.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--greeting', default='Hi')
args = parser.parse_args(['--greeting', 'Hey'])
print(args.greeting)
print(parser.parse_args([]).greeting)

Typed Arguments

Use type=int (or float, etc.) so argparse converts and validates the value. Bad input produces a clear error.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--count', type=int, default=1)
args = parser.parse_args(['--count', '5'])
print(args.count * 2)

Boolean Flags

action='store_true' makes a flag that is False by default and True when present, perfect for switches like --verbose.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true')
print(parser.parse_args(['--verbose']).verbose)
print(parser.parse_args([]).verbose)

Short and Long Names

Give an argument both a short and long form, like -n and --name. Both set the same attribute.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name', default='world')
print(parser.parse_args(['-n', 'Sam']).name)

Choices

Restrict allowed values with choices. argparse rejects anything outside the list.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--mode', choices=['fast', 'slow'], default='fast')
print(parser.parse_args(['--mode', 'slow']).mode)

Multiple Values with nargs

nargs='+' collects one or more values into a list, great for accepting several inputs at once.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+')
args = parser.parse_args(['a.txt', 'b.txt', 'c.txt'])
print(args.files)

Required Optionals and Help

Add required=True to force an optional flag, and help= to document it in the generated usage screen.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--id', required=True, type=int, help='record id')
args = parser.parse_args(['--id', '7'])
print('id is', args.id)

A Complete Mini Tool

Putting it together: positional input, a typed option, and a flag form a small but real CLI.

import argparse

parser = argparse.ArgumentParser(description='Repeat a message')
parser.add_argument('message')
parser.add_argument('--times', type=int, default=1)
parser.add_argument('--shout', action='store_true')
args = parser.parse_args(['hello', '--times', '2', '--shout'])
text = args.message.upper() if args.shout else args.message
for _ in range(args.times):
    print(text)

Quick Check

Test your understanding of argparse.

Recap

You learned argparse basics:

  • ArgumentParser with positional and --optional arguments.
  • type, default, choices, and required validate input.
  • action='store_true' for flags; nargs for lists.
  • Help text is generated for free.

Next: subcommands and richer option handling.

Frequently asked questions

Is the “argparse Basics” lesson free?

Yes — the full text of “argparse Basics” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “argparse Basics”?

Parse command-line arguments. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “argparse Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. argparse Basics
  2. Subcommands and Options
  3. The Click Library
  4. Rich Terminal Output
← Back to Python Academy