mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-24 04:46:58 +00:00
Add --config command line option (#3767)
This commit is contained in:
parent
15a28f79b8
commit
e81ec20af0
@ -105,13 +105,18 @@ fn impl_from_str(ident: &syn::Ident, variants: &Variants) -> TokenStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
let mut err_msg = String::from("Bad variant, expected one of:");
|
||||||
|
for v in variants.iter().filter(|v| is_unit(v)) {
|
||||||
|
err_msg.push_str(&format!(" `{}`", v.ident));
|
||||||
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl ::std::str::FromStr for #ident {
|
impl ::std::str::FromStr for #ident {
|
||||||
type Err = &'static str;
|
type Err = &'static str;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
#if_patterns
|
#if_patterns
|
||||||
return Err("Bad variant");
|
return Err(#err_msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use io::Error as IoError;
|
|||||||
|
|
||||||
use rustfmt_nightly as rustfmt;
|
use rustfmt_nightly as rustfmt;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, stdout, Read, Write};
|
use std::io::{self, stdout, Read, Write};
|
||||||
@ -132,6 +133,12 @@ fn make_opts() -> Options {
|
|||||||
"Prints the names of mismatched files that were formatted. Prints the names of \
|
"Prints the names of mismatched files that were formatted. Prints the names of \
|
||||||
files that would be formated when used with `--check` mode. ",
|
files that would be formated when used with `--check` mode. ",
|
||||||
);
|
);
|
||||||
|
opts.optmulti(
|
||||||
|
"",
|
||||||
|
"config",
|
||||||
|
"Set options from command line. These settings take priority over .rustfmt.toml",
|
||||||
|
"[key1=val1,key2=val2...]",
|
||||||
|
);
|
||||||
|
|
||||||
if is_nightly {
|
if is_nightly {
|
||||||
opts.optflag(
|
opts.optflag(
|
||||||
@ -478,6 +485,7 @@ struct GetOptsOptions {
|
|||||||
quiet: bool,
|
quiet: bool,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
config_path: Option<PathBuf>,
|
config_path: Option<PathBuf>,
|
||||||
|
inline_config: HashMap<String, String>,
|
||||||
emit_mode: EmitMode,
|
emit_mode: EmitMode,
|
||||||
backup: bool,
|
backup: bool,
|
||||||
check: bool,
|
check: bool,
|
||||||
@ -537,6 +545,29 @@ impl GetOptsOptions {
|
|||||||
|
|
||||||
options.config_path = matches.opt_str("config-path").map(PathBuf::from);
|
options.config_path = matches.opt_str("config-path").map(PathBuf::from);
|
||||||
|
|
||||||
|
options.inline_config = matches
|
||||||
|
.opt_strs("config")
|
||||||
|
.iter()
|
||||||
|
.flat_map(|config| config.split(","))
|
||||||
|
.map(
|
||||||
|
|key_val| match key_val.char_indices().find(|(_, ch)| *ch == '=') {
|
||||||
|
Some((middle, _)) => {
|
||||||
|
let (key, val) = (&key_val[..middle], &key_val[middle + 1..]);
|
||||||
|
if !Config::is_valid_key_val(key, val) {
|
||||||
|
Err(format_err!("invalid key=val pair: `{}`", key_val))
|
||||||
|
} else {
|
||||||
|
Ok((key.to_string(), val.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None => Err(format_err!(
|
||||||
|
"--config expects comma-separated list of key=val pairs, found `{}`",
|
||||||
|
key_val
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.collect::<Result<HashMap<_, _>, _>>()?;
|
||||||
|
|
||||||
options.check = matches.opt_present("check");
|
options.check = matches.opt_present("check");
|
||||||
if let Some(ref emit_str) = matches.opt_str("emit") {
|
if let Some(ref emit_str) = matches.opt_str("emit") {
|
||||||
if options.check {
|
if options.check {
|
||||||
@ -624,6 +655,10 @@ impl CliOptions for GetOptsOptions {
|
|||||||
if self.print_misformatted_file_names {
|
if self.print_misformatted_file_names {
|
||||||
config.set().print_misformatted_file_names(true);
|
config.set().print_misformatted_file_names(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (key, val) in self.inline_config {
|
||||||
|
config.override_value(&key, &val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn config_path(&self) -> Option<&Path> {
|
fn config_path(&self) -> Option<&Path> {
|
||||||
|
@ -178,6 +178,16 @@ macro_rules! create_config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unreachable_pub)]
|
||||||
|
pub fn is_valid_key_val(key: &str, val: &str) -> bool {
|
||||||
|
match key {
|
||||||
|
$(
|
||||||
|
stringify!($i) => val.parse::<$ty>().is_ok(),
|
||||||
|
)+
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unreachable_pub)]
|
#[allow(unreachable_pub)]
|
||||||
pub fn used_options(&self) -> PartialConfig {
|
pub fn used_options(&self) -> PartialConfig {
|
||||||
PartialConfig {
|
PartialConfig {
|
||||||
|
@ -30,17 +30,16 @@ fn rustfmt(args: &[&str]) -> (String, String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! assert_that {
|
macro_rules! assert_that {
|
||||||
($args:expr, $check:ident $check_args:tt) => {
|
($args:expr, $($check:ident $check_args:tt)&&+) => {
|
||||||
let (stdout, stderr) = rustfmt($args);
|
let (stdout, stderr) = rustfmt($args);
|
||||||
if !stdout.$check$check_args && !stderr.$check$check_args {
|
if $(!stdout.$check$check_args && !stderr.$check$check_args)||* {
|
||||||
panic!(
|
panic!(
|
||||||
"Output not expected for rustfmt {:?}\n\
|
"Output not expected for rustfmt {:?}\n\
|
||||||
expected: {}{}\n\
|
expected: {}\n\
|
||||||
actual stdout:\n{}\n\
|
actual stdout:\n{}\n\
|
||||||
actual stderr:\n{}",
|
actual stderr:\n{}",
|
||||||
$args,
|
$args,
|
||||||
stringify!($check),
|
stringify!($( $check$check_args )&&*),
|
||||||
stringify!($check_args),
|
|
||||||
stdout,
|
stdout,
|
||||||
stderr
|
stderr
|
||||||
);
|
);
|
||||||
@ -76,3 +75,28 @@ fn print_config() {
|
|||||||
);
|
);
|
||||||
remove_file("minimal-config").unwrap();
|
remove_file("minimal-config").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[ignore]
|
||||||
|
#[test]
|
||||||
|
fn inline_config() {
|
||||||
|
// single invocation
|
||||||
|
assert_that!(
|
||||||
|
&[
|
||||||
|
"--print-config",
|
||||||
|
"current",
|
||||||
|
".",
|
||||||
|
"--config=color=Never,edition=2018"
|
||||||
|
],
|
||||||
|
contains("color = \"Never\"") && contains("edition = \"2018\"")
|
||||||
|
);
|
||||||
|
|
||||||
|
// multiple overriding invocations
|
||||||
|
assert_that!(
|
||||||
|
&["--print-config", "current", ".",
|
||||||
|
"--config", "color=never,edition=2018",
|
||||||
|
"--config", "color=always,format_strings=true"],
|
||||||
|
contains("color = \"Always\"") &&
|
||||||
|
contains("edition = \"2018\"") &&
|
||||||
|
contains("format_strings = true")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user