Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modified to inherit end opt flag after parsing until sub command #53

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ pub struct Cmd<'a> {
args: Vec<&'a str>,
opts: HashMap<&'a str, Vec<&'a str>>,
cfgs: Vec<OptCfg>,
is_after_end_opt: bool,

_leaked_strs: Vec<&'a str>,
_num_of_args: usize,
Expand Down Expand Up @@ -457,6 +458,7 @@ impl<'b, 'a> Cmd<'a> {
args: Vec::new(),
opts: HashMap::new(),
cfgs: Vec::new(),
is_after_end_opt: false,
_leaked_strs,
_num_of_args,
})
Expand Down Expand Up @@ -496,12 +498,13 @@ impl<'b, 'a> Cmd<'a> {
args: Vec::new(),
opts: HashMap::new(),
cfgs: Vec::new(),
is_after_end_opt: false,
_leaked_strs,
_num_of_args,
}
}

fn sub_cmd(&'a self, from_index: usize) -> Cmd<'b> {
fn sub_cmd(&'a self, from_index: usize, is_after_end_opt: bool) -> Cmd<'b> {
let arg_iter = self._leaked_strs[from_index..(self._num_of_args)].into_iter();
let (size, _) = arg_iter.size_hint();
let mut _leaked_strs = Vec::with_capacity(size);
Expand All @@ -518,6 +521,7 @@ impl<'b, 'a> Cmd<'a> {
args: Vec::new(),
opts: HashMap::new(),
cfgs: Vec::new(),
is_after_end_opt: is_after_end_opt,
_leaked_strs,
_num_of_args,
}
Expand Down
11 changes: 6 additions & 5 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ fn parse_args<'a, F1, F2, F3>(
mut collect_opts: F2,
take_opt_args: F3,
until_1st_arg: bool,
) -> Result<Option<usize>, InvalidOption>
is_after_end_opt: bool,
) -> Result<Option<(usize, bool)>, InvalidOption>
where
F1: FnMut(&'a str),
F2: FnMut(&'a str, Option<&'a str>) -> Result<(), InvalidOption>,
F3: Fn(&str) -> bool,
{
let mut is_non_opt = false;
let mut is_non_opt = is_after_end_opt;
let mut prev_opt_taking_args = "";
let mut first_err: Option<InvalidOption> = None;

Expand All @@ -33,7 +34,7 @@ where
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
return Ok(Some((i_arg, is_non_opt)));
}
collect_args(arg);
} else if !prev_opt_taking_args.is_empty() {
Expand Down Expand Up @@ -114,7 +115,7 @@ where
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
return Ok(Some((i_arg, is_non_opt)));
}
collect_args(arg);
continue 'L0;
Expand Down Expand Up @@ -183,7 +184,7 @@ where
if let Some(err) = first_err {
return Err(err);
}
return Ok(Some(i_arg));
return Ok(Some((i_arg, is_non_opt)));
}
collect_args(arg);
}
Expand Down
192 changes: 190 additions & 2 deletions src/parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<'b, 'a> Cmd<'a> {
collect_opts,
take_opt_args,
false,
self.is_after_end_opt,
) {
Ok(_) => {}
Err(err) => return Err(err),
Expand Down Expand Up @@ -112,14 +113,15 @@ impl<'b, 'a> Cmd<'a> {
let take_opt_args = |_arg: &str| false;

if self._num_of_args > 0 {
if let Some(idx) = parse_args(
if let Some((idx, is_after_end_opt)) = parse_args(
&self._leaked_strs[1..(self._num_of_args)],
collect_args,
collect_opts,
take_opt_args,
true,
self.is_after_end_opt,
)? {
return Ok(Some(self.sub_cmd(idx + 1))); // +1, because parse_args parses from 1.
return Ok(Some(self.sub_cmd(idx + 1, is_after_end_opt))); // +1, because parse_args parses from 1.
}
}

Expand Down Expand Up @@ -823,4 +825,190 @@ mod tests_of_parse_until_sub_cmd {
assert_eq!(sub_cmd.has_opt("baz"), true);
}
}

#[test]
fn should_parse_single_hyphen() {
let mut cmd = Cmd::with_strings([
"/path/to/app".to_string(),
"-a".to_string(),
"-".to_string(),
"b".to_string(),
"-".to_string(),
]);

match cmd.parse_until_sub_cmd() {
Ok(None) => assert!(false),
Ok(Some(mut sub_cmd)) => {
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("a"), true);

match sub_cmd.parse() {
Ok(_) => {
assert_eq!(sub_cmd.name(), "-");
assert_eq!(sub_cmd.args(), &["b", "-"]);
assert_eq!(sub_cmd.has_opt("a"), false);
}
Err(_) => assert!(false),
}
}
Err(_) => assert!(false),
}
}

