Skip to content

Commit 23be63f

Browse files
authored
Merge pull request #2047 from japaric/dump-default-config-2
output --dump-default-config to stdout if no path is given
2 parents fc15e9f + a1cfacd commit 23be63f

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/bin/rustfmt.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::io::{self, Read, Write};
2121
use std::path::{Path, PathBuf};
2222
use std::str::FromStr;
2323

24-
use getopts::{Matches, Options};
24+
use getopts::{HasArg, Matches, Occur, Options};
2525

2626
use rustfmt::{run, Input, Summary};
2727
use rustfmt::file_lines::FileLines;
@@ -44,8 +44,8 @@ enum Operation {
4444
Version,
4545
/// Print detailed configuration help.
4646
ConfigHelp,
47-
/// Output default config to a file
48-
ConfigOutputDefault { path: String },
47+
/// Output default config to a file, or stdout if None
48+
ConfigOutputDefault { path: Option<String> },
4949
/// No file specified, read from stdin
5050
Stdin {
5151
input: String,
@@ -125,11 +125,14 @@ fn make_opts() -> Options {
125125
"config-help",
126126
"show details of rustfmt configuration options",
127127
);
128-
opts.optopt(
128+
opts.opt(
129129
"",
130130
"dump-default-config",
131-
"Dumps the default configuration to a file and exits.",
131+
"Dumps the default configuration to a file and exits. PATH defaults to rustfmt.toml if \
132+
omitted.",
132133
"PATH",
134+
HasArg::Maybe,
135+
Occur::Optional,
133136
);
134137
opts.optopt(
135138
"",
@@ -172,9 +175,13 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
172175
Ok(Summary::default())
173176
}
174177
Operation::ConfigOutputDefault { path } => {
175-
let mut file = File::create(path)?;
176178
let toml = Config::default().all_options().to_toml()?;
177-
file.write_all(toml.as_bytes())?;
179+
if let Some(path) = path {
180+
let mut file = File::create(path)?;
181+
file.write_all(toml.as_bytes())?;
182+
} else {
183+
io::stdout().write_all(toml.as_bytes())?;
184+
}
178185
Ok(Summary::default())
179186
}
180187
Operation::Stdin { input, config_path } => {
@@ -327,8 +334,20 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
327334
return Ok(Operation::ConfigHelp);
328335
}
329336

330-
if let Some(path) = matches.opt_str("dump-default-config") {
331-
return Ok(Operation::ConfigOutputDefault { path });
337+
if matches.opt_present("dump-default-config") {
338+
// NOTE for some reason when configured with HasArg::Maybe + Occur::Optional opt_default
339+
// doesn't recognize `--foo bar` as a long flag with an argument but as a long flag with no
340+
// argument *plus* a free argument. Thus we check for that case in this branch -- this is
341+
// required for backward compatibility.
342+
if let Some(path) = matches.free.get(0) {
343+
return Ok(Operation::ConfigOutputDefault {
344+
path: Some(path.clone()),
345+
});
346+
} else {
347+
return Ok(Operation::ConfigOutputDefault {
348+
path: matches.opt_str("dump-default-config"),
349+
});
350+
}
332351
}
333352

334353
if matches.opt_present("version") {

0 commit comments

Comments
 (0)