KKAIROS
Practical guideIllustrative3 min read

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

argparse parses positionals and options, validates types and produces usage errors. The shell splits the command line first; a usage error exits 2.

Published:

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.

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):

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:

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):

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:

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):

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

Command:

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.

Ad

Source

Evidence

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.

All full guides: All guides