Skip to main content

ctl_core/flags/
output.rs

1use clap::Args;
2
3use crate::color::ColorMode;
4use crate::flags::resolve_color;
5use crate::format::OutputFormat;
6
7/// Output flags for the root parser: format, colour, and quiet. Flatten it with
8/// `#[command(flatten)]`.
9///
10/// The flags are global, so a subcommand accepts them too. [`Self::view`]
11/// builds the [`View`] for the parsed flags. A CLI that already uses `-c`
12/// flattens [`ColorLong`](super::ColorLong) plus [`FormatArgs`] instead.
13#[cfg_attr(
14    feature = "view",
15    doc = "",
16    doc = "[`Self::view`]: Self::view",
17    doc = "[`View`]: crate::view::View"
18)]
19#[cfg_attr(
20    not(feature = "view"),
21    doc = "",
22    doc = "[`Self::view`]: crate#cargo-features",
23    doc = "[`View`]: crate#cargo-features"
24)]
25///
26/// The colour flags are private, so [`Self::color`] is their only reader and
27/// `--no-color` is never skipped. A flag can join the struct in a minor
28/// release.
29#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
30#[command(about = None, long_about = None)]
31#[non_exhaustive]
32pub struct OutputArgs {
33    /// Output representation.
34    #[arg(
35        short = 'f',
36        long,
37        global = true,
38        value_enum,
39        default_value = "pretty",
40        help_heading = "Output"
41    )]
42    pub format: OutputFormat,
43    /// Pretty-output colour policy. JSON never contains ANSI.
44    #[arg(
45        short = 'c',
46        long,
47        global = true,
48        value_enum,
49        default_value = "auto",
50        help_heading = "Output"
51    )]
52    color: ColorMode,
53    /// Force colourless pretty output. Wins over `--color`.
54    #[arg(long, global = true, help_heading = "Output")]
55    no_color: bool,
56    /// Suppress successful pretty output.
57    #[arg(short = 'q', long, global = true, help_heading = "Output")]
58    pub quiet: bool,
59}
60
61impl OutputArgs {
62    /// The colour mode in force: [`ColorMode::Never`] when `--no-color` is
63    /// set, otherwise the `--color` value.
64    #[must_use]
65    pub fn color(&self) -> ColorMode {
66        resolve_color(self.color, self.no_color)
67    }
68
69    /// Build the [`View`](crate::view::View) for these flags: the format, the
70    /// quiet setting, and the colour mode in force.
71    #[cfg(feature = "view")]
72    #[must_use]
73    pub fn view(&self) -> crate::view::View {
74        crate::view::View::new(self.format, self.color()).quiet(self.quiet)
75    }
76}
77
78/// `-f/--format` and `-q/--quiet`, with no colour flags.
79///
80/// Pair it with [`ColorLong`](super::ColorLong) when the CLI owns `-c`, and
81/// pass the colour mode to [`Self::view`].
82#[cfg_attr(feature = "view", doc = "", doc = "[`Self::view`]: Self::view")]
83#[cfg_attr(
84    not(feature = "view"),
85    doc = "",
86    doc = "[`Self::view`]: crate#cargo-features"
87)]
88///
89/// A flag can join it in a minor release.
90#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
91#[command(about = None, long_about = None)]
92#[non_exhaustive]
93pub struct FormatArgs {
94    /// Output representation.
95    #[arg(
96        short = 'f',
97        long,
98        global = true,
99        value_enum,
100        default_value = "pretty",
101        help_heading = "Output"
102    )]
103    pub format: OutputFormat,
104    /// Suppress successful pretty output.
105    #[arg(short = 'q', long, global = true, help_heading = "Output")]
106    pub quiet: bool,
107}
108
109impl FormatArgs {
110    /// Build the [`View`](crate::view::View) for these flags, with `color` as
111    /// the colour mode and the parsed format and quiet setting.
112    #[cfg(feature = "view")]
113    #[must_use]
114    pub fn view(&self, color: ColorMode) -> crate::view::View {
115        crate::view::View::new(self.format, color).quiet(self.quiet)
116    }
117}
118
119/// `--format` and `-q`/`--quiet` without the `-f` short flag, for CLIs that
120/// already use `-f` for a domain flag such as `--file`.
121///
122/// A flag can join it in a minor release.
123#[derive(Args, Clone, Debug, Default, Eq, PartialEq)]
124#[command(about = None, long_about = None)]
125#[non_exhaustive]
126pub struct FormatLong {
127    /// Output representation.
128    #[arg(
129        long,
130        global = true,
131        value_enum,
132        default_value = "pretty",
133        help_heading = "Output"
134    )]
135    pub format: OutputFormat,
136    /// Suppress successful pretty output.
137    #[arg(short = 'q', long, global = true, help_heading = "Output")]
138    pub quiet: bool,
139}
140
141impl FormatLong {
142    /// Build the [`View`](crate::view::View) for these flags, with `color` as
143    /// the colour mode and the parsed format and quiet setting.
144    #[cfg(feature = "view")]
145    #[must_use]
146    pub fn view(&self, color: ColorMode) -> crate::view::View {
147        crate::view::View::new(self.format, color).quiet(self.quiet)
148    }
149}