webview_bundle_cli/
options.rs

1use crate::logging::{LoggingKind, LoggingLevel};
2use bpaf::Bpaf;
3use std::str::FromStr;
4
5#[derive(Debug, Clone, Bpaf)]
6pub struct CliOptions {
7  /// Set the formatting mode for markup: "off" prints everything as plain text, "force" forces the formatting of markup using ANSI even if the console output is determined to be incompatible
8  #[bpaf(long("colors"), argument("off|force"))]
9  pub colors: Option<ColorsArg>,
10
11  /// The level of logging. In order, from the most verbose to the least verbose: debug, info, warn, error.
12  ///
13  /// The value `none` won't show any logging.
14  #[bpaf(
15    long("log-level"),
16    argument("none|debug|info|warn|error"),
17    fallback(LoggingLevel::default()),
18    display_fallback
19  )]
20  pub log_level: LoggingLevel,
21
22  /// How the log should look like.
23  #[bpaf(
24    long("log-kind"),
25    argument("pretty|compact|json"),
26    fallback(LoggingKind::default()),
27    display_fallback
28  )]
29  pub log_kind: LoggingKind,
30}
31
32#[derive(Debug, PartialEq, Eq, Clone)]
33pub enum ColorsArg {
34  Off,
35  Force,
36}
37
38impl FromStr for ColorsArg {
39  type Err = String;
40
41  fn from_str(s: &str) -> Result<Self, Self::Err> {
42    match s {
43      "off" => Ok(Self::Off),
44      "force" => Ok(Self::Force),
45      _ => Err(format!(
46        "value {s:?} is not valid for the --colors argument"
47      )),
48    }
49  }
50}
51
52#[cfg(test)]
53mod tests {
54  use super::*;
55
56  #[test]
57  fn color_args_from_str() {
58    assert_eq!("off".parse::<ColorsArg>(), Ok(ColorsArg::Off));
59    assert_eq!("force".parse::<ColorsArg>(), Ok(ColorsArg::Force));
60    assert_eq!(
61      "unknown".parse::<ColorsArg>(),
62      Err("value \"unknown\" is not valid for the --colors argument".to_string())
63    );
64  }
65}