Skip to content

Commit

Permalink
update: changed parse_args to support sub command (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored Aug 7, 2024
1 parent 7a84372 commit 9492709
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
23 changes: 21 additions & 2 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ fn parse_args<'a, F1, F2, F3>(
mut collect_args: F1,
mut collect_opts: F2,
take_opt_args: F3,
) -> Result<(), InvalidOption>
until_1st_arg: bool,
) -> Result<Option<usize>, InvalidOption>
where
F1: FnMut(&'a str),
F2: FnMut(&'a str, Option<&'a str>) -> Result<(), InvalidOption>,
Expand All @@ -28,6 +29,12 @@ where
'L0: for (i_arg, arg) in args.iter().enumerate() {
if is_non_opt {
collect_args(arg);
if until_1st_arg {
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
}
} else if !prev_opt_taking_args.is_empty() {
match collect_opts(prev_opt_taking_args, Some(arg)) {
Err(err) => {
Expand Down Expand Up @@ -101,6 +108,12 @@ where
} else if arg.starts_with("-") {
if arg.len() == 1 {
collect_args(arg);
if until_1st_arg {
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
}
continue 'L0;
}

Expand Down Expand Up @@ -164,12 +177,18 @@ where
}
} else {
collect_args(arg);
if until_1st_arg {
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
}
}
}

match first_err {
Some(err) => Err(err),
None => Ok(()),
None => Ok(None),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<'a> Cmd<'a> {
collect_args,
collect_opts,
take_opt_args,
false,
) {
Ok(_) => {}
Err(err) => return Err(err),
Expand Down
19 changes: 11 additions & 8 deletions src/parse/parse_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ impl<'a> Cmd<'a> {
/// }
/// ```
pub fn parse_with(&mut self, opt_cfgs: Vec<OptCfg>) -> Result<(), InvalidOption> {
let result = self._parse_with(&opt_cfgs);
let result = self.parse_args_with(&opt_cfgs, false);
self.cfgs = opt_cfgs;
result
result?;
Ok(())
}

#[inline(always)]
fn _parse_with(&mut self, opt_cfgs: &Vec<OptCfg>) -> Result<(), InvalidOption> {
pub fn parse_args_with(
&mut self,
opt_cfgs: &Vec<OptCfg>,
until_1st_arg: bool,
) -> Result<Option<usize>, InvalidOption> {
let mut cfg_map = HashMap::<&str, usize>::new();
let mut opt_map = HashMap::<&str, ()>::new();

Expand Down Expand Up @@ -137,7 +141,7 @@ impl<'a> Cmd<'a> {
}

if self._leaked_strs.is_empty() {
return Ok(());
return Ok(None);
}

let take_opt_args = |opt: &str| {
Expand Down Expand Up @@ -238,14 +242,13 @@ impl<'a> Cmd<'a> {
collect_args,
collect_opts,
take_opt_args,
until_1st_arg,
);

for str_ref in str_refs {
self._leaked_strs.push(str_ref);
}

result?;

for cfg in opt_cfgs.iter() {
let store_key: &str = if !cfg.store_key.is_empty() {
&cfg.store_key
Expand Down Expand Up @@ -282,7 +285,7 @@ impl<'a> Cmd<'a> {
}
}

Ok(())
result
}
}

Expand Down

0 comments on commit 9492709

Please sign in to comment.