ported arg parsers to parse_next

arg-parsing-better
Avril 4 years ago
parent 6a8d4ccd1e
commit f6e674d190
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -36,7 +36,7 @@ const OPTIONS_NORMAL: &'static [&'static str] = &[
#[cfg(feature="inspect")] "--save <file> Dump the collected data to this file for further inspection.", #[cfg(feature="inspect")] "--save <file> Dump the collected data to this file for further inspection.",
#[cfg(feature="inspect")] "-D Dump the collected data to `stdout` (see `--save`.)", #[cfg(feature="inspect")] "-D Dump the collected data to `stdout` (see `--save`.)",
#[cfg(feature="inspect")] "--save-raw <file> Dump the collected data to this file uncompressed. (see `--save`.)", #[cfg(feature="inspect")] "--save-raw <file> Dump the collected data to this file uncompressed. (see `--save`.)",
#[cfg(feature="inspect")] "-Dr Dump the collected data to standard output uncompressed. (see `--save-raw`.)", #[cfg(feature="inspect")] "-R Dump the collected data to standard output uncompressed. (see `--save-raw`.)",
"- Stop parsing arguments, treat all the rest as paths.", "- Stop parsing arguments, treat all the rest as paths.",
]; ];
@ -153,7 +153,7 @@ fn parse<I: IntoIterator<Item=String>>(args: I) -> eyre::Result<Mode>
#[cfg(feature="inspect")] "-D" => { #[cfg(feature="inspect")] "-D" => {
cfg.serialise_output = Some(config::OutputSerialisationMode::Stdout); cfg.serialise_output = Some(config::OutputSerialisationMode::Stdout);
}, },
#[cfg(feature="inspect")] "-Dr" => { #[cfg(feature="inspect")] "-R" => {
cfg.serialise_output = Some(config::OutputSerialisationMode::RawStdout); cfg.serialise_output = Some(config::OutputSerialisationMode::RawStdout);
}, },
#[cfg(feature="inspect")] "--save" => { #[cfg(feature="inspect")] "--save" => {

@ -223,11 +223,44 @@ pub type Output = HashSet<Argument>;
"If this was intended as a path instead of an option, use option `-` before it." "If this was intended as a path instead of an option, use option `-` before it."
} }
fn parse_single<I>(args: &mut I, output: &mut Output, this: char) -> eyre::Result<Continue> fn save_output(output: &mut Output, item: Argument) -> eyre::Result<()>
where I: Iterator<Item=String> {
if let Some(mx) = output.iter().filter(|arg| item.is_mx_with(arg)).next() {
return Err(eyre!("Arguments are mutually exclusive"))
.with_section(|| item.header("Trying to add"))
.with_section(|| mx.to_string().header("Which is mutually exclusive with"));
}
output.insert(item); //TODO: Warn when adding duplicate?
Ok(())
}
fn parse_single<I>(_args: &mut I, output: &mut Output, this: char) -> eyre::Result<Continue>
where I: Iterator<Item=String>
{ {
//TODO: Parse single letter args let item = match this
{
'r' => Argument::UnlimitRecurse,
#[cfg(feature="inspect")] 'D' => Argument::SaveStdout,
#[cfg(feature="inspect")] 'R' => Argument::SaveRawStdout,
'v' => Argument::LogVerbose,
'q' => Argument::LogQuiet,
'Q' => Argument::LogSilent,
'm' => Argument::LimitConcMaxProc,
'M' => Argument::UnlimitConc,
unknown => {
return Err(eyre!("Unknown short argument {:?}", unknown))
.with_suggestion(suggestion_intended_arg.clone());
},
};
save_output(output, item)?;
Ok(Continue::Yes) Ok(Continue::Yes)
} }
@ -256,7 +289,18 @@ where I: Iterator<Item=String>
"-" => { "-" => {
return Ok(Continue::No); return Ok(Continue::No);
}, },
//TODO: move rest of long args from `mod` to here #[cfg(feature="inspect")] "--save" => {
let file = args.next().ok_or(eyre!("`--save` expects a parameter"))
.with_suggestion(suggestion_intended_arg.clone())?;
Argument::Save(file)
},
#[cfg(feature="inspect")] "--save-raw" => {
let file = args.next().ok_or(eyre!("`--save` expects a parameter"))
.with_suggestion(suggestion_intended_arg.clone())?;
Argument::SaveRaw(file)
},
single if single.starts_with("-") => { single if single.starts_with("-") => {
for ch in single.chars().skip(1) { for ch in single.chars().skip(1) {
match parse_single(args, output, ch)? { match parse_single(args, output, ch)? {
@ -274,13 +318,7 @@ where I: Iterator<Item=String>
} }
}; };
if let Some(mx) = output.iter().filter(|arg| item.is_mx_with(arg)).next() { save_output(output, item)?;
return Err(eyre!("Arguments are mutually exclusive"))
.with_section(|| item.header("Trying to add"))
.with_section(|| mx.to_string().header("Which is mutually exclusive with"));
}
output.insert(item); //TODO: Warn when adding duplicate?
Ok(keep_reading) Ok(keep_reading)
} }

Loading…
Cancel
Save