From 39538a0c772694930185178eb6aa9bf83cc47c60 Mon Sep 17 00:00:00 2001 From: Alex HotShot Newman Date: Wed, 26 Aug 2015 15:02:06 -0700 Subject: [PATCH] Fix usage of the tool. Provide -h and --help --- src/bin/rustfmt.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 2f61e788ed2..eda3eba107c 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -67,22 +67,40 @@ fn main() { std::process::exit(0); } +fn usage>(reason: S) { + print!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[true/false]] ", reason.into()); + std::process::exit(1); +} + fn determine_params(args: I) -> (Vec, WriteMode) where I: Iterator { - let prefix = "--write-mode="; + let arg_prefix = "-"; + let write_mode_prefix = "--write-mode="; + let help_mode = "-h"; + let long_help_mode = "--help"; let mut write_mode = WriteMode::Replace; // The NewFile option currently isn't supported because it requires another // parameter, but it can be added later. - let args = args.filter(|arg| { - if arg.starts_with(prefix) { - write_mode = FromStr::from_str(&arg[prefix.len()..]).expect("Unrecognized write mode"); + let args:Vec = args.filter(|arg| { + if arg.starts_with(write_mode_prefix) { + write_mode = FromStr::from_str(&arg[write_mode_prefix.len()..]).expect("Unrecognized write mode"); + false + } else if arg.starts_with(help_mode) || arg.starts_with(long_help_mode) { + usage(""); + false + } else if arg.starts_with(arg_prefix) { + usage("Invalid argument"); false } else { true } }).collect(); + if args.len() < 2 { + usage("Please provide a file to be formatted"); + } + (args, write_mode) }