Use a method to apply RustcOptGroup to getopts::Options

This commit is contained in:
Zalathar 2024-11-08 12:20:51 +11:00
parent 8f7f9b93b2
commit 584c8200de
4 changed files with 11 additions and 7 deletions

View File

@ -937,7 +937,7 @@ fn usage(verbose: bool, include_unstable_options: bool, nightly_build: bool) {
let groups = if verbose { config::rustc_optgroups() } else { config::rustc_short_optgroups() };
let mut options = getopts::Options::new();
for option in groups.iter().filter(|x| include_unstable_options || x.is_stable()) {
(option.apply)(&mut options);
option.apply(&mut options);
}
let message = "Usage: rustc [OPTIONS] INPUT";
let nightly_help = if nightly_build {
@ -1219,7 +1219,7 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
let mut options = getopts::Options::new();
let optgroups = config::rustc_optgroups();
for option in &optgroups {
(option.apply)(&mut options);
option.apply(&mut options);
}
let matches = options.parse(args).unwrap_or_else(|e| {
let msg: Option<String> = match e {
@ -1233,7 +1233,7 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
optgroups.iter().find(|option| option.name == opt).map(|option| {
// Print the help just for the option in question.
let mut options = getopts::Options::new();
(option.apply)(&mut options);
option.apply(&mut options);
// getopt requires us to pass a function for joining an iterator of
// strings, even though in this case we expect exactly one string.
options.usage_with_format(|it| {

View File

@ -102,7 +102,7 @@ fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
fn optgroups() -> getopts::Options {
let mut opts = getopts::Options::new();
for group in rustc_optgroups() {
(group.apply)(&mut opts);
group.apply(&mut opts);
}
return opts;
}

View File

@ -1373,7 +1373,7 @@ enum OptionStability {
}
pub struct RustcOptGroup {
pub apply: Box<dyn Fn(&mut getopts::Options) -> &mut getopts::Options>,
apply: Box<dyn Fn(&mut getopts::Options) -> &mut getopts::Options>,
pub name: &'static str,
stability: OptionStability,
}
@ -1383,6 +1383,10 @@ pub fn is_stable(&self) -> bool {
self.stability == OptionStability::Stable
}
pub fn apply(&self, options: &mut getopts::Options) {
(self.apply)(options);
}
pub fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
where
F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,

View File

@ -685,7 +685,7 @@ fn opts() -> Vec<RustcOptGroup> {
fn usage(argv0: &str) {
let mut options = getopts::Options::new();
for option in opts() {
(option.apply)(&mut options);
option.apply(&mut options);
}
println!("{}", options.usage(&format!("{argv0} [options] <input>")));
println!(" @path Read newline separated options from `path`\n");
@ -769,7 +769,7 @@ fn main_args(
let mut options = getopts::Options::new();
for option in opts() {
(option.apply)(&mut options);
option.apply(&mut options);
}
let matches = match options.parse(&args) {
Ok(m) => m,