Skip to main content

lintel_cli_common/
lib.rs

1use bpaf::Bpaf;
2
3/// Global options applied to all commands
4#[derive(Debug, Clone, Bpaf)]
5#[bpaf(generate(cli_global_options))]
6#[allow(clippy::upper_case_acronyms)]
7pub struct CLIGlobalOptions {
8    /// Set the formatting mode for markup: "off" prints everything as plain text,
9    /// "force" forces the formatting of markup using ANSI even if the console
10    /// output is determined to be incompatible
11    #[bpaf(long("colors"), argument("off|force"))]
12    pub colors: Option<ColorsArg>,
13
14    /// Print additional diagnostics, and some diagnostics show more information.
15    /// Also, print out what files were processed and which ones were modified.
16    #[bpaf(short('v'), long("verbose"), switch, fallback(false))]
17    pub verbose: bool,
18
19    /// The level of logging. In order, from the most verbose to the least verbose:
20    /// debug, info, warn, error.
21    #[bpaf(
22        long("log-level"),
23        argument("none|debug|info|warn|error"),
24        fallback(LogLevel::None),
25        display_fallback
26    )]
27    pub log_level: LogLevel,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum ColorsArg {
32    Off,
33    Force,
34}
35
36impl core::str::FromStr for ColorsArg {
37    type Err = String;
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        match s {
40            "off" => Ok(Self::Off),
41            "force" => Ok(Self::Force),
42            _ => Err(format!("expected 'off' or 'force', got '{s}'")),
43        }
44    }
45}
46
47#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
48pub enum LogLevel {
49    #[default]
50    None,
51    Debug,
52    Info,
53    Warn,
54    Error,
55}
56
57impl core::str::FromStr for LogLevel {
58    type Err = String;
59    fn from_str(s: &str) -> Result<Self, Self::Err> {
60        match s {
61            "none" => Ok(Self::None),
62            "debug" => Ok(Self::Debug),
63            "info" => Ok(Self::Info),
64            "warn" => Ok(Self::Warn),
65            "error" => Ok(Self::Error),
66            _ => Err(format!(
67                "expected 'none', 'debug', 'info', 'warn', or 'error', got '{s}'"
68            )),
69        }
70    }
71}
72
73impl core::fmt::Display for LogLevel {
74    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        match self {
76            Self::None => write!(f, "none"),
77            Self::Debug => write!(f, "debug"),
78            Self::Info => write!(f, "info"),
79            Self::Warn => write!(f, "warn"),
80            Self::Error => write!(f, "error"),
81        }
82    }
83}