Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(step): honor -C and --config in `wt step --help` (#2176)
Maximilian Roos9d6c5b71d4a8
diff --git a/src/help.rs b/src/help.rsindex 8f9e4b90a..744f36295 100644--- a/src/help.rs+++ b/src/help.rs@@ -69,7 +69,11 @@ use crate::cli;/// respects `-h` (short) vs `--help` (long) distinction.////// Returns `true` if help was handled (caller should exit), `false` to continue normal parsing.-pub fn maybe_handle_help_with_pager() -> bool {+///+/// `is_step_help` is computed by the caller from the same early-parse pass that+/// extracts global options, and controls whether we splice the configured+/// aliases into the rendered output.+pub fn maybe_handle_help_with_pager(is_step_help: bool) -> bool {let args: Vec<String> = std::env::args().collect();// --help uses pager, -h prints directly (git convention)@@ -140,7 +144,7 @@ pub fn maybe_handle_help_with_pager() -> bool {// Splice configured aliases into `wt step --help` / `-h`// so the help here matches bare `wt step`. Scoped to the// step subcommand only — other help passes through.- let clap_output = if is_help_for_step(&args) {+ let clap_output = if is_step_help {crate::commands::augment_step_help(&clap_output)} else {clap_output@@ -179,34 +183,6 @@ pub fn maybe_handle_help_with_pager() -> bool {}}-/// True when the help being rendered is for `wt step` (no subcommand past-/// `step`). Used to splice the configured-aliases listing into the help-/// output, matching the bare `wt step` behavior.-///-/// Scans positional args rather than re-parsing with clap because clap's-/// `DisplayHelp` error doesn't expose the command path at which help was-/// requested. Handles the two global flags that take values (`-C <path>`,-/// `--config <path>`) so `wt -C some/path step --help` still classifies.-/// The `--config=path` attached form needs no special handling — the value-/// is part of the same arg, which already gets skipped as a flag.-fn is_help_for_step(args: &[String]) -> bool {- const VALUE_FLAGS: &[&str] = &["-C", "--config"];- let mut positionals = Vec::new();- let mut i = 1; // skip binary name- while i < args.len() {- let arg = args[i].as_str();- if VALUE_FLAGS.contains(&arg) {- i += 2; // skip flag and its value- continue;- }- if !arg.starts_with('-') {- positionals.push(arg);- }- i += 1;- }- positionals == ["step"]-}-/// Get the help reference block with configurable color output.////// `ColorChoice::Always` produces ANSI codes for HTML conversion (web docs).diff --git a/src/main.rs b/src/main.rsindex a9d88c53a..ceaee2b55 100644--- a/src/main.rs+++ b/src/main.rs@@ -938,8 +938,16 @@ fn parse_cli() -> Option<Cli> {return None;}+ // Apply -C / --config before help handling so `wt -C other step --help`+ // and `wt --config custom.toml step --help` resolve aliases against the+ // requested repo and user config (not the process cwd / default config).+ // The same early parse also tells us whether this is `wt step` help, so+ // the splice path in `augment_step_help` has no separate arg scanner.+ let (directory, config, is_step_help) = parse_early_globals();+ apply_global_options(directory, config);+// Handle --help with pager before clap processes it- if help::maybe_handle_help_with_pager() {+ if help::maybe_handle_help_with_pager(is_step_help) {return None;}@@ -968,6 +976,35 @@ fn apply_global_options(directory: Option<std::path::PathBuf>, config: Option<st}}+/// Parse global options (`-C`, `--config`) and detect whether this invocation+/// is `wt step` help, in a single pass against the real `Cli` definition.+///+/// Uses `ignore_errors(true)` so unknown args, missing values, and `--help`+/// don't abort parsing — we just read what matched. This lets `wt -C other+/// step --help` apply `-C` before the help path renders, so `augment_step_help`+/// resolves aliases against the requested repo instead of the process cwd.+///+/// Using `cli::build_command()` rather than a hand-rolled mini-command keeps+/// the global-flag definitions in one place (the derive on `Cli`), so renaming+/// `-C` or adding a value-taking global doesn't silently desync this path.+fn parse_early_globals() -> (Option<std::path::PathBuf>, Option<std::path::PathBuf>, bool) {+ let cmd = cli::build_command()+ .ignore_errors(true)+ .disable_help_flag(true);+ let Ok(matches) = cmd.try_get_matches_from(std::env::args_os()) else {+ return (None, None, false);+ };+ let directory = matches.get_one::<std::path::PathBuf>("directory").cloned();+ let config = matches.get_one::<std::path::PathBuf>("config").cloned();+ // `wt step --help` (or `-h`) lands here with the `step` subcommand matched+ // and nothing past it. `wt step promote --help` has a nested subcommand+ // and should render plain clap help without the aliases splice.+ let is_step_help = matches+ .subcommand()+ .is_some_and(|(name, sub)| name == "step" && sub.subcommand_name().is_none());+ (directory, config, is_step_help)+}+fn init_command_log(command_line: &str) {// Initialize command log for always-on logging of hooks and LLM commands.// Directory and file are created lazily on first log_command() call.@@ -1205,6 +1242,9 @@ fn main() {verbose,command,} = cli;+ // Globals were already applied in `parse_cli` before help rendering;+ // OnceLock makes this call a no-op, but keeping it avoids touching the+ // existing destructure pattern.apply_global_options(directory.clone(), config);let command_line = std::env::args().collect::<Vec<_>>().join(" ");diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex ad4bad9f4..58a82ca56 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -1146,6 +1146,39 @@ fn test_step_help_silent_with_deprecated_user_config(repo: TestRepo) {);}+/// `wt -C <other> step --help` lists aliases from `<other>`'s project config,+/// not from the process cwd. Without applying global options before the help+/// branch, the Aliases section was rendered from the wrong repo.+#[cfg(not(windows))]+#[rstest]+fn test_step_help_honors_dash_c(repo: TestRepo) {+ repo.write_project_config(+ r#"+[aliases]+xyzzy = "echo nothing happens"+"#,+ );+ repo.commit("Add alias config");+ let repo_path = repo.root_path().to_path_buf();++ // Invoke from a directory that is *not* inside the repo so the alias can+ // only be discovered via -C. Using the system temp dir keeps this+ // independent of the test's working directory.+ let cwd = std::env::temp_dir();+ let mut cmd = repo.wt_command();+ cmd.current_dir(&cwd)+ .args(["-C", repo_path.to_str().unwrap(), "step", "--help"]);+ let output = cmd.output().expect("failed to run wt");++ let stdout = String::from_utf8_lossy(&output.stdout);+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stdout.contains("xyzzy"),+ "expected `xyzzy` alias to appear in `wt -C <repo> step --help` output\n\+ stdout:\n{stdout}\nstderr:\n{stderr}"+ );+}+/// Declining approval prevents alias execution#[rstest]fn test_alias_approval_decline(mut repo: TestRepo) {