@@ -21,7 +21,7 @@ use std::io::{self, Read, Write};
21
21
use std:: path:: { Path , PathBuf } ;
22
22
use std:: str:: FromStr ;
23
23
24
- use getopts:: { Matches , Options } ;
24
+ use getopts:: { HasArg , Matches , Occur , Options } ;
25
25
26
26
use rustfmt:: { run, Input , Summary } ;
27
27
use rustfmt:: file_lines:: FileLines ;
@@ -44,8 +44,8 @@ enum Operation {
44
44
Version ,
45
45
/// Print detailed configuration help.
46
46
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 > } ,
49
49
/// No file specified, read from stdin
50
50
Stdin {
51
51
input : String ,
@@ -125,11 +125,14 @@ fn make_opts() -> Options {
125
125
"config-help" ,
126
126
"show details of rustfmt configuration options" ,
127
127
) ;
128
- opts. optopt (
128
+ opts. opt (
129
129
"" ,
130
130
"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.",
132
133
"PATH" ,
134
+ HasArg :: Maybe ,
135
+ Occur :: Optional ,
133
136
) ;
134
137
opts. optopt (
135
138
"" ,
@@ -172,9 +175,13 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
172
175
Ok ( Summary :: default ( ) )
173
176
}
174
177
Operation :: ConfigOutputDefault { path } => {
175
- let mut file = File :: create ( path) ?;
176
178
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
+ }
178
185
Ok ( Summary :: default ( ) )
179
186
}
180
187
Operation :: Stdin { input, config_path } => {
@@ -327,8 +334,20 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
327
334
return Ok ( Operation :: ConfigHelp ) ;
328
335
}
329
336
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
+ }
332
351
}
333
352
334
353
if matches. opt_present ( "version" ) {
0 commit comments