Skip to content

Add --version and -- help support with clap_lex #900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ xdg-mime = { git = "https://github.com/ellieplayswow/xdg-mime-rs", branch = "fea
bzip2 = { version = "0.5", optional = true } #TODO: replace with pure Rust crate
flate2 = "1.0"
tar = "0.4.43"
xz2 = { version = "0.1", optional = true } #TODO: replace with pure Rust crate
# CLI arguments
clap_lex = "0.7"
# Internationalization
i18n-embed = { version = "0.15", features = [
"fluent-system",
Expand Down
42 changes: 41 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,46 @@ use tikv_jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

fn main() -> Result<(), Box<dyn std::error::Error>> {
use clap_lex::RawArgs;

use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let raw_args = RawArgs::from_args();
let mut cursor = raw_args.cursor();

// Parse the arguments
while let Some(arg) = raw_args.next_os(&mut cursor) {
match arg.to_str() {
Some("--help") | Some("-h") => {
print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA"));
return Ok(());
}
Some("--version") | Some("-V") => {
println!(
"cosmic-files {} (git commit {})",
env!("CARGO_PKG_VERSION"),
env!("VERGEN_GIT_SHA")
);
return Ok(());
}
_ => {}
}
}
cosmic_files::main()
}

fn print_help(version: &str, git_rev: &str) {
println!(
r#"cosmic-files {version} (git commit {git_rev})
System76 <[email protected]>

Designed for the COSMIC™ desktop environment, cosmic-files is a libcosmic-based file manager.

Project home page: https://github.com/pop-os/cosmic-files

Options:
-h, --help Show this message
-v, --version Show the version of cosmic-files"#
);
}