# Python argparse: read command-line arguments and get useful errors

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-argparse-full-en
argparse parses positionals and options, validates types and produces usage errors. The shell splits the command line first; a usage error exits 2.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-51-A, TE-51-B and TE-51-C. The expected non-zero exit is TE-51-C (exit status 2). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-51-A: argparse separates positionals from options and exits 2 on a usage error

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
printf 'import argparse\np=argparse.ArgumentParser()\np.add_argument("name")\np.add_argument("--count", type=int, default=1)\na=p.parse_args()\nprint(a.name, a.count)\n' > s.py
```

Command:

```bash
python3 s.py bob --count 3; python3 s.py --count x bob 2>&1 | tail -1; python3 s.py --count x bob >/dev/null 2>&1; echo exit=$?
```

Recorded output:

```
bob 3
s.py: error: argument --count: invalid int value: 'x'
exit=2
```

This run checks the exit status and records the output; it asserts no expected text.

Exit status: 0. This is the status recorded in the log for TE-51-A.

### TE-51-B: an option with choices and a default takes the value that is given

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
import argparse
p = argparse.ArgumentParser()
p.add_argument('--mode', choices=['a', 'b'], default='a')
print(p.parse_args().mode)
EOS
```

Command:

```bash
python3 s.py --mode b
```

Recorded output:

```
b
```

Exit status: 0. This status and the output match what was written before the run.

### TE-51-C: expected non-zero exit: a missing required positional argument ends the program with exit status 2

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
import argparse
p = argparse.ArgumentParser()
p.add_argument('name')
p.parse_args()
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
usage: s.py [-h] name
s.py: error: the following arguments are required: name
```

Exit status: 2. This status and the output match what was written before the run.

TE-51-B: --mode b is one of the choices a and b, and the program printed b. TE-51-C: by the setup, the name argument is required. Running without it exited with status 2, and standard error ends with s.py: error: the following arguments are required: name.

## Limits

The shell splits the command line first; a usage error exits 2.

## Source

- https://docs.python.org/3/library/argparse.html

## Evidence

- `TE-51-A`: argparse separates positionals from options and exits 2 on a usage error

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-51-A` | argparse separates positionals from options and exits 2 on a usage error | 0 | PASS | `f6be54800227` |
| `TE-51-B` | an option with choices and a default takes the value that is given | 0 | PASS | `80cdfed18dd0` |
| `TE-51-C` | expected non-zero exit: a missing required positional argument ends the program with exit status 2 | 2 | PASS | `5087b5d1fa21` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python comprehensions: build a list, dict or set in one line](https://kairos-2z6s.onrender.com/res/python-comprehensions-full-en)
- [Python csv module: read and write rows with quoting rules](https://kairos-2z6s.onrender.com/res/python-csv-full-en)
- [Python classes: a small example, line by line](https://kairos-2z6s.onrender.com/res/python-class-basics-full-en)
- [Python any and all: test a whole list](https://kairos-2z6s.onrender.com/res/python-any-all-full-en)
- [Python f-strings: expressions and format specs](https://kairos-2z6s.onrender.com/res/python-functions-fstrings-full-en)
