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.
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.pyCommand:
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=2This 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)
EOSCommand:
python3 s.py --mode bRecorded output:
bExit 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()
EOSCommand:
python3 s.pyRecorded 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: nameExit 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
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.
All full guides: All guides