arg parsing works

arg-parsing-better
Avril 4 years ago
parent deddddcdce
commit a264a7b05d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -127,7 +127,7 @@ fn parse<I: IntoIterator<Item=String>>(args: I) -> eyre::Result<Mode>
parsing::consume(args, &mut buffer);
break;
},
parsing::Continue::Abort(Some(change_to)) => return Ok(change_to.try_into_mode(buffer).unwrap()),
parsing::Continue::Abort(Some(change_to)) => return Ok(*change_to),
parsing::Continue::Abort(_) => break,
_ => (),
}

@ -99,7 +99,7 @@ impl Argument
},
#[cfg(feature="inspect")] SaveRawStdout => cfg.serialise_output = Some(OutputSerialisationMode::RawStdout),
LimitRecurse(limit) => cfg.recursive = config::Recursion::Limited(limit),
LimitRecurse(limit) => cfg.recursive = if limit.get() == 1 { config::Recursion::None } else { config::Recursion::Limited(limit) },
UnlimitRecurse => cfg.recursive = config::Recursion::Unlimited,
LogVerbose => cfg.output_level = config::OutputLevel::Verbose,
@ -108,7 +108,7 @@ impl Argument
Input(path) => cfg.paths.push(path.into()),
_ => unreachable!(),
_ => (), //unreachable()! // Do nothing instead of panic.
}
}
}
@ -123,7 +123,7 @@ impl fmt::Display for Argument
ModeChangeHelp => write!(f, "--help"),
LimitConcMaxProc => write!(f, "-m"),
LimitConc(limit) => write!(f, "--threads {}", limit),
UnlimitConc => write!(f, "-M"),
UnlimitConc => write!(f, "-M (--threads 0)"),
Save(s) => write!(f, "--save {:?}", s),
SaveStdout => write!(f, "-D"),
@ -131,7 +131,7 @@ impl fmt::Display for Argument
SaveRawStdout => write!(f, "-R"),
LimitRecurse(rec) => write!(f, "--recursive {}", rec),
UnlimitRecurse => write!(f, "-r"),
UnlimitRecurse => write!(f, "-r (--recursive 0)"),
LogVerbose => write!(f, "-v"),
LogQuiet => write!(f, "-q"),
@ -255,7 +255,7 @@ impl Argument
}
/// Should we continue parsing and/or reading arguments?
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Continue
{
/// Keep parsing the arguments
@ -268,7 +268,7 @@ pub enum Continue
// Box `Argument` to reduce the size of `Continue`, as it is returned from functions often and when its value is set to `Some` it will always be the last `Argument` processed anyway and the only one to be boxed here at all.
//TODO: Deprecate the early return of an `Argument` here. Either change it to `Mode`, or have no early return. Mode change happens at the bottom in `into_mode` now.
Abort(Option<Box<Argument>>),
Abort(Option<Box<Mode>>),
}
impl Continue
@ -326,8 +326,8 @@ fn save_output(output: &mut Output, item: Argument) -> eyre::Result<()>
{
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"));
.with_section(|| item.header("Trying to addargument "))
.with_section(|| mx.to_string().header("Which is mutually exclusive with previously added"));
}
output.insert(item); //TODO: Warn when adding duplicate?
@ -358,7 +358,8 @@ where I: Iterator<Item=String>
},
};
save_output(output, item)?;
save_output(output, item)
.with_section(|| this.header("Short argument was"))?;
Ok(Continue::Yes)
}
@ -388,8 +389,19 @@ where I: Iterator<Item=String>
None => Argument::UnlimitConc,
}
},
"--recursive" => {
let max = args.next().ok_or(eyre!("`--recursive` expects a parameter"))
.with_suggestion(suggestion_intended_arg.clone())?;
match NonZeroUsize::new(max.parse::<usize>().wrap_err(eyre!("`--recursive` expects a non-negative number"))
.with_suggestion(suggestion_intended_arg.clone())
.with_section(move || max.header("Parameter given was"))?)
{
Some(x) => Argument::LimitRecurse(x),
None => Argument::UnlimitRecurse,
}
},
"--help" => {
return Ok(Continue::Abort(Some(Box::new(Argument::ModeChangeHelp))));
return Ok(Continue::Abort(Some(Box::new(Mode::Help))));
},
"-" => {
return Ok(Continue::No);
@ -408,12 +420,14 @@ where I: Iterator<Item=String>
},
single if single.starts_with("-") => {
for ch in single.chars().skip(1) {
match parse_single(args, output, ch)? {
abort @ Continue::Abort(Some(_)) => return Ok(abort),
x @ Continue::No |
x @ Continue::Abort(_) if !x.is_abort() => keep_reading = x,
_ => (),
}
match parse_single(args, output, ch)
.wrap_err(eyre!("Error parsing short argument"))
.with_section(|| this.clone().header("Full short argument chain was"))? {
abort @ Continue::Abort(Some(_)) => return Ok(abort),
x @ Continue::No |
x @ Continue::Abort(_) if !x.is_abort() => keep_reading = x,
_ => (),
}
}
return Ok(keep_reading);
},
@ -442,7 +456,10 @@ mod modes {
{
let mut cfg = Config::default();
//TODO: Consume `args` into `cfg` for Normal mode, then return `cfg.`
for arg in args.into_iter()
{
arg.insert_into_cfg(&mut cfg);
}
Ok(cfg)
}

@ -173,6 +173,7 @@ async fn parse_mode() -> eyre::Result<()>
match arg::parse_args().wrap_err(eyre!("Failed to parse args"))?
{
arg::Mode::Normal(cfg) => {
#[cfg(debug_assertions)] eprintln!("cfg: {:#?}", cfg);
normal(cfg).await
},
arg::Mode::Help => arg::help(),

Loading…
Cancel
Save