#[test]
fn should_parse_single_hyphen_but_error() {
let mut cmd = Cmd::with_strings([
"/path/to/app".to_string(),
"-a".to_string(),
"-@".to_string(),
"-".to_string(),
"b".to_string(),
"-".to_string(),
]);

match cmd.parse_until_sub_cmd() {
Ok(_) => assert!(false),
Err(InvalidOption::OptionContainsInvalidChar { option }) => {
assert_eq!(option, "@");
}
Err(_) => assert!(false),
}

assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("a"), true);
}

#[test]
fn should_parse_with_end_opt_mark() {
let mut cmd = Cmd::with_strings([
"/path/to/app".to_string(),
"sub".to_string(),
"--".to_string(),
"-a".to_string(),
"-s@".to_string(),
"--".to_string(),
"xxx".to_string(),
]);

match cmd.parse_until_sub_cmd() {
Ok(None) => assert!(false),
Ok(Some(mut sub_cmd)) => {
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("a"), false);
assert_eq!(cmd.opt_arg("a"), None);
assert_eq!(cmd.opt_args("a"), None);
assert_eq!(cmd.has_opt("alphabet"), false);
assert_eq!(cmd.opt_arg("alphabet"), None);
assert_eq!(cmd.opt_args("alphabet"), None);
assert_eq!(cmd.has_opt("s"), false);
assert_eq!(cmd.opt_arg("s"), None);
assert_eq!(cmd.opt_args("s"), None);
assert_eq!(cmd.has_opt("silent"), false);
assert_eq!(cmd.opt_arg("silent"), None);
assert_eq!(cmd.opt_args("silent"), None);

match sub_cmd.parse() {
Err(_) => assert!(false),
Ok(_) => {
assert_eq!(sub_cmd.name(), "sub");
assert_eq!(sub_cmd.args(), &["-a", "-s@", "--", "xxx"]);
assert_eq!(sub_cmd.has_opt("a"), false);
assert_eq!(sub_cmd.opt_arg("a"), None);
assert_eq!(sub_cmd.opt_args("a"), None);
assert_eq!(sub_cmd.has_opt("alphabet"), false);
assert_eq!(sub_cmd.opt_arg("alphabet"), None);
assert_eq!(sub_cmd.opt_args("alphabet"), None);
assert_eq!(sub_cmd.has_opt("s"), false);
assert_eq!(sub_cmd.opt_arg("s"), None);
assert_eq!(sub_cmd.opt_args("s"), None);
assert_eq!(sub_cmd.has_opt("silent"), false);
assert_eq!(sub_cmd.opt_arg("silent"), None);
assert_eq!(sub_cmd.opt_args("silent"), None);
}
}
}
Err(_) => assert!(false),
}
}

#[test]
fn should_parse_after_end_opt_mark() {
let mut cmd = Cmd::with_strings([
"/path/to/app".to_string(),
"-s".to_string(),
"--".to_string(),
"-a".to_string(),
"-s@".to_string(),
"--".to_string(),
"xxx".to_string(),
]);

match cmd.parse_until_sub_cmd() {
Ok(None) => assert!(false),
Ok(Some(mut sub_cmd)) => {
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("a"), false);
assert_eq!(cmd.opt_arg("a"), None);
assert_eq!(cmd.opt_args("a"), None);
assert_eq!(cmd.has_opt("alphabet"), false);
assert_eq!(cmd.opt_arg("alphabet"), None);
assert_eq!(cmd.opt_args("alphabet"), None);
assert_eq!(cmd.has_opt("s"), true);
assert_eq!(cmd.opt_arg("s"), None);
assert_eq!(cmd.opt_args("s"), Some(&[] as &[&str]));
assert_eq!(cmd.has_opt("silent"), false);
assert_eq!(cmd.opt_arg("silent"), None);
assert_eq!(cmd.opt_args("silent"), None);

match sub_cmd.parse() {
Err(_) => assert!(false),
Ok(_) => {
assert_eq!(sub_cmd.name(), "-a");
assert_eq!(sub_cmd.args(), &["-s@", "--", "xxx"]);
assert_eq!(sub_cmd.has_opt("a"), false);
assert_eq!(sub_cmd.opt_arg("a"), None);
assert_eq!(sub_cmd.opt_args("a"), None);
assert_eq!(sub_cmd.has_opt("alphabet"), false);
assert_eq!(sub_cmd.opt_arg("alphabet"), None);
assert_eq!(sub_cmd.opt_args("alphabet"), None);
assert_eq!(sub_cmd.has_opt("s"), false);
assert_eq!(sub_cmd.opt_arg("s"), None);
assert_eq!(sub_cmd.opt_args("s"), None);
assert_eq!(sub_cmd.has_opt("silent"), false);
assert_eq!(sub_cmd.opt_arg("silent"), None);
assert_eq!(sub_cmd.opt_args("silent"), None);
}
}
}
Err(_) => assert!(false),
}
}

