Skip to content

Commit 798bffb

Browse files
committed
--help options
cc #1976
1 parent 1869888 commit 798bffb

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

src/bin/main.rs

+55-19
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ enum Operation {
6767
minimal_config_path: Option<String>,
6868
},
6969
/// Print the help message.
70-
Help,
70+
Help(HelpOp),
7171
// Print version information
7272
Version,
73-
/// Print detailed configuration help.
74-
ConfigHelp,
7573
/// Output default config to a file, or stdout if None
7674
ConfigOutputDefault {
7775
path: Option<String>,
@@ -82,6 +80,13 @@ enum Operation {
8280
},
8381
}
8482

83+
/// Arguments to `--help`
84+
enum HelpOp {
85+
None,
86+
Config,
87+
FileLines,
88+
}
89+
8590
fn make_opts() -> Options {
8691
let mut opts = Options::new();
8792

@@ -92,11 +97,6 @@ fn make_opts() -> Options {
9297
"Use colored output (if supported)",
9398
"[always|never|auto]",
9499
);
95-
opts.optflag(
96-
"",
97-
"config-help",
98-
"Show details of rustfmt configuration options",
99-
);
100100
opts.optopt(
101101
"",
102102
"config-path",
@@ -130,7 +130,12 @@ fn make_opts() -> Options {
130130
"Format specified line ranges. See README for more detail on the JSON format.",
131131
"JSON",
132132
);
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+
);
134139
opts.optflag("", "skip-children", "Don't reformat child modules");
135140
opts.optflag(
136141
"",
@@ -154,17 +159,21 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
154159
let matches = opts.parse(env::args().skip(1))?;
155160

156161
match determine_operation(&matches)? {
157-
Operation::Help => {
162+
Operation::Help(HelpOp::None) => {
158163
print_usage_to_stdout(opts, "");
159164
Summary::print_exit_codes();
160165
Ok((WriteMode::None, Summary::default()))
161166
}
162-
Operation::Version => {
163-
print_version();
167+
Operation::Help(HelpOp::Config) => {
168+
Config::print_docs(&mut stdout(), matches.opt_present("unstable-features"));
164169
Ok((WriteMode::None, Summary::default()))
165170
}
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();
168177
Ok((WriteMode::None, Summary::default()))
169178
}
170179
Operation::ConfigOutputDefault { path } => {
@@ -298,6 +307,27 @@ fn print_usage_to_stdout(opts: &Options, reason: &str) {
298307
println!("{}", opts.usage(&msg));
299308
}
300309

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+
301331
fn print_version() {
302332
let version_info = format!(
303333
"{}-{}",
@@ -310,11 +340,17 @@ fn print_version() {
310340

311341
fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
312342
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+
}
318354
}
319355

320356
if matches.opt_present("dump-default-config") {

0 commit comments

Comments
 (0)