Skip to content

default --dump-default-config path to rustfmt.toml #2046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/bin/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use getopts::{Matches, Options};
use getopts::{HasArg, Matches, Occur, Options};

use rustfmt::{run, Input, Summary};
use rustfmt::file_lines::FileLines;
Expand Down Expand Up @@ -125,11 +125,14 @@ fn make_opts() -> Options {
"config-help",
"show details of rustfmt configuration options",
);
opts.optopt(
opts.opt(
"",
"dump-default-config",
"Dumps the default configuration to a file and exits.",
"Dumps the default configuration to a file and exits. PATH defaults to rustfmt.toml if \
omitted.",
"PATH",
HasArg::Maybe,
Occur::Optional,
);
opts.optopt(
"",
Expand Down Expand Up @@ -327,8 +330,20 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
return Ok(Operation::ConfigHelp);
}

if let Some(path) = matches.opt_str("dump-default-config") {
return Ok(Operation::ConfigOutputDefault { path });
if matches.opt_present("dump-default-config") {
// NOTE for some reason when configured with HasArg::Maybe + Occur::Optional opt_default
// doesn't recognize `--foo bar` as a long flag with an argument but as a long flag with no
// argument *plus* a free argument. Thus we check for that case in this branch -- this is
// required for backward compatibility.
if let Some(path) = matches.free.get(0) {
return Ok(Operation::ConfigOutputDefault { path: path.clone() });
} else {
return Ok(Operation::ConfigOutputDefault {
path: matches
.opt_default("dump-default-config", "rustfmt.toml")
.unwrap(),
});
}
}

if matches.opt_present("version") {
Expand Down