Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: suggest nested subcommands for unrecognized commands (#556)
Maximilian Roosf88c40560eae
diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex 4c7733d0b..e5596cfb1 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -124,6 +124,25 @@ pub fn build_command() -> Command {})}+/// Parent commands whose subcommands can be suggested for unrecognized top-level commands.+const NESTED_COMMAND_PARENTS: &[&str] = &["step", "hook"];++/// Check if an unrecognized subcommand matches a nested subcommand.+///+/// Returns the full command path if found, e.g., "wt step squash" for "squash".+pub fn suggest_nested_subcommand(cmd: &Command, unknown: &str) -> Option<String> {+ for parent in NESTED_COMMAND_PARENTS {+ if let Some(parent_cmd) = cmd.get_subcommands().find(|c| c.get_name() == *parent)+ && parent_cmd+ .get_subcommands()+ .any(|s| s.get_name() == unknown)+ {+ return Some(format!("wt {parent} {unknown}"));+ }+ }+ None+}+fn apply_help_template_recursive(mut cmd: Command, path: &str) -> Command {cmd = cmd.help_template(HELP_TEMPLATE).display_name(path);diff --git a/src/main.rs b/src/main.rsindex 526f8fded..275728c91 100644--- a/src/main.rs+++ b/src/main.rs@@ -685,10 +685,25 @@ fn expand_demo_placeholders(text: &str) -> String {/// Enhance clap errors with command-specific hints, then exit.////// For `wt switch` missing the branch argument, adds hints about shortcuts.+/// For unrecognized subcommands that match nested commands, suggests the full path.fn enhance_and_exit_error(err: clap::Error) -> ! {use clap::error::ErrorKind;use color_print::ceprintln;+ // For unrecognized subcommands, check if they match a nested subcommand+ // e.g., `wt squash` -> suggest `wt step squash`+ if err.kind() == ErrorKind::InvalidSubcommand+ && let Some(unknown) = err.get(clap::error::ContextKind::InvalidSubcommand)+ {+ let cmd = cli::build_command();+ if let Some(suggestion) = cli::suggest_nested_subcommand(&cmd, unknown.to_string().as_str())+ {+ eprint!("{}", err.render().ansi());+ ceprintln!("\n <yellow>tip:</> did you mean <cyan,bold>{suggestion}</cyan,bold>?");+ process::exit(2);+ }+ }+// Enhance `wt switch` missing argument error with shortcut hints.// Hints go to stderr, which is safe since stdout is reserved for data output.// Check for both "wt switch" and "wt.exe switch" (Windows)diff --git a/tests/integration_tests/help.rs b/tests/integration_tests/help.rsindex ca60e34f2..241dda0b5 100644--- a/tests/integration_tests/help.rs+++ b/tests/integration_tests/help.rs@@ -179,3 +179,46 @@ fn test_help_list_narrow_terminal() {assert_cmd_snapshot!("help_list_narrow_80", cmd);});}++/// Tests that using a nested subcommand at the top level suggests the correct command.+///+/// When users type `wt squash` instead of `wt step squash`, or `wt pre-merge` instead+/// of `wt hook pre-merge`, they should get a helpful suggestion.+#[rstest]+#[case("nested_subcommand_step_squash", "squash", "wt step squash")]+#[case("nested_subcommand_step_commit", "commit", "wt step commit")]+#[case("nested_subcommand_hook_pre_merge", "pre-merge", "wt hook pre-merge")]+#[case(+ "nested_subcommand_hook_post_create",+ "post-create",+ "wt hook post-create"+)]+fn test_nested_subcommand_suggestion(+ #[case] test_name: &str,+ #[case] subcommand: &str,+ #[case] expected_suggestion: &str,+) {+ let mut settings = Settings::clone_current();+ settings.set_snapshot_path("../snapshots");+ // Remove trailing ANSI reset codes for cross-platform consistency+ settings.add_filter(r"\x1b\[0m$", "");+ settings.add_filter(r"\x1b\[0m\n", "\n");+ settings.bind(|| {+ let mut cmd = wt_command();+ cmd.arg(subcommand);+ let output = cmd.output().expect("failed to run wt");++ // Should fail (exit code 2)+ assert_eq!(output.status.code(), Some(2));++ // Should contain the suggestion+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains(expected_suggestion),+ "Expected stderr to contain '{expected_suggestion}', got:\n{stderr}"+ );++ // Snapshot the full error output+ assert_cmd_snapshot!(test_name, cmd);+ });+}diff --git a/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_post_create.snap b/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_post_create.snapnew file mode 100644index 000000000..284dfacde--- /dev/null+++ b/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_post_create.snap@@ -0,0 +1,28 @@+---+source: tests/integration_tests/help.rs+info:+ program: wt+ args:+ - post-create+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_EDITOR: ""+ RUST_LOG: warn+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+---+success: false+exit_code: 2+----- stdout -----++----- stderr -----+[1m[31merror:[0m unrecognized subcommand '[1m[33mpost-create[0m'++[1m[32mUsage:[0m [1m[36mwt[0m [36m[OPTIONS][0m [36m[COMMAND]++For more information, try '[1m[36m--help[0m'.++ [33mtip:[39m did you mean [36m[1mwt hook post-create[39m[22m?diff --git a/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_pre_merge.snap b/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_pre_merge.snapnew file mode 100644index 000000000..d5e40530b--- /dev/null+++ b/tests/snapshots/integration__integration_tests__help__nested_subcommand_hook_pre_merge.snap@@ -0,0 +1,30 @@+---+source: tests/integration_tests/help.rs+info:+ program: wt+ args:+ - pre-merge+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_EDITOR: ""+ RUST_LOG: warn+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+---+success: false+exit_code: 2+----- stdout -----++----- stderr -----+[1m[31merror:[0m unrecognized subcommand '[1m[33mpre-merge[0m'++ [1m[32mtip:[0m a similar subcommand exists: '[1m[32mremove[0m'++[1m[32mUsage:[0m [1m[36mwt[0m [36m[OPTIONS][0m [36m[COMMAND]++For more information, try '[1m[36m--help[0m'.++ [33mtip:[39m did you mean [36m[1mwt hook pre-merge[39m[22m?diff --git a/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_commit.snap b/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_commit.snapnew file mode 100644index 000000000..b5bf93982--- /dev/null+++ b/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_commit.snap@@ -0,0 +1,28 @@+---+source: tests/integration_tests/help.rs+info:+ program: wt+ args:+ - commit+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_EDITOR: ""+ RUST_LOG: warn+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+---+success: false+exit_code: 2+----- stdout -----++----- stderr -----+[1m[31merror:[0m unrecognized subcommand '[1m[33mcommit[0m'++[1m[32mUsage:[0m [1m[36mwt[0m [36m[OPTIONS][0m [36m[COMMAND]++For more information, try '[1m[36m--help[0m'.++ [33mtip:[39m did you mean [36m[1mwt step commit[39m[22m?diff --git a/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_squash.snap b/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_squash.snapnew file mode 100644index 000000000..bb9077073--- /dev/null+++ b/tests/snapshots/integration__integration_tests__help__nested_subcommand_step_squash.snap@@ -0,0 +1,28 @@+---+source: tests/integration_tests/help.rs+info:+ program: wt+ args:+ - squash+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_EDITOR: ""+ RUST_LOG: warn+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+---+success: false+exit_code: 2+----- stdout -----++----- stderr -----+[1m[31merror:[0m unrecognized subcommand '[1m[33msquash[0m'++[1m[32mUsage:[0m [1m[36mwt[0m [36m[OPTIONS][0m [36m[COMMAND]++For more information, try '[1m[36m--help[0m'.++ [33mtip:[39m did you mean [36m[1mwt step squash[39m[22m?