Product case study · December 1, 2026

Ask Claude Code to stop CLI shortcuts breaking later

A shortened Python CLI option can break when another flag is added. Give Claude Code a precise contract and test the parser before and after that change.

Reading time
6 min
Checked
Dec 1, 2026
Paper keys approach a charcoal keyhole, with an incomplete key stopped by an ochre barrier.
An accidental shortcut should not become a script's dependency.
Bottom line

For commands stored in automation, require full long-option names and test that future options cannot change accepted spellings.

A script uses --out for months. Someone adds --output-format, and suddenly the same command is ambiguous. Nobody removed an option: the script had been depending on a shortcut the application never explicitly defined.

Python’s argparse accepts unambiguous prefixes of long options by default. That is convenient at a terminal, but I would turn it off for commands copied into scheduled jobs. A little less typing today is a poor trade for an interface that changes when another option appears.

This lab gives Claude Code a precise repair: require full long-option names, then prove that adding a similarly named option does not weaken that rule. Bring Python 3.10 or newer, Bash on Linux, macOS or WSL, and an installed, signed-in Claude Code. The Python fixture and reference fix were tested locally; the agent prompt is an authored exercise, not a recorded Claude run. Official sources were checked December 1, 2026.

Make the accidental shortcut visible

Create a disposable folder. This example only parses arguments and prints a namespace; it does not create an export or write to the supplied path.

option_lab=$(mktemp -d /tmp/claude-option-names.XXXXXX)
cd "$option_lab"
cat > export_cli.py <<'PY'
import argparse


def make_parser(expanded=False):
    parser = argparse.ArgumentParser(prog="export-demo")
    parser.add_argument("--output", default="report.json")
    if expanded:
        parser.add_argument("--output-format", choices=["json", "csv"],
                            default="json")
    return parser


if __name__ == "__main__":
    print(make_parser().parse_args())
PY
python3 export_cli.py --out draft.json

The original parser accepts --out and reports output='draft.json'. Now simulate the next version of the interface:

python3 - <<'PY'
from export_cli import make_parser
make_parser(expanded=True).parse_args(["--out", "draft.json"])
PY

It exits with an ambiguous-option error. The prefix now matches both --output and --output-format. Python documents this behavior under argument abbreviations, and provides allow_abbrev=False to disable long-option abbreviation.

The repair is an interface decision. If people already use abbreviated forms in production scripts, update those callers to full names as part of a planned migration. This exercise defines a new, strict contract; it does not pretend existing callers will keep working unchanged.

Specify the spellings, then freeze the tests

Save this as SPEC.md:

Change only export_cli.py. Keep test_export_cli.py unchanged.
Both make_parser() and make_parser(expanded=True) must reject
abbreviated long options with SystemExit code 2.
Both must accept --output PATH and --output=PATH.
The default output stays report.json.
The expanded parser must accept --output-format csv and default to json.
Unknown options still fail. --help still exits successfully.
Do not add aliases, dependencies, file writes or custom argv rewriting.
Keep the make_parser(expanded=False) interface.

Save this as test_export_cli.py:

import contextlib
import io
import unittest

from export_cli import make_parser


class OptionNameTests(unittest.TestCase):
    def assert_rejected(self, parser, argv):
        with contextlib.redirect_stderr(io.StringIO()):
            with self.assertRaises(SystemExit) as caught:
                parser.parse_args(argv)
        self.assertEqual(caught.exception.code, 2)

    def test_defaults(self):
        self.assertEqual(make_parser().parse_args([]).output, "report.json")
        args = make_parser(True).parse_args([])
        self.assertEqual(args.output, "report.json")
        self.assertEqual(args.output_format, "json")

    def test_full_output_names(self):
        for expanded in (False, True):
            for argv in (["--output", "draft.json"],
                         ["--output=draft.json"]):
                with self.subTest(expanded=expanded, argv=argv):
                    args = make_parser(expanded).parse_args(argv)
                    self.assertEqual(args.output, "draft.json")

    def test_full_format_name(self):
        args = make_parser(True).parse_args(["--output-format", "csv"])
        self.assertEqual(args.output_format, "csv")

    def test_shortened_output_rejected(self):
        for expanded in (False, True):
            for flag in ("--out", "--outp"):
                with self.subTest(expanded=expanded, flag=flag):
                    self.assert_rejected(make_parser(expanded),
                                         [flag, "draft.json"])

    def test_shortened_format_rejected(self):
        self.assert_rejected(make_parser(True), ["--output-f", "csv"])

    def test_unknown_option_rejected(self):
        for expanded in (False, True):
            with self.subTest(expanded=expanded):
                self.assert_rejected(make_parser(expanded), ["--unknown"])

    def test_help_stays_successful(self):
        for expanded in (False, True):
            with self.subTest(expanded=expanded):
                with contextlib.redirect_stdout(io.StringIO()):
                    with self.assertRaises(SystemExit) as caught:
                        make_parser(expanded).parse_args(["--help"])
                self.assertEqual(caught.exception.code, 0)

