Skip to content

Commit e1d59cd

Browse files
committed
Add a separate parse_tester_args function and add add a --target argument
1 parent c8e7bf7 commit e1d59cd

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

src/args.rs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ pub(crate) fn parse_args() -> Result<Command, ErrorString> {
3030
cmd => cmd,
3131
}),
3232
Some("runner") => parse_runner_args(args),
33-
Some("tester") => parse_runner_args(args).map(|cmd| match cmd {
34-
Command::Runner(args) => Command::Tester(TesterArgs::from(args)),
35-
Command::RunnerHelp => Command::TesterHelp,
36-
other => other,
37-
}),
33+
Some("tester") => parse_tester_args(args),
3834
Some("--help") | Some("-h") => Ok(Command::Help),
3935
Some("--version") => Ok(Command::Version),
4036
_ => Ok(Command::NoSubcommand),
@@ -252,7 +248,7 @@ where
252248
Ok(Command::Runner(RunnerArgs {
253249
executable,
254250
run_command,
255-
run_args: run_args,
251+
run_args,
256252
}))
257253
}
258254

@@ -263,17 +259,59 @@ pub struct RunnerArgs {
263259
pub run_args: Option<Vec<String>>,
264260
}
265261

262+
fn parse_tester_args<A>(args: A) -> Result<Command, ErrorString>
263+
where
264+
A: Iterator<Item = String>,
265+
{
266+
let mut arg_iter = args.into_iter().fuse();
267+
let test_path = PathBuf::from(
268+
arg_iter
269+
.next()
270+
.ok_or("excepted path to test source file as first argument")?,
271+
)
272+
.canonicalize()
273+
.map_err(|err| format!("Failed to canonicalize test path: {}", err))?;
274+
let mut run_command = None;
275+
let mut target = None;
276+
277+
loop {
278+
match arg_iter.next().as_ref().map(|s| s.as_str()) {
279+
Some("--command") => {
280+
let old = mem::replace(&mut run_command, Some(arg_iter.collect()));
281+
if !old.is_none() {
282+
Err("multiple `--command` arguments")?;
283+
}
284+
break;
285+
}
286+
Some("--target") => {
287+
let old = mem::replace(&mut target, arg_iter.next());
288+
if !old.is_none() {
289+
Err("multiple `--target` arguments")?;
290+
}
291+
break;
292+
}
293+
Some("--help") | Some("-h") => {
294+
return Ok(Command::TesterHelp);
295+
}
296+
Some("--version") => {
297+
return Ok(Command::Version);
298+
}
299+
None => break,
300+
Some(arg) => Err(format!("unexpected argument `{}`", arg))?,
301+
}
302+
}
303+
304+
Ok(Command::Tester(TesterArgs {
305+
test_path,
306+
run_command,
307+
target,
308+
}))
309+
}
310+
266311
#[derive(Debug, Clone)]
267312
pub struct TesterArgs {
268313
pub test_path: PathBuf,
269314
pub run_command: Option<Vec<String>>,
315+
pub target: Option<String>,
270316
}
271317

272-
impl From<RunnerArgs> for TesterArgs {
273-
fn from(args: RunnerArgs) -> Self {
274-
Self {
275-
test_path: args.executable,
276-
run_command: args.run_command,
277-
}
278-
}
279-
}

0 commit comments

Comments
 (0)