Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 2520ce80 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Add default cli for grimoire, with args-parsing!

parent bc81a609
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -276,6 +276,7 @@ version = "0.0.1" ...@@ -276,6 +276,7 @@ version = "0.0.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"derive_more",
"log", "log",
"thiserror", "thiserror",
] ]
......
...@@ -8,8 +8,9 @@ edition.workspace = true ...@@ -8,8 +8,9 @@ edition.workspace = true
authors.workspace = true authors.workspace = true
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
thiserror.workspace = true thiserror.workspace = true
log.workspace = true log.workspace = true
clap.workspace = true derive_more.workspace = true
clap.workspace = true
/// The struct to #[command(flatten)] inside the cli's arguments.
#[derive(clap::Args, Debug, Clone, Copy, Default)]
#[command(about = None, long_about = None)]
pub struct ColorChoice {
#[arg( long
, global = true
, default_value_t = clap::ColorChoice::Auto
, value_name = "WHEN"
, value_enum
)]
/// Chose whever to display using colors or not
color: clap::ColorChoice,
}
impl ColorChoice {
pub fn into_inner(self) -> clap::ColorChoice {
self.into()
}
}
impl From<ColorChoice> for clap::ColorChoice {
fn from(value: ColorChoice) -> Self {
value.color
}
}
pub mod color_choice;
pub mod verbose;
#[derive(clap::Parser, Debug)]
pub struct Args {
#[command(flatten)]
pub verbosity: verbose::VerboseChoice,
#[command(flatten)]
pub color_choice: color_choice::ColorChoice,
}
/// The struct to #[command(flatten)] inside the cli's arguments.
#[derive(clap::Args, Debug, Clone, Copy, Default)]
#[command( about = None
, long_about = None
, groups = [clap::ArgGroup::new("verbosity").args(["verbose", "quiet"])]
)]
pub struct VerboseChoice {
#[arg( long
, short = 'v'
, action = clap::ArgAction::Count
, global = true
)]
/// Verbosity level, supply multiple times to increase log level
verbose: u8,
#[arg( long
, short = 'q'
, action = clap::ArgAction::SetTrue
, global = true
)]
/// Use to silence any log, only errors will be shown
quiet: bool,
}
impl From<VerboseChoice> for log::Level {
fn from(value: VerboseChoice) -> Self {
match value {
VerboseChoice { quiet: true, .. } => log::Level::Error,
VerboseChoice { verbose: 0, .. } => log::Level::Error,
VerboseChoice { verbose: 1, .. } => log::Level::Warn,
VerboseChoice { verbose: 2, .. } => log::Level::Info,
VerboseChoice { verbose: 3, .. } => log::Level::Debug,
VerboseChoice { .. } => log::Level::Trace,
}
}
}
impl From<VerboseChoice> for log::LevelFilter {
fn from(value: VerboseChoice) -> Self {
Into::<log::Level>::into(value).to_level_filter()
}
}
use anyhow::Context as _;
use clap::Parser as _;
fn main() {} mod args;
fn main() -> anyhow::Result<()> {
let args = args::Args::try_parse().context("failed to parse arguments")?;
println!("{args:?}");
let _: log::Level = args.verbosity.into();
let _ = args.color_choice.into_inner();
Ok(())
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter