Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add trailing argument support to `wt switch --execute` (#271)
Maximilian Roos2d84e93f8faa
diff --git a/Cargo.lock b/Cargo.lockindex a9371df23..1dcc2a9d2 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3093,6 +3093,7 @@ dependencies = ["serde_json","shell-escape","shellexpand",+ "shlex","skim","strum","synoptic",diff --git a/Cargo.toml b/Cargo.tomlindex 5e283472a..3fc47a799 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -80,6 +80,7 @@ serde = { version = "1.0", features = ["derive"] }serde_json = "1.0"shell-escape = "0.1"shellexpand = "3.1"+shlex = "1.3"strum = { version = "0.27", features = ["derive"] }synoptic = "2"terminal_size = "0.4"diff --git a/docs/content/switch.md b/docs/content/switch.mdindex 06a338531..2092bc006 100644--- a/docs/content/switch.md+++ b/docs/content/switch.md@@ -67,7 +67,7 @@ wt switch --create fix --base=@ # Branch from current HEAD```wt switch - Switch to a worktree-Usage: wt switch [OPTIONS] <BRANCH>+Usage: wt switch [OPTIONS] <BRANCH> [-- <EXECUTE_ARGS>...]Arguments:<BRANCH>@@ -75,6 +75,12 @@ Arguments:Shortcuts: '^' (main), '-' (previous), '@' (current)+ [EXECUTE_ARGS]...+ Additional arguments for --execute command (after --)++ Arguments after -- are appended to the execute command. Each argument+ is POSIX shell-escaped before appending.+Options:-c, --createCreate a new branch@@ -91,12 +97,14 @@ Options:full terminal control. Useful for launching editors, AI agents, orother interactive tools.- Especially useful in shell aliases to create a worktree and start- working in one command:+ Especially useful with shell aliases:- alias wsc='wt switch --create --execute=claude'+ alias wsc='wt switch --create -x claude'+ wsc feature-branch -- 'implement the login flow'Then wsc feature-branch creates the worktree and launches Claude Code.+ Arguments after -- are passed to the command, so wsc feature --+ 'implement login' works.-f, --forceSkip approval promptsdiff --git a/src/cli.rs b/src/cli.rsindex f1c3ba180..f672ebd40 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -2170,17 +2170,26 @@ wt switch --create fix --base=@ # Branch from current HEAD/// it full terminal control. Useful for launching editors, AI agents,/// or other interactive tools.///- /// Especially useful in shell aliases to create a worktree and start- /// working in one command:+ /// Especially useful with shell aliases:////// ```sh- /// alias wsc='wt switch --create --execute=claude'+ /// alias wsc='wt switch --create -x claude'+ /// wsc feature-branch -- 'implement the login flow'/// ```///- /// Then `wsc feature-branch` creates the worktree and launches Claude Code.+ /// Then `wsc feature-branch` creates the worktree and launches Claude+ /// Code. Arguments after `--` are passed to the command, so+ /// `wsc feature -- 'implement login'` works.#[arg(short = 'x', long)]execute: Option<String>,+ /// Additional arguments for --execute command (after --)+ ///+ /// Arguments after `--` are appended to the execute command.+ /// Each argument is POSIX shell-escaped before appending.+ #[arg(last = true, requires = "execute")]+ execute_args: Vec<String>,+/// Skip approval prompts#[arg(short = 'f', long)]force: bool,diff --git a/src/commands/select.rs b/src/commands/select.rsindex 6141c2419..35d18357d 100644--- a/src/commands/select.rs+++ b/src/commands/select.rs@@ -110,7 +110,10 @@ fn run_git_diff_with_pager(git_args: &[&str], pager_cmd: &str) -> Option<String>// Build shell pipeline: git <args> | pager// Shell-escape args to handle paths with spaces- let escaped_args: Vec<String> = git_args.iter().map(|arg| shell_escape(arg)).collect();+ let escaped_args: Vec<String> = git_args+ .iter()+ .map(|arg| shlex::try_quote(arg).unwrap_or((*arg).into()).into_owned())+ .collect();let pipeline = format!("git {} | {}", escaped_args.join(" "), pager_with_args);log::debug!("Running pager pipeline: {}", pipeline);@@ -172,18 +175,6 @@ fn run_git_diff_with_pager(git_args: &[&str], pager_cmd: &str) -> Option<String>}}-/// Shell-escape a string for use in sh -c commands.-fn shell_escape(s: &str) -> String {- // If it contains special chars, wrap in single quotes and escape existing single quotes- if s.chars()- .any(|c| c.is_whitespace() || "\"'\\$`!*?[]{}|&;<>()".contains(c))- {- format!("'{}'", s.replace('\'', "'\\''"))- } else {- s.to_string()- }-}-/// Preview modes for the interactive selector////// Each mode shows a different aspect of the worktree:@@ -1099,42 +1090,6 @@ mod tests {assert!(pager_needs_paging_disabled("batcat")); // Debian's bat package name}- #[test]- fn test_shell_escape_simple() {- // Simple strings pass through unchanged- assert_eq!(shell_escape("hello"), "hello");- assert_eq!(shell_escape("foo-bar"), "foo-bar");- assert_eq!(shell_escape("path/to/file"), "path/to/file");- }-- #[test]- fn test_shell_escape_with_spaces() {- // Strings with spaces get quoted- assert_eq!(shell_escape("hello world"), "'hello world'");- assert_eq!(shell_escape("path/to/my file"), "'path/to/my file'");- }-- #[test]- fn test_shell_escape_with_special_chars() {- // Special shell characters get quoted- assert_eq!(shell_escape("foo$bar"), "'foo$bar'");- assert_eq!(shell_escape("foo`bar"), "'foo`bar'");- assert_eq!(shell_escape("foo;bar"), "'foo;bar'");- assert_eq!(shell_escape("foo|bar"), "'foo|bar'");- assert_eq!(shell_escape("foo&bar"), "'foo&bar'");- assert_eq!(shell_escape("foo*bar"), "'foo*bar'");- assert_eq!(shell_escape("foo?bar"), "'foo?bar'");- }-- #[test]- fn test_shell_escape_with_quotes() {- // Single quotes get escaped specially- assert_eq!(shell_escape("it's"), "'it'\\''s'");- assert_eq!(shell_escape("don't"), "'don'\\''t'");- // Double quotes trigger quoting but don't need internal escaping- assert_eq!(shell_escape("say \"hi\""), "'say \"hi\"'");- }-#[test]fn test_render_preview_tabs_working_tree_mode() {let output = WorktreeSkimItem::render_preview_tabs(PreviewMode::WorkingTree);diff --git a/src/completion.rs b/src/completion.rsindex 76288c2ba..51841bf68 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -360,7 +360,6 @@ thread_local! {fn completion_command() -> Command {let cmd = cli::build_command();- let cmd = adjust_completion_command(cmd);hide_non_positional_options_for_completion(cmd)}@@ -409,38 +408,3 @@ fn hide_non_positional_options_for_completion(cmd: Command) -> Command {process_command(cmd, true)}--// Mark positional args as `.last(true)` to allow them after all flags.-// This enables flexible argument ordering like:-// - `wt switch --create --execute=cmd --base=main feature` instead of `wt switch feature --create --execute=cmd --base=main`-// - `wt merge --no-squash main` instead of `wt merge main --no-squash`-// - `wt remove --no-delete-branch feature` instead of `wt remove feature --no-delete-branch`-fn adjust_completion_command(cmd: Command) -> Command {- cmd.mut_subcommand("switch", |switch| {- switch.mut_arg("branch", |arg| arg.last(true))- })- .mut_subcommand("remove", |remove| {- remove.mut_arg("worktrees", |arg| arg.last(true))- })- .mut_subcommand("merge", |merge| {- merge.mut_arg("target", |arg| arg.last(true))- })- .mut_subcommand("step", |step| {- step.mut_subcommand("push", |push| push.mut_arg("target", |arg| arg.last(true)))- .mut_subcommand("squash", |squash| {- squash.mut_arg("target", |arg| arg.last(true))- })- .mut_subcommand("rebase", |rebase| {- rebase.mut_arg("target", |arg| arg.last(true))- })- })- .mut_subcommand("hook", |hook| {- // Hook subcommands - allow name after --force- hook.mut_subcommand("post-create", |c| c.mut_arg("name", |arg| arg.last(true)))- .mut_subcommand("post-start", |c| c.mut_arg("name", |arg| arg.last(true)))- .mut_subcommand("pre-commit", |c| c.mut_arg("name", |arg| arg.last(true)))- .mut_subcommand("pre-merge", |c| c.mut_arg("name", |arg| arg.last(true)))- .mut_subcommand("post-merge", |c| c.mut_arg("name", |arg| arg.last(true)))- .mut_subcommand("pre-remove", |c| c.mut_arg("name", |arg| arg.last(true)))- })-}diff --git a/src/main.rs b/src/main.rsindex 550bb140b..6fd95f25d 100644--- a/src/main.rs+++ b/src/main.rs@@ -1184,6 +1184,7 @@ fn main() {create,base,execute,+ execute_args,force,clobber,verify,@@ -1253,8 +1254,19 @@ fn main() {}// Execute user command after post-start hooks have been spawned+ // Note: execute_args requires execute via clap's `requires` attributeif let Some(cmd) = execute {- execute_user_command(&cmd)?;+ // Append any trailing args (after --) to the execute command+ let full_cmd = if execute_args.is_empty() {+ cmd+ } else {+ let escaped_args: Vec<_> = execute_args+ .iter()+ .map(|arg| shlex::try_quote(arg).unwrap_or(arg.into()).into_owned())+ .collect();+ format!("{} {}", cmd, escaped_args.join(" "))+ };+ execute_user_command(&full_cmd)?;}Ok(())diff --git a/tests/snapshots/integration__integration_tests__help__help_page_switch.snap b/tests/snapshots/integration__integration_tests__help__help_page_switch.snapindex 877f48459..e666ca42b 100644--- a/tests/snapshots/integration__integration_tests__help__help_page_switch.snap+++ b/tests/snapshots/integration__integration_tests__help__help_page_switch.snap@@ -9,6 +9,7 @@ info:CLICOLOR_FORCE: "1"COLUMNS: "150"GIT_EDITOR: ""+ RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml---@@ -76,7 +77,7 @@ wt switch --create fix --base=@ # Branch from current HEAD```wt switch - Switch to a worktree-Usage: wt switch [OPTIONS] <BRANCH>+Usage: wt switch [OPTIONS] <BRANCH> [-- <EXECUTE_ARGS>...]Arguments:<BRANCH>@@ -84,6 +85,12 @@ Arguments:Shortcuts: '^' (main), '-' (previous), '@' (current)+ [EXECUTE_ARGS]...+ Additional arguments for --execute command (after --)++ Arguments after -- are appended to the execute command. Each argument+ is POSIX shell-escaped before appending.+Options:-c, --createCreate a new branch@@ -100,12 +107,14 @@ Options:full terminal control. Useful for launching editors, AI agents, orother interactive tools.- Especially useful in shell aliases to create a worktree and start- working in one command:+ Especially useful with shell aliases:- alias wsc='wt switch --create --execute=claude'+ alias wsc='wt switch --create -x claude'+ wsc feature-branch -- 'implement the login flow'Then wsc feature-branch creates the worktree and launches Claude Code.+ Arguments after -- are passed to the command, so wsc feature --+ 'implement login' works.-f, --forceSkip approval promptsdiff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snapindex 1cdf1aedd..1476ad5f8 100644--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap@@ -9,6 +9,7 @@ info:CLICOLOR_FORCE: "1"COLUMNS: "150"GIT_EDITOR: ""+ RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml---@@ -19,7 +20,7 @@ exit_code: 0----- stderr -----wt switch - Switch to a worktree-Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>+Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>[0m [1m[36m[--[0m [36m<EXECUTE_ARGS>...[0m[1m[36m][1m[32mArguments:[36m<BRANCH>@@ -27,6 +28,11 @@ Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>Shortcuts: '^' (main), '-' (previous), '@' (current)+ [36m[EXECUTE_ARGS]...+ Additional arguments for --execute command (after --)++ Arguments after [1m--[0m are appended to the execute command. Each argument is POSIX shell-escaped before appending.+[1m[32mOptions:[1m[36m-c[0m, [1m[36m--createCreate a new branch@@ -42,11 +48,13 @@ Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>Replaces the wt process with the command after switching, giving it full terminal control. Useful for launching editors, AI agents, or otherinteractive tools.- Especially useful in shell aliases to create a worktree and start working in one command:+ Especially useful with shell aliases:- [1m[1malias wsc='wt switch --create --execute=claude'+ [1m[1malias wsc='wt switch --create -x claude'+ [1mwsc feature-branch -- 'implement the login flow'[0m- Then [1mwsc feature-branch[0m creates the worktree and launches Claude Code.+ Then [1mwsc feature-branch[0m creates the worktree and launches Claude Code. Arguments after [1m--[0m are passed to the command, so [1mwsc feature --+ 'implement login'[0m works.[1m[36m-f[0m, [1m[36m--forceSkip approval promptsdiff --git a/tests/snapshots/integration__integration_tests__help__help_switch_short.snap b/tests/snapshots/integration__integration_tests__help__help_switch_short.snapindex 9c636863f..4d0d6e0ef 100644--- a/tests/snapshots/integration__integration_tests__help__help_switch_short.snap+++ b/tests/snapshots/integration__integration_tests__help__help_switch_short.snap@@ -9,6 +9,7 @@ info:CLICOLOR_FORCE: "1"COLUMNS: "150"GIT_EDITOR: ""+ RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml---@@ -19,10 +20,11 @@ exit_code: 0----- stderr -----wt switch - Switch to a worktree-Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>+Usage: [1m[36mwt switch[0m [36m[OPTIONS][0m [36m<BRANCH>[0m [1m[36m[--[0m [36m<EXECUTE_ARGS>...[0m[1m[36m][1m[32mArguments:- [36m<BRANCH>[0m Branch or worktree name+ [36m<BRANCH>[0m Branch or worktree name+ [36m[EXECUTE_ARGS]...[0m Additional arguments for --execute command (after --)[1m[32mOptions:[1m[36m-c[0m, [1m[36m--create[0m Create a new branchdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_missing_argument_hints.snap b/tests/snapshots/integration__integration_tests__switch__switch_missing_argument_hints.snapindex c13dd9dc8..26c84a487 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_missing_argument_hints.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_missing_argument_hints.snap@@ -5,6 +5,7 @@ info:args:- switchenv:+ APPDATA: "[TEST_CONFIG_HOME]"CLICOLOR_FORCE: "1"COLUMNS: "150"GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"@@ -12,12 +13,16 @@ info:GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"GIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"HOME: "[TEST_HOME]"LANG: CLC_ALL: C+ PATH: "[PATH]"+ RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"USERPROFILE: "[TEST_HOME]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"---success: falseexit_code: 2@@ -27,7 +32,7 @@ exit_code: 2[1m[31merror:[0m the following required arguments were not provided:[1m[32m<BRANCH>-[1m[32mUsage:[0m [1m[36mwt switch[0m [36m<BRANCH>+[1m[32mUsage:[0m [1m[36mwt switch[0m [36m<BRANCH>[0m [1m[36m[--[0m [36m<EXECUTE_ARGS>...[0m[1m[36m]For more information, try '[1m[36m--help[0m'.[32m[1mQuick switches:[39m[22m