Code of the Day
BeginnerUtility Thinking

CLI arguments

argparse turns a Python script into a proper command-line tool with named flags, positional arguments, and --help built in.

UtilitiesBeginner10 min read
Recommended first
By the end of this lesson you will be able to:
  • Use argparse to define positional and optional arguments
  • Get --help generated automatically for free
  • Parse a --verbose flag and a positional filename argument
  • Access parsed values from the Namespace object

Reading from works well when your tool is in a pipeline. But sometimes a user wants to name a file directly, or pass options to change behavior. That is what command-line arguments are for — and argparse is the standard Python library for handling them.

Why not just use sys.argv?

sys.argv gives you the raw list of strings the user typed. You could parse it manually, but you would have to handle errors, write your own --help, and invent a syntax for flags yourself. argparse does all of that for you.

A minimal argparse setup

import argparse

parser = argparse.ArgumentParser(description="Count words in a file.")
parser.add_argument("filename", help="The file to read")
parser.add_argument("--verbose", action="store_true", help="Print extra detail")
args = parser.parse_args()

# args.filename is the string the user passed
# args.verbose is True or False
print("File:", args.filename)
print("Verbose:", args.verbose)

Run this as python counter.py report.txt --verbose and args.filename is "report.txt", args.verbose is True. Run it with no arguments and argparse prints an error message with usage. Run it with --help and argparse prints the full help text — generated automatically from the help= strings you provided.

The --help output looks like this:

usage: counter.py [-h] [--verbose] filename

Count words in a file.

positional arguments:
  filename    The file to read

options:
  -h, --help  show this help message and exit
  --verbose   Print extra detail

You wrote none of that manually. It came from the description=, help=, and argument definitions.

Try it

The browser runner does not support sys.argv, so this demo calls parse_args() with an explicit list — the same values that would come from the command line in a real terminal:

Python — editable, runs in your browser

action="store_true" is the pattern for boolean flags: the argument takes no value; its presence sets the attribute to True, its absence leaves it False. For arguments that take a value, just omit action= and argparse stores the string. Use type=int if you want argparse to convert it to an integer for you.

Common patterns

GoalCode
Positional argumentparser.add_argument("filename")
Optional flag (boolean)parser.add_argument("--verbose", action="store_true")
Optional argument with valueparser.add_argument("--count", type=int, default=10)
Short aliasparser.add_argument("-v", "--verbose", action="store_true")

Where to go next

Next: exit codes and errors — what exit codes are, when to use them, and why a program that exits 0 on failure is dangerous in a pipeline.

Finished reading? Mark it complete to track your progress.

On this page