Run the baseline:

python3 --version
python3 -m unittest -v test_export_cli

Locally, seven test methods ran with three reported failures: two subcases accepting shortened output names in the original parser, and one accepting the shortened format name in the expanded parser. The other checks passed. The test deliberately covers a prefix that becomes ambiguous and one that remains unambiguous: rejecting only the first would miss the contract.

The checks use unittest subtests and exception assertions, so several argument spellings can share one test method. A failure count is not necessarily a count of failed methods. We inspect the exit code rather than exact diagnostic wording, which can vary between Python versions.

Give Claude the failure and the boundary

Start claude inside the practice folder and paste:

Read SPEC.md, export_cli.py and test_export_cli.py.
Run python3 -m unittest -v test_export_cli and report the actual failures.
Explain why --out works in one parser version but fails in the other.
Change only export_cli.py to satisfy the contract for both versions.
Keep all tests fixed. Do not install packages, commit or delegate.
Run the tests again and show the exact source change.
Report which callers would need updating if this were an existing CLI.

Anthropic’s best-practices guide recommends supplying a concrete verification command and reviewing the output against your specification. Review requested tool access for the lab; the prompt is a scope instruction, not a filesystem sandbox.

Reject a repair that manually searches argv for --out. It may pass that one example while still admitting --outp or --output-f. Also reject a new explicit --out alias: that would make the shortcut stable, but it would violate this exercise’s chosen interface.

Inspect the small fix and the bigger migration

The reference repair changes the parser construction to:

parser = argparse.ArgumentParser(prog="export-demo", allow_abbrev=False)

Keep both add_argument calls and the return statement. That change passed all seven test methods locally. Exit Claude and rerun the checks yourself:

python3 -m unittest -v test_export_cli
python3 export_cli.py --output draft.json
python3 export_cli.py --out draft.json

The full name should succeed. The shortened name should now exit with an error, which is the intended behavior. If both still succeed, check that you edited the parser factory imported by the tests. If Python cannot import export_cli, check the working directory and that your editor did not append .txt to the filename.

In an existing project, search shell scripts, scheduled-job definitions and documentation for abbreviated invocations before shipping this change. Replace them with the full registered names and run each affected command’s own checks. If a short form is a deliberate public interface, register and test that alias explicitly instead of relying on prefix matching; that is a different contract from this lab.

This test proves argument acceptance for two versions of a tiny parser. It does not prove an export succeeds, and it says nothing about other parser instances or subcommands in your application. Apply the policy where those parsers are constructed and add tests for their actual command spellings.

For a separate conversion mistake, see why --dry-run False can mean true. That lab fixes boolean values; this one fixes which option names the interface accepts. For workspace setup, use the Claude Code project-session lesson.

Sources

Checked December 1, 2026:

Put this to work

Why an unambiguous option prefix is not a stable CLI alias.

Try

Ask Claude Code to repair the parser factory while keeping the regression tests fixed.

Prove it worked

Reject shortened options in both parser versions while preserving full names, defaults and help.

Keep in view

  • Reproduce a shortcut that works until a second option is introduced.
  • Specify exact long-option names for an automation-facing CLI.
  • Test current and expanded parsers against the same acceptance contract.
Learn the workflow: Start a scoped Claude Code project session