From a099e2289e53aaad1d99aade06535532bf6a00ae Mon Sep 17 00:00:00 2001 From: jannikac Date: Thu, 28 Nov 2024 20:35:04 +0100 Subject: [PATCH 1/2] add clap as cli argument parser --- Cargo.lock | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 14 +++---- 3 files changed, 123 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64dabcb..b2ef927 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -11,6 +11,55 @@ dependencies = [ "memchr 2.7.4", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + [[package]] name = "anyhow" version = "1.0.93" @@ -100,6 +149,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "config" version = "0.14.1" @@ -351,6 +446,7 @@ name = "display_switch" version = "1.4.0" dependencies = [ "anyhow", + "clap", "config", "ddc", "ddc-hi", @@ -489,6 +585,12 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "i2c" version = "0.1.0" @@ -692,6 +794,12 @@ dependencies = [ "libc", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.11" @@ -1350,6 +1458,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index ddc4759..3f57213 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ ddc = "0.2" ddc-hi = "0.4" rusb = "^0.9" shell-words = "1.0" +clap = { version = "4.5.21", features = ["derive"] } [build-dependencies] vergen-git2 = { version = "1.0.0", features = ["build", "cargo"] } diff --git a/src/main.rs b/src/main.rs index b85515a..1eb3bdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ extern crate log; use anyhow::Result; -use std::env; +use clap::Parser; #[cfg(target_os = "windows")] use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS}; @@ -21,6 +21,11 @@ mod logging; mod platform; mod usb; +#[derive(Parser, Debug)] +#[command(version)] +struct Args { +} + /// On Windows, re-attach the console, if parent process has the console. This allows /// to see the log output when run from the command line. fn attach_console() { @@ -32,12 +37,7 @@ fn attach_console() { fn main() -> Result<()> { attach_console(); - - let args: Vec = env::args().collect(); - if args.len() == 2 && args[1] == "--version" { - println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); - return Ok(()); - } + let args = Args::parse(); let app = app::App::new()?; app.run()?; From 1e35be21cfa8dbe8b0abb75b30f1f7aa12d00e4e Mon Sep 17 00:00:00 2001 From: jannikac Date: Thu, 28 Nov 2024 20:35:48 +0100 Subject: [PATCH 2/2] add an option to change the log level --- src/app.rs | 6 +++--- src/logging.rs | 17 +++++++++-------- src/main.rs | 5 ++++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6dd7c07..d19c803 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,10 +6,10 @@ use anyhow::{Context, Result}; use crate::configuration::{Configuration, SwitchDirection}; -use crate::display_control; use crate::logging; use crate::platform::{wake_displays, PnPDetect}; use crate::usb; +use crate::{display_control, Args}; pub struct App { config: Configuration, @@ -38,8 +38,8 @@ impl usb::UsbCallback for App { } impl App { - pub fn new() -> Result { - logging::init_logging().context("failed to initialize logging")?; + pub fn new(args: Args) -> Result { + logging::init_logging(args.debug).context("failed to initialize logging")?; info!( "display-switch v{version} built on {timestamp} from git {git}", version = env!("CARGO_PKG_VERSION"), diff --git a/src/logging.rs b/src/logging.rs index 9b0df0a..3b2aeb8 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -10,16 +10,17 @@ use simplelog::*; use crate::configuration::Configuration; -pub fn init_logging() -> Result<()> { +pub fn init_logging(log_debug: bool) -> Result<()> { + let log_level = if log_debug { + LevelFilter::Debug + } else { + LevelFilter::Info + }; + Ok(CombinedLogger::init(vec![ - TermLogger::new( - LevelFilter::Debug, - Config::default(), - TerminalMode::Mixed, - ColorChoice::Auto, - ), + TermLogger::new(log_level, Config::default(), TerminalMode::Mixed, ColorChoice::Auto), WriteLogger::new( - LevelFilter::Debug, + log_level, Config::default(), File::create(Configuration::log_file_name()?)?, ), diff --git a/src/main.rs b/src/main.rs index 1eb3bdf..1c72010 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,9 @@ mod usb; #[derive(Parser, Debug)] #[command(version)] struct Args { + /// Print debug information + #[arg(short, long, default_value_t = false)] + debug: bool, } /// On Windows, re-attach the console, if parent process has the console. This allows @@ -39,7 +42,7 @@ fn main() -> Result<()> { attach_console(); let args = Args::parse(); - let app = app::App::new()?; + let app = app::App::new(args)?; app.run()?; Ok(()) }