Skip to content

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.rs
index 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.rs
index 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.rs
index 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.snap
new file mode 100644
index 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 -----
+error: unrecognized subcommand 'post-create'
+
+Usage: wt [OPTIONS] [COMMAND]
+
+For more information, try '--help'.
+
+ tip: did you mean wt hook post-create?
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.snap
new file mode 100644
index 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 -----
+error: unrecognized subcommand 'pre-merge'
+
+ tip: a similar subcommand exists: 'remove'
+
+Usage: wt [OPTIONS] [COMMAND]
+
+For more information, try '--help'.
+
+ tip: did you mean wt hook pre-merge?
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.snap
new file mode 100644
index 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 -----
+error: unrecognized subcommand 'commit'
+
+Usage: wt [OPTIONS] [COMMAND]
+
+For more information, try '--help'.
+
+ tip: did you mean wt step commit?
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.snap
new file mode 100644
index 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 -----
+error: unrecognized subcommand 'squash'
+
+Usage: wt [OPTIONS] [COMMAND]
+
+For more information, try '--help'.
+
+ tip: did you mean wt step squash?