#[test]
fn should_parse_after_end_opt_mark_but_error() {
let mut cmd = Cmd::with_strings([
"/path/to/app".to_string(),
"-@".to_string(),
"--".to_string(),
"-a".to_string(),
"-s@".to_string(),
"--".to_string(),
"xxx".to_string(),
]);

match cmd.parse_until_sub_cmd() {
Ok(_) => assert!(false),
Err(InvalidOption::OptionContainsInvalidChar { option }) => {
assert_eq!(option, "@");
}
Err(_) => assert!(false),
}

assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
}
}
88 changes: 88 additions & 0 deletions src/parse/parse_for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,4 +1909,92 @@ mod tests_of_parse_util_sub_cmd_for {

assert_eq!(my_options.foo, false);
}

#[test]
fn should_parse_with_end_opt_mark() {
let mut my_options = MyOptions::with_defaults();
let mut sub_options = SubOptions::with_defaults();

let mut cmd = Cmd::with_strings([
"app".to_string(),
"--foo".to_string(),
"sub".to_string(),
"--".to_string(),
"bar".to_string(),
"-@".to_string(),
]);

match cmd.parse_until_sub_cmd_for(&mut my_options) {
Ok(Some(mut sub_cmd)) => {
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("foo"), true);
assert_eq!(cmd.opt_arg("foo"), None);
assert_eq!(cmd.opt_args("foo"), Some(&[] as &[&str]));
assert_eq!(cmd.has_opt("bar"), false);
assert_eq!(cmd.opt_arg("bar"), None);
assert_eq!(cmd.opt_args("bar"), None);

match sub_cmd.parse_for(&mut sub_options) {
Ok(_) => {
assert_eq!(sub_cmd.name(), "sub");
assert_eq!(sub_cmd.args(), &["bar", "-@"] as &[&str]);
assert_eq!(sub_cmd.has_opt("foo"), false);
assert_eq!(sub_cmd.opt_arg("foo"), None);
assert_eq!(sub_cmd.opt_args("foo"), None);
assert_eq!(sub_cmd.has_opt("bar"), false);
assert_eq!(sub_cmd.opt_arg("bar"), None);
assert_eq!(sub_cmd.opt_args("bar"), None);
}
Err(_) => assert!(false),
}
}
Ok(None) => assert!(false),
Err(_) => assert!(false),
}
}

#[test]
fn should_parse_after_end_opt_mark() {
let mut my_options = MyOptions::with_defaults();
let mut sub_options = SubOptions::with_defaults();

let mut cmd = Cmd::with_strings([
"app".to_string(),
"--".to_string(),
"--foo".to_string(),
"sub".to_string(),
"bar".to_string(),
"-@".to_string(),
]);

match cmd.parse_until_sub_cmd_for(&mut my_options) {
Ok(Some(mut sub_cmd)) => {
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &[] as &[&str]);
assert_eq!(cmd.has_opt("foo"), false);
assert_eq!(cmd.opt_arg("foo"), None);
assert_eq!(cmd.opt_args("foo"), None);
assert_eq!(cmd.has_opt("bar"), false);
assert_eq!(cmd.opt_arg("bar"), None);
assert_eq!(cmd.opt_args("bar"), None);

match sub_cmd.parse_for(&mut sub_options) {
Ok(_) => {
assert_eq!(sub_cmd.name(), "--foo");
assert_eq!(sub_cmd.args(), &["sub", "bar", "-@"] as &[&str]);
assert_eq!(sub_cmd.has_opt("foo"), false);
assert_eq!(sub_cmd.opt_arg("foo"), None);
assert_eq!(sub_cmd.opt_args("foo"), None);
assert_eq!(sub_cmd.has_opt("bar"), false);
assert_eq!(sub_cmd.opt_arg("bar"), None);
assert_eq!(sub_cmd.opt_args("bar"), None);
}
Err(_) => assert!(false),
}
}
Ok(None) => assert!(false),
Err(_) => assert!(false),
}
}
}
Loading