Skip to content

Commit 2c9ae48

Browse files
committed
Oops, we can't parse options until all options have been defined. Tiny bit of manual arg-parsing. Fixed tidy stuff too.
1 parent aa4bd0e commit 2c9ae48

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/bootstrap/flags.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,28 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`");
113113
process::exit(exit_code);
114114
};
115115

116-
// Get subcommand
117-
let matches = opts.parse(&args[..]).unwrap_or_else(|e| {
118-
// Invalid argument/option format
119-
println!("\n{}\n", e);
120-
usage(1, &opts, &subcommand_help, &extra_help);
121-
});
122-
let subcommand = match matches.free.get(0) {
123-
Some(s) => { s },
124-
None => {
125-
// No subcommand -- lets only show the general usage and subcommand help in this case.
116+
// We can't use getopt to parse the options until we have completed specifying which
117+
// options are valid, but under the current implementation, some options are conditional on
118+
// the subcommand. Therefore we must manually identify the subcommand first, so that we can
119+
// complete the definition of the options. Then we can use the getopt::Matches object from
120+
// there on out.
121+
let mut possible_subcommands = args.iter().collect::<Vec<_>>();
122+
possible_subcommands.retain(|&s| !s.starts_with('-'));
123+
let subcommand = match possible_subcommands.first() {
124+
Some(s) => s,
125+
None => {
126+
// No subcommand -- show the general usage and subcommand help
126127
println!("{}\n", subcommand_help);
127128
process::exit(0);
128129
}
129130
};
130131

131-
// Get any optional paths which occur after the subcommand
132-
let cwd = t!(env::current_dir());
133-
let paths = matches.free[1..].iter().map(|p| cwd.join(p)).collect::<Vec<_>>();
134-
135132
// Some subcommands have specific arguments help text
136133
match subcommand.as_str() {
137134
"build" => {
138135
subcommand_help.push_str("\n
139136
Arguments:
140-
This subcommand accepts a number of paths to directories to the crates
137+
This subcommand accepts a number of paths to directories to the crates
141138
and/or artifacts to compile. For example:
142139
143140
./x.py build src/libcore
@@ -195,6 +192,17 @@ Arguments:
195192
_ => { }
196193
};
197194

195+
// Done specifying what options are possible, so do the getopts parsing
196+
let matches = opts.parse(&args[..]).unwrap_or_else(|e| {
197+
// Invalid argument/option format
198+
println!("\n{}\n", e);
199+
usage(1, &opts, &subcommand_help, &extra_help);
200+
});
201+
// Get any optional paths which occur after the subcommand
202+
let cwd = t!(env::current_dir());
203+
let paths = matches.free[1..].iter().map(|p| cwd.join(p)).collect::<Vec<_>>();
204+
205+
198206
// All subcommands can have an optional "Available paths" section
199207
if matches.opt_present("verbose") {
200208
let flags = Flags::parse(&["build".to_string()]);

0 commit comments

Comments
 (0)