From 4a41aa1d866ec4f7d7033affc1fb2521b64c1d6b Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 23 Jun 2025 19:22:32 -0400 Subject: [PATCH] Support ClI argument for Version and Help --- Cargo.lock | 7 +++++ Cargo.toml | 2 ++ src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab74d26..eb125a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -973,6 +973,12 @@ dependencies = [ "inout", ] +[[package]] +name = "clap_lex" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" + [[package]] name = "clipboard-win" version = "5.4.0" @@ -1497,6 +1503,7 @@ name = "cosmic-term" version = "0.1.0" dependencies = [ "alacritty_terminal", + "clap_lex", "cosmic-files", "cosmic-text", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 8779d84..78317c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,8 @@ ron = "0.8" serde = { version = "1", features = ["serde_derive"] } shlex = "1" tokio = { version = "1", features = ["sync"] } +# CLI arguments +clap_lex = "0.7" # Internationalization i18n-embed = { version = "0.15", features = [ "fluent-system", diff --git a/src/main.rs b/src/main.rs index 3a4cede..5a9f997 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,6 +69,10 @@ mod terminal_theme; mod dnd; +use clap_lex::RawArgs; + +use std::error::Error; + lazy_static::lazy_static! { static ref ICON_CACHE: Mutex = Mutex::new(IconCache::new()); } @@ -79,21 +83,39 @@ pub fn icon_cache_get(name: &'static str, size: u16) -> widget::icon::Icon { } /// Runs application with these settings -fn main() -> Result<(), Box> { - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init(); +#[rustfmt::skip] +fn main() -> Result<(), Box> { + let raw_args = RawArgs::from_args(); + let mut cursor = raw_args.cursor(); + let mut shell_program_opt = None; + let mut shell_args = Vec::new(); let mut daemonize = true; - let mut args_iter = env::args().fuse(); - // more performant than an iterator adapter - _ = args_iter.next(); - for arg in args_iter.by_ref() { - match arg.as_str() { - // These flags indicate the end of parsing flags - "-e" | "--command" | "--" => { + // Parse the arguments using clap_lex + while let Some(arg) = raw_args.next_os(&mut cursor) { + match arg.to_str() { + Some("--help") | Some("-h") => { + print_help(); + return Ok(()); + } + Some("--version") | Some("-V") => { + println!( + "cosmic-term {} (git commit {})", + env!("CARGO_PKG_VERSION"), + env!("VERGEN_GIT_SHA") + ); + return Ok(()); + } + Some("--no-daemon") => { + daemonize = false; + } + Some("-e") | Some("--command") => { + // Handle the '--command' or '-e' flag break; } - "--no-daemon" => { - daemonize = false; + Some("--") => { + // End of flags, the next args are shell-related + break; } _ => { //TODO: should this throw an error? @@ -101,8 +123,16 @@ fn main() -> Result<(), Box> { } } } - let shell_program_opt = args_iter.next(); - let shell_args = Vec::from_iter(args_iter); + // After flags, process remaining shell program and args + while let Some(arg) = raw_args.next_os(&mut cursor) { + if let Some(program) = shell_program_opt.take() { + shell_args.push(arg.to_string_lossy().to_string()); + } else { + shell_program_opt = Some(arg.to_string_lossy().to_string()); + } + } + + // Platform-specific daemonization logic #[cfg(all(unix, not(target_os = "redox")))] if daemonize { @@ -145,27 +175,44 @@ fn main() -> Result<(), Box> { None }; + // Terminal config setup let term_config = term::Config::default(); // Set up environmental variables for terminal tty::setup_env(); // Override TERM for better compatibility env::set_var("TERM", "xterm-256color"); + // Set settings let mut settings = Settings::default(); settings = settings.theme(config.app_theme.theme()); settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0)); + // Flags let flags = Flags { config_handler, config, startup_options, term_config, }; + + // Run the cosmic app cosmic::app::run::(settings, flags)?; Ok(()) } +fn print_help() { + println!( + r#"COSMIC Terminal +Designed for the COSMICâ„¢ desktop environment, cosmic-term is a libcosmic-based terminal emulator. + +Project home page: https://github.com/pop-os/cosmic-term +Options: + --help Show this message + --version Show the version of cosmic-term"# + ); +} + #[derive(Clone, Debug)] pub struct Flags { config_handler: Option,