]> git.lizzy.rs Git - rust.git/blobdiff - src/bin/cargo-fmt.rs
Merge pull request #710 from JanLikar/master
[rust.git] / src / bin / cargo-fmt.rs
index af6c6b059a05fad25e727fd2624b7ab7a77ef23b..58d5e4d8d0f32a1e9a7dac25b4e45e6e9a855941 100644 (file)
@@ -28,7 +28,7 @@ fn main() {
     let mut opts = getopts::Options::new();
     opts.optflag("h", "help", "show this message");
 
-    let matches = match opts.parse(env::args().skip(1)) {
+    let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
         Ok(m) => m,
         Err(e) => {
             print_usage(&opts, &e.to_string());
@@ -45,7 +45,8 @@ fn main() {
 
 fn print_usage(opts: &Options, reason: &str) {
     let msg = format!("{}\nusage: cargo fmt [options]", reason);
-    println!("{}\nThis utility formats all bin and lib files of the current crate using rustfmt.",
+    println!("{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \
+              Arguments after `--` are passed to rustfmt.",
              opts.usage(&msg));
 }
 
@@ -64,7 +65,12 @@ fn format_crate(opts: &Options) {
                                .map(|t| t.path)
                                .collect();
 
-    format_files(&files).unwrap_or_else(|e| print_usage(opts, &e.to_string()));
+    format_files(&files, &get_fmt_args()).unwrap_or_else(|e| print_usage(opts, &e.to_string()));
+}
+
+fn get_fmt_args() -> Vec<String> {
+    // All arguments after -- are passed to rustfmt
+    env::args().skip_while(|a| a != "--").skip(1).collect()
 }
 
 #[derive(Debug)]
@@ -133,10 +139,10 @@ fn target_from_json(jtarget: &Json) -> Target {
     }
 }
 
-fn format_files(files: &Vec<PathBuf>) -> Result<(), std::io::Error> {
+fn format_files(files: &Vec<PathBuf>, fmt_args: &Vec<String>) -> Result<(), std::io::Error> {
     let mut command = try!(Command::new("rustfmt")
-                               .arg("--write-mode=overwrite")
                                .args(files)
+                               .args(fmt_args)
                                .spawn());
     try!(command.wait());