@@ -67,11 +67,9 @@ enum Operation {
67
67
minimal_config_path : Option < String > ,
68
68
} ,
69
69
/// Print the help message.
70
- Help ,
70
+ Help ( HelpOp ) ,
71
71
// Print version information
72
72
Version ,
73
- /// Print detailed configuration help.
74
- ConfigHelp ,
75
73
/// Output default config to a file, or stdout if None
76
74
ConfigOutputDefault {
77
75
path : Option < String > ,
@@ -82,6 +80,13 @@ enum Operation {
82
80
} ,
83
81
}
84
82
83
+ /// Arguments to `--help`
84
+ enum HelpOp {
85
+ None ,
86
+ Config ,
87
+ FileLines ,
88
+ }
89
+
85
90
fn make_opts ( ) -> Options {
86
91
let mut opts = Options :: new ( ) ;
87
92
@@ -92,11 +97,6 @@ fn make_opts() -> Options {
92
97
"Use colored output (if supported)" ,
93
98
"[always|never|auto]" ,
94
99
) ;
95
- opts. optflag (
96
- "" ,
97
- "config-help" ,
98
- "Show details of rustfmt configuration options" ,
99
- ) ;
100
100
opts. optopt (
101
101
"" ,
102
102
"config-path" ,
@@ -130,7 +130,12 @@ fn make_opts() -> Options {
130
130
"Format specified line ranges. See README for more detail on the JSON format." ,
131
131
"JSON" ,
132
132
) ;
133
- opts. optflag ( "h" , "help" , "Show this message" ) ;
133
+ opts. optflagopt (
134
+ "h" ,
135
+ "help" ,
136
+ "Show this message or help about a specific topic: config or file-lines" ,
137
+ "=TOPIC" ,
138
+ ) ;
134
139
opts. optflag ( "" , "skip-children" , "Don't reformat child modules" ) ;
135
140
opts. optflag (
136
141
"" ,
@@ -154,17 +159,21 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
154
159
let matches = opts. parse ( env:: args ( ) . skip ( 1 ) ) ?;
155
160
156
161
match determine_operation ( & matches) ? {
157
- Operation :: Help => {
162
+ Operation :: Help ( HelpOp :: None ) => {
158
163
print_usage_to_stdout ( opts, "" ) ;
159
164
Summary :: print_exit_codes ( ) ;
160
165
Ok ( ( WriteMode :: None , Summary :: default ( ) ) )
161
166
}
162
- Operation :: Version => {
163
- print_version ( ) ;
167
+ Operation :: Help ( HelpOp :: Config ) => {
168
+ Config :: print_docs ( & mut stdout ( ) , matches . opt_present ( "unstable-features" ) ) ;
164
169
Ok ( ( WriteMode :: None , Summary :: default ( ) ) )
165
170
}
166
- Operation :: ConfigHelp => {
167
- Config :: print_docs ( & mut stdout ( ) , matches. opt_present ( "unstable-features" ) ) ;
171
+ Operation :: Help ( HelpOp :: FileLines ) => {
172
+ print_help_file_lines ( ) ;
173
+ Ok ( ( WriteMode :: None , Summary :: default ( ) ) )
174
+ }
175
+ Operation :: Version => {
176
+ print_version ( ) ;
168
177
Ok ( ( WriteMode :: None , Summary :: default ( ) ) )
169
178
}
170
179
Operation :: ConfigOutputDefault { path } => {
@@ -298,6 +307,27 @@ fn print_usage_to_stdout(opts: &Options, reason: &str) {
298
307
println ! ( "{}" , opts. usage( & msg) ) ;
299
308
}
300
309
310
+ fn print_help_file_lines ( ) {
311
+ println ! ( "If you want to restrict reformatting to specific sets of lines, you can
312
+ use the `--file-lines` option. Its argument is a JSON array of objects
313
+ with `file` and `range` properties, where `file` is a file name, and
314
+ `range` is an array representing a range of lines like `[7,13]`. Ranges
315
+ are 1-based and inclusive of both end points. Specifying an empty array
316
+ will result in no files being formatted. For example,
317
+
318
+ ```
319
+ rustfmt --file-lines '[
320
+ {{\" file\" :\" src/lib.rs\" ,\" range\" :[7,13]}},
321
+ {{\" file\" :\" src/lib.rs\" ,\" range\" :[21,29]}},
322
+ {{\" file\" :\" src/foo.rs\" ,\" range\" :[10,11]}},
323
+ {{\" file\" :\" src/foo.rs\" ,\" range\" :[15,15]}}]'
324
+ ```
325
+
326
+ would format lines `7-13` and `21-29` of `src/lib.rs`, and lines `10-11`,
327
+ and `15` of `src/foo.rs`. No other files would be formatted, even if they
328
+ are included as out of line modules from `src/lib.rs`." ) ;
329
+ }
330
+
301
331
fn print_version ( ) {
302
332
let version_info = format ! (
303
333
"{}-{}" ,
@@ -310,11 +340,17 @@ fn print_version() {
310
340
311
341
fn determine_operation ( matches : & Matches ) -> FmtResult < Operation > {
312
342
if matches. opt_present ( "h" ) {
313
- return Ok ( Operation :: Help ) ;
314
- }
315
-
316
- if matches. opt_present ( "config-help" ) {
317
- return Ok ( Operation :: ConfigHelp ) ;
343
+ let topic = matches. opt_str ( "h" ) ;
344
+ if topic == None {
345
+ return Ok ( Operation :: Help ( HelpOp :: None ) ) ;
346
+ } else if topic == Some ( "config" . to_owned ( ) ) {
347
+ return Ok ( Operation :: Help ( HelpOp :: Config ) ) ;
348
+ } else if topic == Some ( "file-lines" . to_owned ( ) ) {
349
+ return Ok ( Operation :: Help ( HelpOp :: FileLines ) ) ;
350
+ } else {
351
+ println ! ( "Unknown help topic: `{}`\n " , topic. unwrap( ) ) ;
352
+ return Ok ( Operation :: Help ( HelpOp :: None ) ) ;
353
+ }
318
354
}
319
355
320
356
if matches. opt_present ( "dump-default-config" ) {
0 commit comments