]> git.lizzy.rs Git - rust.git/blobdiff - src/bin/rustfmt.rs
Fix a typo
[rust.git] / src / bin / rustfmt.rs
index c7461069db42e7c65b740d50900899350b0b6a2e..75b45156acec6d6e7a5f8aaa937fbbd868b13839 100644 (file)
@@ -10,7 +10,6 @@
 
 #![cfg(not(test))]
 
-
 extern crate env_logger;
 extern crate getopts;
 extern crate rustfmt_nightly as rustfmt;
 use std::path::{Path, PathBuf};
 use std::str::FromStr;
 
-use getopts::{HasArg, Matches, Occur, Options};
+use getopts::{Matches, Options};
 
 use rustfmt::{run, Input, Summary};
 use rustfmt::file_lines::FileLines;
-use rustfmt::config::{get_toml_path, Config, WriteMode};
+use rustfmt::config::{get_toml_path, Color, Config, WriteMode};
 
 type FmtError = Box<error::Error + Send + Sync>;
 type FmtResult<T> = std::result::Result<T, FmtError>;
@@ -45,7 +44,9 @@ enum Operation {
     /// Print detailed configuration help.
     ConfigHelp,
     /// Output default config to a file, or stdout if None
-    ConfigOutputDefault { path: Option<String> },
+    ConfigOutputDefault {
+        path: Option<String>,
+    },
     /// No file specified, read from stdin
     Stdin {
         input: String,
@@ -59,6 +60,7 @@ struct CliOptions {
     skip_children: bool,
     verbose: bool,
     write_mode: Option<WriteMode>,
+    color: Option<Color>,
     file_lines: FileLines, // Default is all lines in all files.
     unstable_features: bool,
 }
@@ -68,14 +70,14 @@ fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
         let mut options = CliOptions::default();
         options.skip_children = matches.opt_present("skip-children");
         options.verbose = matches.opt_present("verbose");
-        let unstable_features = matches.opt_present("unstable_features");
+        let unstable_features = matches.opt_present("unstable-features");
         let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
             .map(|c| c == "nightly")
             .unwrap_or(false);
         if unstable_features && !rust_nightly {
-            return Err(FmtError::from(format!(
-                "Unstable features are only available on Nightly channel"
-            )));
+            return Err(FmtError::from(
+                "Unstable features are only available on Nightly channel",
+            ));
         } else {
             options.unstable_features = unstable_features;
         }
@@ -84,9 +86,17 @@ fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
             if let Ok(write_mode) = WriteMode::from_str(write_mode) {
                 options.write_mode = Some(write_mode);
             } else {
-                return Err(FmtError::from(
-                    format!("Invalid write-mode: {}", write_mode),
-                ));
+                return Err(FmtError::from(format!(
+                    "Invalid write-mode: {}",
+                    write_mode
+                )));
+            }
+        }
+
+        if let Some(ref color) = matches.opt_str("color") {
+            match Color::from_str(color) {
+                Ok(color) => options.color = Some(color),
+                _ => return Err(FmtError::from(format!("Invalid color: {}", color))),
             }
         }
 
@@ -105,6 +115,9 @@ fn apply_to(self, config: &mut Config) {
         if let Some(write_mode) = self.write_mode {
             config.set().write_mode(write_mode);
         }
+        if let Some(color) = self.color {
+            config.set().color(color);
+        }
     }
 }
 
@@ -122,35 +135,45 @@ fn match_cli_path_or_file(
 
 fn make_opts() -> Options {
     let mut opts = Options::new();
-    opts.optflag("h", "help", "show this message");
-    opts.optflag("V", "version", "show version information");
-    opts.optflag("v", "verbose", "print verbose output");
-    opts.optopt(
-        "",
-        "write-mode",
-        "how to write output (not usable when piping from stdin)",
-        "[replace|overwrite|display|plain|diff|coverage|checkstyle]",
-    );
-    opts.optflag("", "skip-children", "don't reformat child modules");
 
+    opts.optflag("h", "help", "Show this message");
+    opts.optflag("V", "version", "Show version information");
+    opts.optflag("v", "verbose", "Print verbose output");
+    opts.optflag("", "skip-children", "Don't reformat child modules");
     opts.optflag(
         "",
         "unstable-features",
         "Enables unstable features. Only available on nightly channel",
     );
-
     opts.optflag(
         "",
         "config-help",
-        "show details of rustfmt configuration options",
+        "Show details of rustfmt configuration options",
     );
-    opts.opt(
+    opts.optflag(
+        "",
+        "error-on-unformatted",
+        "Error if unable to get comments or string literals within max_width, \
+         or they are left with trailing whitespaces",
+    );
+
+    opts.optopt(
+        "",
+        "write-mode",
+        "How to write output (not usable when piping from stdin)",
+        "[replace|overwrite|display|plain|diff|coverage|checkstyle]",
+    );
+    opts.optopt(
+        "",
+        "color",
+        "Use colored output (if supported)",
+        "[always|never|auto]",
+    );
+    opts.optopt(
         "",
         "dump-default-config",
         "Dumps default configuration to PATH. PATH defaults to stdout, if omitted.",
         "PATH",
-        HasArg::Maybe,
-        Occur::Optional,
     );
     opts.optopt(
         "",