C0038 cli-no-abbreviations¶
Rule: A CLI runs no subcommand from a unique prefix.
| Code | C0038 (stable, never reused) |
| Id | cli-no-abbreviations |
| Category | cli |
| Source | spirit_level |
| Fix | by hand |
| Needs | cargo installed |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | warn |
a caller's shorthand for one subcommand can silently point at a different one once a sibling is added |
| small-workspace | warn |
a caller's shorthand for one subcommand can silently point at a different one once a sibling is added |
| medium | deny |
clap defaults to exact subcommand names for free; turning inference on is a choice, not an accident |
| big | deny |
clap defaults to exact subcommand names for free; turning inference on is a choice, not an accident |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
exempt |
list of strings | [] |
subcommand paths allowed to run from a unique prefix, words separated by spaces (* add; * one word, ** any) |
Why¶
Command::infer_subcommands(true) lets a caller type any unambiguous prefix of a subcommand and runs it. It feels convenient until a sibling command is added: a script or a habit that typed tool rem for remove now resolves to the new rename, or to nothing, with no warning that its meaning moved. A subcommand's name is a promise; an unstated prefix is not.
Example¶
$ tool rem --help # today: unambiguous, runs `remove`
Remove a thing
...
$ tool add rename # tomorrow: `tool rem` is now ambiguous between `remove` and `rename`
How to fix¶
Don't call Command::infer_subcommands(true) (clap's builder API) or set #[command(infer_subcommands = true)] on a derive struct. Callers type the full subcommand name, or a stated alias (visible_alias), which stays put whatever else is added. The lint runs each subcommand's shortest unique prefix and flags it when that prefix's -h prints the subcommand's own help.
Configure¶
[lints.cli-no-abbreviations]
level = "deny"
exempt = []
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(cli-no-abbreviations) <reason> (waivers); the code works there too, as in [lints.C0038].