Skip to content

Commit 2a4dc09

Browse files
kylehgcclaude
andcommitted
fix(cli): exempt rtk format from help forwarding, opt prisma parents out, tree -h is a size flag
Review round 4 on #165 (approved with notes): format is rtk's own aggregator so it joins RTK_OWN_RUNNERS; Prisma and Prisma::Migrate are subcommand parents with no external arm and now carry the opt-out, and the contract walk treats a parent as forwarding when any of its subcommands do, which is what would have caught them; tree -h means human sizes like ls -h; the capture help path maps the exit status through exit_code_from_status like every other capture. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 372cf48 commit 2a4dc09

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎src/core/runner.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ where
172172
}
173173

174174
/// Tools that define `-h` as something other than help: `psql -h host`,
175-
/// `ls -h` (human sizes), `grep`/`rg -h` (no filename). Everywhere else a
176-
/// bare `-h` is the usage request it is for cargo, go, dotnet, git, …
177-
const DASH_H_IS_NOT_HELP: &[&str] = &["psql", "ls", "grep", "rg"];
175+
/// `ls`/`tree -h` (human sizes), `grep`/`rg -h` (no filename). Everywhere
176+
/// else a bare `-h` is the usage request it is for cargo, go, dotnet, git, …
177+
const DASH_H_IS_NOT_HELP: &[&str] = &["psql", "ls", "tree", "grep", "rg"];
178178

179179
/// `--help` (or `-h`, unless the tool defines it) before any `--` asks the
180180
/// tool for its usage. A filter models the tool's normal output, so it reads
@@ -962,6 +962,7 @@ mod err_test_runner_tests {
962962
)));
963963
assert!(!requests_help(&build("C:\\tools\\ls.exe", &["-lh"])));
964964
assert!(!requests_help(&build("ls", &["-h"])));
965+
assert!(!requests_help(&build("tree", &["-h", "-L", "2"])));
965966
assert!(!requests_help(&build("grep", &["-h", "pattern", "a", "b"])));
966967
assert!(!requests_help(&build("rg.exe", &["-h", "x"])));
967968
// …but `--help` is help for them too.

‎src/core/stream.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn capture(cmd: &mut Command) -> Result<CaptureResult> {
607607
.stderr(Stdio::inherit())
608608
.status()
609609
.with_context(|| format!("Failed to execute {program}"))?;
610-
std::process::exit(status.code().unwrap_or(1));
610+
std::process::exit(super::utils::exit_code_from_status(&status, &program));
611611
}
612612
let output = cmd.output().context("Failed to execute command")?;
613613
let exit_code = super::utils::exit_code_from_output(&output, &program);

‎src/main.rs‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ enum Commands {
540540
},
541541

542542
/// Prisma commands with compact output (no ASCII art)
543+
// `--help` belongs to the tool: see forward_help_to_wrapped_tools.
544+
#[command(disable_help_flag = true)]
543545
Prisma {
544546
#[command(subcommand)]
545547
command: PrismaCommands,
@@ -1220,6 +1222,8 @@ enum PrismaCommands {
12201222
args: Vec<String>,
12211223
},
12221224
/// Manage migrations
1225+
// `--help` belongs to the tool: see forward_help_to_wrapped_tools.
1226+
#[command(disable_help_flag = true)]
12231227
Migrate {
12241228
#[command(subcommand)]
12251229
command: PrismaMigrateCommands,
@@ -1844,7 +1848,7 @@ fn is_native_test_expression(command: &[String]) -> bool {
18441848
/// tsc, and through the hook that is what `tsc --help` came back as.
18451849
///
18461850
/// The exact exemption is [`keeps_clap_help`]: rtk's own entry points
1847-
/// (`RTK_META_COMMANDS`, plus the `sh -c` runners `err`, `test`, `summary`),
1851+
/// (`RTK_META_COMMANDS`, plus `RTK_OWN_RUNNERS`: `err`, `test`, `summary`, `format`),
18481852
/// and everything beneath them (`rtk hook check <args>`), keep clap's help
18491853
/// even when they take a trailing command, because there is no tool to hand
18501854
/// it to. Subcommands with nothing to forward (`gain`, `init`, …) are
@@ -1862,13 +1866,14 @@ fn forward_help_to_wrapped_tools(cmd: clap::Command) -> clap::Command {
18621866
forward_help_below(cmd, 0, false)
18631867
}
18641868

1865-
/// rtk subcommands that take a trailing command but run it themselves
1866-
/// (`sh -c`), so there is no tool to receive `--help`.
1867-
const SHELL_RUNNERS: &[&str] = &["err", "test", "summary"];
1869+
/// rtk subcommands that take a trailing command but run it themselves —
1870+
/// through `sh -c` (`err`, `test`, `summary`) or a detected formatter
1871+
/// (`format`) — so there is no one tool to receive `--help`.
1872+
const RTK_OWN_RUNNERS: &[&str] = &["err", "test", "summary", "format"];
18681873

18691874
/// A top-level subcommand that is rtk's own: `--help` on it means rtk's help.
18701875
fn keeps_clap_help(name: &str) -> bool {
1871-
core::constants::RTK_META_COMMANDS.contains(&name) || SHELL_RUNNERS.contains(&name)
1876+
core::constants::RTK_META_COMMANDS.contains(&name) || RTK_OWN_RUNNERS.contains(&name)
18721877
}
18731878

18741879
/// `depth` 1 is a direct child of `rtk`: the rtk-owned names are only rtk's
@@ -3272,6 +3277,7 @@ mod tests {
32723277
vec!["rtk", "err", "--help"],
32733278
vec!["rtk", "test", "--help"],
32743279
vec!["rtk", "summary", "--help"],
3280+
vec!["rtk", "format", "--help"],
32753281
] {
32763282
let err = match parse_cli(argv.clone()) {
32773283
Err(e) => e,
@@ -3287,8 +3293,15 @@ mod tests {
32873293
// hyphen args or an external subcommand) must not let clap claim
32883294
// `--help`; a meta command or one that forwards nothing must keep it.
32893295
fn walk(cmd: &clap::Command, path: &str, depth: usize, under_meta: bool, seen: &mut usize) {
3290-
let forwards = cmd.get_positionals().any(|a| a.is_trailing_var_arg_set())
3291-
|| cmd.is_allow_external_subcommands_set();
3296+
// A parent counts as forwarding when any subcommand of its own does:
3297+
// `rtk prisma --help` is prisma's even though `prisma` itself takes
3298+
// no trailing args.
3299+
fn forwards(cmd: &clap::Command) -> bool {
3300+
cmd.get_positionals().any(|a| a.is_trailing_var_arg_set())
3301+
|| cmd.is_allow_external_subcommands_set()
3302+
|| cmd.get_subcommands().any(forwards)
3303+
}
3304+
let forwards = depth > 0 && forwards(cmd);
32923305
let meta = under_meta || (depth == 1 && keeps_clap_help(cmd.get_name()));
32933306
if meta && cmd.get_name() == "help" {
32943307
// clap's own `help` subcommand (and its per-subcommand

0 commit comments

Comments
 (0)