|
| 1 | +// Copyright (c) 2026 Soheil Alizadeh / 8Network. All rights reserved. |
| 2 | +// Licensed under the Business Source License 1.1 (BSL-1.1). |
| 3 | +// See LICENSE file in the project root. |
| 4 | + |
| 5 | +//! Shared output format flags — one struct, all commands. |
| 6 | +
|
| 7 | +use o8v_core::render::Audience; |
| 8 | + |
| 9 | +/// Output format flags embedded in every command's Args via `#[command(flatten)]`. |
| 10 | +#[derive(clap::Args, Debug, Default)] |
| 11 | +pub struct OutputFormat { |
| 12 | + /// Plain text output for AI agents and pipes. |
| 13 | + #[arg(long, conflicts_with = "json")] |
| 14 | + pub plain: bool, |
| 15 | + |
| 16 | + /// JSON output for tools and CI. |
| 17 | + #[arg(long, conflicts_with = "plain")] |
| 18 | + pub json: bool, |
| 19 | + |
| 20 | + /// Disable colored output. |
| 21 | + #[arg(long)] |
| 22 | + pub no_color: bool, |
| 23 | +} |
| 24 | + |
| 25 | +impl OutputFormat { |
| 26 | + /// Resolve audience, using `default` when no explicit flag was passed. |
| 27 | + /// |
| 28 | + /// - `--json` → `Audience::Machine` (always, regardless of caller) |
| 29 | + /// - `--plain` → `Audience::Agent` (always, regardless of caller) |
| 30 | + /// - no flag → `default` (caller decides: Human for CLI, Agent for MCP) |
| 31 | + pub fn audience_with_default(&self, default: Audience) -> Audience { |
| 32 | + if self.json { |
| 33 | + Audience::Machine |
| 34 | + } else if self.plain { |
| 35 | + Audience::Agent |
| 36 | + } else { |
| 37 | + default |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + use o8v_core::render::Audience; |
| 46 | + |
| 47 | + fn fmt(json: bool, plain: bool, no_color: bool) -> OutputFormat { |
| 48 | + OutputFormat { json, plain, no_color } |
| 49 | + } |
| 50 | + |
| 51 | + // 1. No flags + Human default → Human |
| 52 | + #[test] |
| 53 | + fn no_flags_human_default_returns_human() { |
| 54 | + let f = fmt(false, false, false); |
| 55 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Human); |
| 56 | + } |
| 57 | + |
| 58 | + // 2. No flags + Agent default → Agent |
| 59 | + #[test] |
| 60 | + fn no_flags_agent_default_returns_agent() { |
| 61 | + let f = fmt(false, false, false); |
| 62 | + assert_eq!(f.audience_with_default(Audience::Agent), Audience::Agent); |
| 63 | + } |
| 64 | + |
| 65 | + // 3. --json + Human default → Machine |
| 66 | + #[test] |
| 67 | + fn json_flag_human_default_returns_machine() { |
| 68 | + let f = fmt(true, false, false); |
| 69 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Machine); |
| 70 | + } |
| 71 | + |
| 72 | + // 4. --json + Agent default → Machine |
| 73 | + #[test] |
| 74 | + fn json_flag_agent_default_returns_machine() { |
| 75 | + let f = fmt(true, false, false); |
| 76 | + assert_eq!(f.audience_with_default(Audience::Agent), Audience::Machine); |
| 77 | + } |
| 78 | + |
| 79 | + // 5. --plain + Human default → Agent |
| 80 | + #[test] |
| 81 | + fn plain_flag_human_default_returns_agent() { |
| 82 | + let f = fmt(false, true, false); |
| 83 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Agent); |
| 84 | + } |
| 85 | + |
| 86 | + // 6. --plain + Agent default → Agent |
| 87 | + #[test] |
| 88 | + fn plain_flag_agent_default_returns_agent() { |
| 89 | + let f = fmt(false, true, false); |
| 90 | + assert_eq!(f.audience_with_default(Audience::Agent), Audience::Agent); |
| 91 | + } |
| 92 | + |
| 93 | + // 7. Default trait → Human audience (all false) |
| 94 | + #[test] |
| 95 | + fn default_trait_gives_human_audience() { |
| 96 | + let f = OutputFormat::default(); |
| 97 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Human); |
| 98 | + } |
| 99 | + |
| 100 | + // 8. no_color doesn't affect audience |
| 101 | + #[test] |
| 102 | + fn no_color_does_not_affect_audience() { |
| 103 | + let f = fmt(false, false, true); |
| 104 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Human); |
| 105 | + let f = fmt(true, false, true); |
| 106 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Machine); |
| 107 | + let f = fmt(false, true, true); |
| 108 | + assert_eq!(f.audience_with_default(Audience::Human), Audience::Agent); |
| 109 | + } |
| 110 | +} |
0 commit comments