about summary refs log tree commit diff
path: root/src/bin
diff options
context:
space:
mode:
authorAlex HotShot Newman <posix4e@gmail.com>2015-08-26 15:02:06 -0700
committerAlex HotShot Newman <posix4e@gmail.com>2015-08-26 15:07:01 -0700
commit39538a0c772694930185178eb6aa9bf83cc47c60 (patch)
treedac827ffd9ff6fbaf924c8579f1823243549b7be /src/bin
parent0957f3101ed77dd3b5f4c23ffaa0ea83fe7b3e91 (diff)
Fix usage of the tool. Provide -h and --help
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/rustfmt.rs26
1 files 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<S: Into<String>>(reason: S) {
+    print!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[true/false]] <file_name>", reason.into());
+    std::process::exit(1);
+}
+
 fn determine_params<I>(args: I) -> (Vec<String>, WriteMode)
     where I: Iterator<Item = String>
 {
-    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<String> = 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)
 }