kaish_tool_api/global_flags.rs
1//! Global flags shared by every builtin via `#[command(flatten)]`.
2//!
3//! Today this is just `--json`. Every builtin flattens `GlobalFlags` into its
4//! own clap struct and calls `parsed.global.apply(ctx)` after parsing; the
5//! kernel reads the output format the flag set (via
6//! [`ToolCtx::set_output_format`](crate::ToolCtx::set_output_format)) after
7//! `execute()` returns and applies it. See `docs/clap-migration.md`.
8
9use clap::Args;
10
11use kaish_types::OutputFormat;
12
13use crate::ctx::ToolCtx;
14use kaish_types::ToolArgs;
15
16/// Flags injected into every migrated builtin via `#[command(flatten)] global: GlobalFlags`.
17///
18/// Builtins call `parsed.global.apply(ctx)` after their own argv parse so the
19/// dispatcher can read the output format post-execute and apply it.
20#[derive(Args, Debug, Clone, Default)]
21pub struct GlobalFlags {
22 /// Render structured output as JSON.
23 #[arg(long)]
24 pub json: bool,
25}
26
27impl GlobalFlags {
28 /// Apply the flags to `ctx` so the dispatcher can pick them up after the
29 /// builtin's `execute()` returns.
30 pub fn apply(&self, ctx: &mut dyn ToolCtx) {
31 if self.json {
32 ctx.set_output_format(OutputFormat::Json);
33 }
34 }
35
36 /// Honor `--json` straight off `ToolArgs` before any per-builtin clap parse.
37 ///
38 /// The kernel calls this just before `tool.execute()` so the format is set
39 /// even when a builtin's own `try_parse_from` rejects argv and returns
40 /// before `parsed.global.apply(ctx)` would have run. Idempotent with the
41 /// per-builtin apply: both writing `OutputFormat::Json` yields the same
42 /// state.
43 pub fn apply_from_args(args: &ToolArgs, ctx: &mut dyn ToolCtx) {
44 if args.has_flag("json") {
45 ctx.set_output_format(OutputFormat::Json);
46 }
47 }
48}