From 13b24a39bf855584541892b2406562c2dc8fcc2b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 20 Aug 2022 19:43:35 +0200 Subject: Add version command line argument * -V and --version now print the program version and exit * Use the `exitcode` crate * Rename `options` variable to `accepted_args`, which seems clearer given its usage. --- src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index fbbd252..38a6d3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ // file for more information. extern crate clipboard; +extern crate exitcode; extern crate rustty; use clipboard::ClipboardContext; @@ -114,7 +115,17 @@ fn main() { // TODO: Add --version -V arg // TODO: When done, update man page - let options = ["-i"]; + let accepted_args = ["-i", "-V", "--version"]; + + match args.get(1) { + Some(arg) => { + if arg == "-V" || arg == "--version" { + println!("{}", env!("CARGO_PKG_VERSION")); + process::exit(exitcode::OK); + } + } + None => (), + } let hide_password = match args.get(1) { Some(arg) if arg == "-i" => true, @@ -124,7 +135,7 @@ fn main() { let last_arg = &args[args.len() - 1]; let input = if args.len() > 1 && - !options.contains(&last_arg.as_ref()) { + !accepted_args.contains(&last_arg.as_ref()) { last_arg } else { "-" -- cgit v1.2.3