Reject Abbreviated Python CLI Options with argparse
A build script invokes an asset tool with --out build. It works while the tool has only --output, then breaks when a later version adds --outline. The caller was relying on an accepted prefix rather than a declared option name. For automation, requiring the full long name makes this contract easier to review and maintain.
These examples use Python 3.10 or later. The tool prints a proposed asset job without opening files or performing a conversion, so its parsing behavior can be tested independently.
Reproduce an Accepted Prefix
Run this short example:
The parser accepts the shortened name because it uniquely identifies --output. After adding an option such as --outline, that same prefix becomes ambiguous. A full --output invocation avoids depending on which other options happen to exist. The setting that controls this behavior is documented in
Python’s allow_abbrev reference
.
Require Declared Long Option Names
Save this as asset_cli.py:
| |
Run python asset_cli.py --output build --format webp hero.png. It prints:
| |
Now python asset_cli.py --output build --form webp hero.png fails instead of treating --form as --format. Keep parse_args() here: this command owns the complete argument list and should reject leftovers. parse_known_args() serves a different interface where another component handles the remaining arguments.
The setting governs long option abbreviation. It does not make a declared short option invalid or validate whether a path exists. Add explicit aliases only when you intend to support them as part of the command’s public interface.
Separate Option Values from Positional Filenames
A filename that starts with a hyphen can look like an option. After specifying the options, place -- before the positional filename:
The separator ends option parsing; it is not included in the filename. Put the options before it. For an option value that itself starts with hyphens, attach the value to the full option name: --output=--staging assigns --staging to output. It is an ordinary relative path string in this demonstration. The parser still does no filesystem access. See the documented
argument syntax rules
.
Test the Contract Without Launching a Shell
Save this as test_asset_cli.py beside the tool and run python test_asset_cli.py:
| |
The tests cover full names, defaults, the separator, an attached value, abbreviations, an unsupported format, extra arguments, and missing arguments. They check the error status and presence of diagnostics without depending on exact help wording, which can differ between Python releases.
Preserve Failures in the Calling Pipeline
Also test the actual entry point. Save this as test_asset_process.py in the same directory and run python test_asset_process.py from that directory:
| |
Parsing finishes before the tool prints its job, so a malformed invocation produces no success output. A real converter should likewise finish argument validation before writing assets. Let the parser’s failure reach the process boundary so a build runner can stop. When introducing this setting to an existing command, first update callers that rely on abbreviated names and run these checks against the Python versions used by the pipeline.