Skip to main content

How to Stop a Python Command-Line Tool with a Usage Error Using argparse error()

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

argparse checks each option on its own: its type, its choices, whether it's required. A rule that involves two options, such as "the end can't come before the start", is yours to check. parser.error() reports it the same way argparse reports its own errors. This guide shows it on Python 3.15.2.

Syntax​

Syntax
parser.error(<message>)
PartWhat it means
parserThe ArgumentParser (or a subcommand's parser) whose usage line should be shown
<message>The text that follows error:

Python's argparse documentation says the method "prints a usage message, including the message, to sys.stderr and terminates the program with a status code of 2." The code after it never runs.

The Example​

A count script prints the whole numbers from --start to --end. Each option alone is a valid number, but an end smaller than the start makes no sense, so the script checks the pair after parsing.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

count.py
import argparse

parser = argparse.ArgumentParser(prog="count")
parser.add_argument("--start", type=int, default=1)
parser.add_argument("--end", type=int, default=5)
args = parser.parse_args()

if args.end < args.start:
parser.error("--end must not be smaller than --start")

print(list(range(args.start, args.end + 1)))

The count.py script in a macOS-style code window: --start and --end integer options with defaults 1 and 5, a check that calls parser.error when the end is smaller than the start, then a print of the numbers from start to end.

range() stops one before its end, so args.end + 1 includes the end itself.

Step 2: Run It with Good Values​

Terminal
uv run python count.py --start 3 --end 6
Output
[3, 4, 5, 6]

The terminal after uv run python count.py --start 3 --end 6: the list 3, 4, 5, 6.

The check passed, so the script printed the numbers from 3 to 6.

Step 3: Run It with an End Before the Start​

Run it with the values the wrong way round, then print the exit status the script ended with:

Terminal
uv run python count.py --start 8 --end 2
echo $?
Output
usage: count [-h] [--start START] [--end END]
count: error: --end must not be smaller than --start
2

The terminal after the run with --start 8 --end 2: the usage line for count, then count: error: --end must not be smaller than --start, then the exit status 2.

parser.error() printed the usage line, then count: error: and the message, and ended the script with status 2, the same status argparse uses for its own errors. The list was never printed. echo $? and $LASTEXITCODE show the exit status of the last command. The PowerShell output was captured in PowerShell 7.6.10 on the same Mac.

Common Mistakes​

MistakeWhat happensDo this instead
print() the message and carry onThe script runs on with values that make no senseCall parser.error(), which stops the script
sys.exit("message") insteadThe script printed only the message and stopped with status 1, without the usage lineUse parser.error() for problems with the command line
Calling it before parse_args()There are no values to check yetCheck after args = parser.parse_args()

FAQ​

Does it work with subcommands?​

Yes. Call error() on the subcommand's own parser, the one add_parser() returned, and the usage line names the subcommand, such as usage: tool devices [-h] ....

Where does the message go?​

To standard error (sys.stderr), as the documentation says, so it still appears when the normal output is redirected to a file.

Conclusion​

With --start 3 --end 6, the check passed and the script printed [3, 4, 5, 6]. With --start 8 --end 2, parser.error() printed the usage line and count: error: --end must not be smaller than --start, and the script ended with exit status 2.

Additional Resources​