From c6afa063ef8f877b28494f3c41de73afc99e1826 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 29 Jun 2024 17:05:50 +0200 Subject: [PATCH] docs(cli): example for a simple cli --- members/libpt-cli/examples/cli.rs | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 members/libpt-cli/examples/cli.rs diff --git a/members/libpt-cli/examples/cli.rs b/members/libpt-cli/examples/cli.rs new file mode 100644 index 0000000..91fdcad --- /dev/null +++ b/members/libpt-cli/examples/cli.rs @@ -0,0 +1,41 @@ +use clap::Parser; +use libpt_cli::args::VerbosityLevel; +use libpt_cli::{clap, printing}; +use libpt_log::{debug, Logger}; + +/// This is the help +/// +/// This is more help +#[derive(Parser, Debug)] +struct Cli { + // already has documentation + #[command(flatten)] + verbosity: VerbosityLevel, + + /// texts to be echoed + #[arg(required = true)] + text: Vec, + + /// try to be more machine readable + #[arg(short, long)] + machine: bool, +} + +fn main() { + let cli = Cli::parse(); + let _logger = Logger::builder() + .max_level(cli.verbosity.level()) + .show_time(false) + .build(); + + debug!("logger initialized with level: {}", cli.verbosity.level()); + + if !cli.machine { + let text = cli.text.join(" "); + printing::blockprint(text, console::Color::Green); + } else { + for text in cli.text { + println!("{text}") + } + } +}