A StudyLoop command is a Click command or group in its own module under
packages/studyloop/src/studyloop/cli/, mounted by name in
packages/studyloop/src/studyloop/cli/__init__.py and imported only when invoked. Its --help
text is documentation that a test resolves, and its proof is a click.testing.CliRunner test the
coverage gate can see.
- Put the command in
packages/studyloop/src/studyloop/cli/_<name>.py. - Mount it in the
lazy_subcommandstable ofpackages/studyloop/src/studyloop/cli/__init__.py. - Write the
--helptext as if a learner will read only that. - Add a
CliRunnertest beside the nearestpackages/studyloop/tests/test_cli_*.pyfile. - Run the three gates below before you open the pull request.
- One module per command or command group:
packages/studyloop/src/studyloop/cli/_*.py. The leading underscore says "not imported by anyone but the mount table". - The mount table is
lazy_subcommandsinpackages/studyloop/src/studyloop/cli/__init__.py:"chat-note": "studyloop.cli._chat_note:chat_note"maps the name a learner types tomodule:attribute.packages/studyloop/src/studyloop/cli/_lazy.pyimports the module only when that name is resolved, which is whystudyloop --helpstays fast with over forty commands mounted. - Experimental commands go in the
dev_subcommandsset of the same decorator. They are absent from--helpand from command resolution unless the root is invoked withstudyloop --dev. - Shared helpers (the rich
console, topic lookups) come frompackages/studyloop/src/studyloop/cli/_shared.py. Heavy imports belong inside the command function, not at module top, for the same start-up reason.
Read packages/studyloop/src/studyloop/cli/_chat_note.py (47 lines). It is one @click.command
with one argument, three options, a click.ClickException for the refusal path and a JSON output
flag, and it is mounted on a single line of the table. The smallest group is
packages/studyloop/src/studyloop/cli/_mastery.py: a @click.group with two subcommands and a
rich table. Copy the shape, not the body.
Its help text is what the learner sees, so check it by running it:
uv run studyloop --help
uv run studyloop chat-note --helpEvery fenced studyloop … line on this page, on docs/cli-reference.md and in any Examples:
section of a command docstring is resolved against the real Click tree by
packages/studyloop/tests/test_docs_drift.py (F11). A help example that names an option the
command does not declare fails that test, so write --help first and let the test keep it true.
The pattern is the CliRunner template in the
testing cookbook:
invoke the command object directly, assert the exit code and the side effect that did or did
not happen. The maintained example is
packages/studyloop/tests/test_contributor_examples.py::test_cli_refusal_never_calls_verifier.
For a real per-command file, read packages/studyloop/tests/test_cli_doctor.py: it patches the
seam the command calls (_get_registry) and asserts the exit-code contract for each outcome.
uv run --group dev pytest packages/studyloop/tests/test_contributor_examples.py -qName the file packages/studyloop/tests/test_cli_<name>.py and keep the basename unique across
packages/studyloop/tests/ (the coverage collector imports in prepend mode).
-
The default suite runs your test:
just test(pyproject.tomldeselects the opt-in markers, F7). The focused form is the pytest line above. -
The help text is true:
uv run --group dev pytest packages/studyloop/tests/test_docs_drift.py -q -k studyloop_example
-
The command is covered.
packages/studyloop/tests/test_e2e_coverage_gate.pydiscovers every command in the Click tree and fails if no test invokes it; skipped or xfailing tests do not count, and a waiver needs a written reason:uv run --group dev pytest packages/studyloop/tests/test_e2e_coverage_gate.py -q -k cli
just preflight runs all of these with lint, types, JS tests and the strict docs build (F7).
- The name in
lazy_subcommandsis what a learner would type; the module imports nothing heavy at top level, and nothing inpackages/studyloop/src/studyloop/cli/__init__.pyimports your module. --helpsays what the command does in one sentence; every option hashelp=; refusals are aclick.ClickExceptionwith a remedy, never a traceback.- The test asserts the exit code and the side effect (one record written, nothing executed), as the cookbook template does, and it fails without your change.
- A learner-facing command has its row in
docs/cli-reference.md; a developer-only command is indev_subcommands, not hidden behind an environment variable. - The changelog entry under
[Unreleased]names the behaviour, not the file (see the changelog process).