Merge pull request #615 from JanLikar/version

Add --version switch
This commit is contained in:
Nick Cameron 2015-12-07 07:32:16 +13:00
commit 26297c56df

View file

@ -33,6 +33,8 @@ enum Operation {
Format(Vec<PathBuf>, WriteMode), Format(Vec<PathBuf>, WriteMode),
/// Print the help message. /// Print the help message.
Help, Help,
// Print version information
Version,
/// Print detailed configuration help. /// Print detailed configuration help.
ConfigHelp, ConfigHelp,
/// Invalid program input, including reason. /// Invalid program input, including reason.
@ -82,6 +84,7 @@ fn update_config(config: &mut Config, matches: &Matches) {
fn execute() -> i32 { fn execute() -> i32 {
let mut opts = Options::new(); let mut opts = Options::new();
opts.optflag("h", "help", "show this message"); opts.optflag("h", "help", "show this message");
opts.optflag("V", "version", "show version information");
opts.optflag("v", "verbose", "show progress"); opts.optflag("v", "verbose", "show progress");
opts.optopt("", opts.optopt("",
"write-mode", "write-mode",
@ -111,6 +114,10 @@ fn execute() -> i32 {
print_usage(&opts, ""); print_usage(&opts, "");
0 0
} }
Operation::Version => {
print_version();
0
}
Operation::ConfigHelp => { Operation::ConfigHelp => {
Config::print_docs(); Config::print_docs();
0 0
@ -166,6 +173,14 @@ fn print_usage(opts: &Options, reason: &str) {
println!("{}", opts.usage(&reason)); println!("{}", opts.usage(&reason));
} }
fn print_version() {
println!("{}.{}.{}{}",
option_env!("CARGO_PKG_VERSION_MAJOR").unwrap_or("X"),
option_env!("CARGO_PKG_VERSION_MINOR").unwrap_or("X"),
option_env!("CARGO_PKG_VERSION_PATCH").unwrap_or("X"),
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
}
fn determine_operation(matches: &Matches) -> Operation { fn determine_operation(matches: &Matches) -> Operation {
if matches.opt_present("h") { if matches.opt_present("h") {
return Operation::Help; return Operation::Help;
@ -175,6 +190,10 @@ fn determine_operation(matches: &Matches) -> Operation {
return Operation::ConfigHelp; return Operation::ConfigHelp;
} }
if matches.opt_present("version") {
return Operation::Version;
}
// if no file argument is supplied, read from stdin // if no file argument is supplied, read from stdin
if matches.free.is_empty() { if matches.free.is_empty() {