Skip to main content

heddle_cli_args/cli/cli_args/
command_suggestions.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Near-miss table for Git-shaped verbs that are not Heddle commands.
3//!
4//! Clap already suggests Levenshtein-near names (`statuz` → `status`).
5//! Users who follow the old text vocabulary (`save`) or Git muscle memory
6//! (`add`, `stash`) land too far from the real verb for that heuristic.
7
8/// Suggested replacement for an unrecognized top-level subcommand.
9pub fn suggested_command(unknown: &str) -> Option<&'static str> {
10    match unknown.trim().to_ascii_lowercase().as_str() {
11        "save" | "add" => Some("capture"),
12        "stash" => Some("start"),
13        _ => None,
14    }
15}
16
17/// Clap-shaped usage error that names the near-miss.
18pub fn format_unrecognized_suggestion(unknown: &str, suggested: &str) -> String {
19    format!(
20        "error: unrecognized subcommand '{unknown}'\n\n  tip: a similar subcommand exists: '{suggested}'\n\nUsage: heddle [OPTIONS] <COMMAND>\n\nFor more information, try '--help'.\n"
21    )
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn save_and_add_suggest_capture() {
30        assert_eq!(suggested_command("save"), Some("capture"));
31        assert_eq!(suggested_command("SAVE"), Some("capture"));
32        assert_eq!(suggested_command("add"), Some("capture"));
33    }
34
35    #[test]
36    fn stash_suggests_start() {
37        assert_eq!(suggested_command("stash"), Some("start"));
38    }
39
40    #[test]
41    fn unknown_verbs_have_no_table_hit() {
42        assert_eq!(suggested_command("statuz"), None);
43        assert_eq!(suggested_command("capture"), None);
44    }
45
46    #[test]
47    fn suggestion_text_names_both_verbs() {
48        let text = format_unrecognized_suggestion("save", "capture");
49        assert!(text.contains("unrecognized subcommand 'save'"));
50        assert!(text.contains("capture"));
51    }
52}