Require complete long options with argparse
By default, argparse accepts an unambiguous prefix of a long option. A script that accepts --out today can reject it after you add another option with the same prefix. Require the full spelling when you want automation to use stable option names.
Set up
Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as complete_options.py:
import argparse
from contextlib import redirect_stderr
from io import StringIO
def parser(allow_abbrev):
result = argparse.ArgumentParser(prog="export-report", allow_abbrev=allow_abbrev)
result.add_argument("--output", required=True)
return result
permissive = parser(True)
print("Default prefix:", permissive.parse_args(["--out", "report.csv"]).output)
strict = parser(False)
print("Full option:", strict.parse_args(["--output", "report.csv"]).output)
diagnostic = StringIO()
try:
with redirect_stderr(diagnostic):
strict.parse_args(["--output", "report.csv", "--out", "other.csv"])
except SystemExit as error:
assert error.code == 2
assert "unrecognized arguments: --out other.csv" in diagnostic.getvalue()
print("Abbreviation exit:", error.code)
else:
raise AssertionError("abbreviated option was accepted")
strict.add_argument("--outbox", default="pending")
args = strict.parse_args(["--output=report.csv"])
assert args.output == "report.csv" and args.outbox == "pending"
print("After adding outbox:", args.output, args.outbox)Run:
python3 -B complete_options.pyObserved output:
Default prefix: report.csv
Full option: report.csv
Abbreviation exit: 2
After adding outbox: report.csv pendingKeep the public command spelling stable
The strict parser is created with allow_abbrev=False. The final call still accepts the documented option after --outbox is added. The equals form is also valid.
The rejected call supplies a valid required option before the abbreviation, which lets the test check the unknown argument directly. The test captures the usual usage/error text from standard error and catches SystemExit. A real command-line program normally lets that exit reach the shell.
Know what the setting covers
This setting controls long-option abbreviation. It does not reject repeated options, validate output paths, or change how explicitly declared short options work.
When migrating an existing command, check scripts that already use prefixes and update their invocations. The argparse reference describes the default and disabled behavior.
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- complete_options.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.