mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-12 08:32:07 +00:00
Fix magiskboot cli parsing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{Utf8CStr, cstr, ffi};
|
||||
use argh::EarlyExit;
|
||||
use argh::{EarlyExit, MissingRequirements};
|
||||
use libc::c_char;
|
||||
use std::{
|
||||
fmt,
|
||||
@@ -102,6 +102,51 @@ impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PositionalArgParser<'a>(pub slice::Iter<'a, &'a str>);
|
||||
|
||||
impl PositionalArgParser<'_> {
|
||||
pub fn required(&mut self, field_name: &'static str) -> Result<String, EarlyExit> {
|
||||
if let Some(next) = self.0.next() {
|
||||
Ok(next.to_string())
|
||||
} else {
|
||||
let mut missing = MissingRequirements::default();
|
||||
missing.missing_positional_arg(field_name);
|
||||
missing.err_on_any()?;
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn optional(&mut self) -> Option<String> {
|
||||
self.0.next().map(ToString::to_string)
|
||||
}
|
||||
|
||||
pub fn last_required(&mut self, field_name: &'static str) -> Result<String, EarlyExit> {
|
||||
let r = self.required(field_name)?;
|
||||
self.ensure_end()?;
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
pub fn last_optional(&mut self) -> Result<Option<String>, EarlyExit> {
|
||||
let r = self.optional();
|
||||
if r.is_none() {
|
||||
return Ok(r);
|
||||
}
|
||||
self.ensure_end()?;
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
fn ensure_end(&mut self) -> Result<(), EarlyExit> {
|
||||
if self.0.len() == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(EarlyExit::from(format!(
|
||||
"Unrecognized argument: {}\n",
|
||||
self.0.next().unwrap()
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FmtAdaptor<'a, T>(pub &'a mut T)
|
||||
where
|
||||
T: Write;
|
||||
|
||||
Reference in New Issue
Block a user