How to Stop a Python Command-Line Tool with a Usage Error Using argparse error()
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
parser.error(<message>)
| Part | What it means |
|---|---|
parser | The 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
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)))

range() stops one before its end, so args.end + 1 includes the end itself.
Step 2: Run It with Good Values
uv run python count.py --start 3 --end 6
[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:
- macOS / Linux (bash or zsh)
- Windows (PowerShell)
uv run python count.py --start 8 --end 2
echo $?
usage: count [-h] [--start START] [--end END]
count: error: --end must not be smaller than --start
2
uv run python count.py --start 8 --end 2
$LASTEXITCODE
usage: count [-h] [--start START] [--end END]
count: error: --end must not be smaller than --start
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
| Mistake | What happens | Do this instead |
|---|---|---|
print() the message and carry on | The script runs on with values that make no sense | Call parser.error(), which stops the script |
sys.exit("message") instead | The script printed only the message and stopped with status 1, without the usage line | Use parser.error() for problems with the command line |
Calling it before parse_args() | There are no values to check yet | Check 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
- ArgumentParser.error() — what the method prints and its exit status.
- ArgumentParser.exit() — ends the program with a status you choose.
- sys.exit() — ending a script for reasons that have nothing to do with the command line.
