From 2a1bf37e6983451e49360711a342e1ec16db8088 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 1 Aug 2019 12:44:30 +0300 Subject: [PATCH] Add --version command line flag --- meli.1 | 3 +++ src/bin.rs | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/meli.1 b/meli.1 index 1dec48ad4..5047da7fa 100644 --- a/meli.1 +++ b/meli.1 @@ -26,6 +26,7 @@ .Sh SYNOPSIS .Nm meli .Op Fl -help | h +.Op Fl -version | v .Op Fl -create-config Op Ar path .Op Fl -config Ar path .Sh DESCRIPTION @@ -33,6 +34,8 @@ Experimental terminal mail client .Bl -tag -width flag -offset indent .It Fl -help, h Show help message and exit. +.It Fl -version, v +Show version and exit. .It Fl -create-config Op Ar path Create configuration file in .Pa path diff --git a/src/bin.rs b/src/bin.rs index ebbe5a758..4f726df22 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -47,7 +47,7 @@ use xdg; macro_rules! error_and_exit { ($($err:expr),*) => {{ - println!($($err),*); + eprintln!($($err),*); std::process::exit(1); }} } @@ -57,6 +57,7 @@ struct CommandLineArguments { create_config: Option, config: Option, help: bool, + version: bool, } fn main() { @@ -70,6 +71,7 @@ fn main() { create_config: None, config: None, help: false, + version: false, }; for i in std::env::args().skip(1) { @@ -88,10 +90,12 @@ fn main() { Some(CreateConfig) => error_and_exit!("Duplicate value for flag `--create-config`"), Some(Config) => error_and_exit!("invalid value for flag `--config`"), }, - "--help" | "-h" => match prev { - Some(_) => {} - None => args.help = true, - }, + "--help" | "-h" => { + args.help = true; + } + "--version" | "-v" => { + args.version = true; + } e => match prev { None => error_and_exit!("error: value without command {}", e), Some(CreateConfig) if args.create_config.is_none() => { @@ -111,13 +115,20 @@ fn main() { if args.help { println!("usage:\tmeli [--create-config[ PATH]] [--config[ PATH]|-c[ PATH]]"); println!("\tmeli --help"); + println!("\tmeli --version"); println!(""); - println!("\t--help\t\t\tshow this message and exit"); + println!("\t--help, -h\t\tshow this message and exit"); + println!("\t--version, -v\t\tprint version and exit"); println!("\t--create-config[ PATH]\tCreate a sample configuration file with available configuration options. If PATH is not specified, meli will try to create it in $XDG_CONFIG_HOME/meli/config"); println!("\t--config PATH, -c PATH\tUse specified configuration file"); std::process::exit(0); } + if args.version { + println!("meli {}", option_env!("CARGO_PKG_VERSION").unwrap_or("0.0")); + std::process::exit(0); + } + match prev { None => {} Some(CreateConfig) if args.create_config.is_none() => args.create_config = Some("".into()),