Skip to content

Commit

Permalink
fix: changed to simply move to cfgs field and add opt_cfgs method to …
Browse files Browse the repository at this point in the history
…Cmd (#27)
  • Loading branch information
sttk authored Jul 23, 2024
1 parent 7ce1809 commit 0f54c42
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 159 deletions.
161 changes: 82 additions & 79 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ pub struct Cmd<'a> {
name: &'a str,
args: Vec<&'a str>,
opts: HashMap<&'a str, Vec<&'a str>>,

/// The option configurations which is used to parse command line arguments.
pub cfgs: Vec<OptCfg>,
cfgs: Vec<OptCfg>,

_leaked_strs: Vec<&'a str>,
}
Expand Down Expand Up @@ -412,6 +410,11 @@ impl<'a> Cmd<'a> {
None => None,
}
}

/// Retrieves the option configurations which was used to parse command line arguments.
pub fn opt_cfgs(&'a self) -> &[OptCfg] {
&self.cfgs
}
}

#[cfg(test)]
Expand Down Expand Up @@ -750,10 +753,10 @@ mod tests_of_cmd {
#[test]
fn should_move_by_passing_a_parameter() {
fn move_cmd(cmd: Cmd) {
assert_eq!(cmd.name, "app");
assert_eq!(cmd.args, &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opts.get("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opts.get("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opt_args("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opt_args("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(
cmd._leaked_strs,
&[
Expand All @@ -769,21 +772,21 @@ mod tests_of_cmd {
"bar",
]
);
assert_eq!(cmd.cfgs.len(), 2);
assert_eq!(cmd.cfgs[0].store_key, "");
assert_eq!(cmd.cfgs[0].names, &["foo"]);
assert_eq!(cmd.cfgs[0].has_arg, false);
assert_eq!(cmd.cfgs[0].is_array, false);
assert_eq!(cmd.cfgs[0].defaults, None);
assert_eq!(cmd.cfgs[0].desc, "");
assert_eq!(cmd.cfgs[0].arg_in_help, "");
assert_eq!(cmd.cfgs[1].store_key, "");
assert_eq!(cmd.cfgs[1].names, &["bar"]);
assert_eq!(cmd.cfgs[1].has_arg, true);
assert_eq!(cmd.cfgs[1].is_array, true);
assert_eq!(cmd.cfgs[1].defaults, None);
assert_eq!(cmd.cfgs[1].desc, "");
assert_eq!(cmd.cfgs[1].arg_in_help, "");
assert_eq!(cmd.opt_cfgs().len(), 2);
assert_eq!(cmd.opt_cfgs()[0].store_key, "");
assert_eq!(cmd.opt_cfgs()[0].names, &["foo"]);
assert_eq!(cmd.opt_cfgs()[0].has_arg, false);
assert_eq!(cmd.opt_cfgs()[0].is_array, false);
assert_eq!(cmd.opt_cfgs()[0].defaults, None);
assert_eq!(cmd.opt_cfgs()[0].desc, "");
assert_eq!(cmd.opt_cfgs()[0].arg_in_help, "");
assert_eq!(cmd.opt_cfgs()[1].store_key, "");
assert_eq!(cmd.opt_cfgs()[1].names, &["bar"]);
assert_eq!(cmd.opt_cfgs()[1].has_arg, true);
assert_eq!(cmd.opt_cfgs()[1].is_array, true);
assert_eq!(cmd.opt_cfgs()[1].defaults, None);
assert_eq!(cmd.opt_cfgs()[1].desc, "");
assert_eq!(cmd.opt_cfgs()[1].arg_in_help, "");
}

let cfgs = vec![
Expand Down Expand Up @@ -829,10 +832,10 @@ mod tests_of_cmd {
}

let cmd = move_cmd();
assert_eq!(cmd.name, "app");
assert_eq!(cmd.args, &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opts.get("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opts.get("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opt_args("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opt_args("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(
cmd._leaked_strs,
&[
Expand All @@ -848,21 +851,21 @@ mod tests_of_cmd {
"bar",
]
);
assert_eq!(cmd.cfgs.len(), 2);
assert_eq!(cmd.cfgs[0].store_key, "");
assert_eq!(cmd.cfgs[0].names, &["foo"]);
assert_eq!(cmd.cfgs[0].has_arg, false);
assert_eq!(cmd.cfgs[0].is_array, false);
assert_eq!(cmd.cfgs[0].defaults, None);
assert_eq!(cmd.cfgs[0].desc, "");
assert_eq!(cmd.cfgs[0].arg_in_help, "");
assert_eq!(cmd.cfgs[1].store_key, "");
assert_eq!(cmd.cfgs[1].names, &["bar"]);
assert_eq!(cmd.cfgs[1].has_arg, true);
assert_eq!(cmd.cfgs[1].is_array, true);
assert_eq!(cmd.cfgs[1].defaults, None);
assert_eq!(cmd.cfgs[1].desc, "");
assert_eq!(cmd.cfgs[1].arg_in_help, "");
assert_eq!(cmd.opt_cfgs().len(), 2);
assert_eq!(cmd.opt_cfgs()[0].store_key, "");
assert_eq!(cmd.opt_cfgs()[0].names, &["foo"]);
assert_eq!(cmd.opt_cfgs()[0].has_arg, false);
assert_eq!(cmd.opt_cfgs()[0].is_array, false);
assert_eq!(cmd.opt_cfgs()[0].defaults, None);
assert_eq!(cmd.opt_cfgs()[0].desc, "");
assert_eq!(cmd.opt_cfgs()[0].arg_in_help, "");
assert_eq!(cmd.opt_cfgs()[1].store_key, "");
assert_eq!(cmd.opt_cfgs()[1].names, &["bar"]);
assert_eq!(cmd.opt_cfgs()[1].has_arg, true);
assert_eq!(cmd.opt_cfgs()[1].is_array, true);
assert_eq!(cmd.opt_cfgs()[1].defaults, None);
assert_eq!(cmd.opt_cfgs()[1].desc, "");
assert_eq!(cmd.opt_cfgs()[1].arg_in_help, "");
}

#[test]
Expand Down Expand Up @@ -891,10 +894,10 @@ mod tests_of_cmd {
}

let cmd = move_cmd();
assert_eq!(cmd.name, "app");
assert_eq!(cmd.args, &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opts.get("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opts.get("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opt_args("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opt_args("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(
cmd._leaked_strs,
&[
Expand All @@ -910,21 +913,21 @@ mod tests_of_cmd {
"bar",
]
);
assert_eq!(cmd.cfgs.len(), 2);
assert_eq!(cmd.cfgs[0].store_key, "");
assert_eq!(cmd.cfgs[0].names, &["foo"]);
assert_eq!(cmd.cfgs[0].has_arg, false);
assert_eq!(cmd.cfgs[0].is_array, false);
assert_eq!(cmd.cfgs[0].defaults, None);
assert_eq!(cmd.cfgs[0].desc, "");
assert_eq!(cmd.cfgs[0].arg_in_help, "");
assert_eq!(cmd.cfgs[1].store_key, "");
assert_eq!(cmd.cfgs[1].names, &["bar"]);
assert_eq!(cmd.cfgs[1].has_arg, true);
assert_eq!(cmd.cfgs[1].is_array, true);
assert_eq!(cmd.cfgs[1].defaults, None);
assert_eq!(cmd.cfgs[1].desc, "");
assert_eq!(cmd.cfgs[1].arg_in_help, "");
assert_eq!(cmd.opt_cfgs().len(), 2);
assert_eq!(cmd.opt_cfgs()[0].store_key, "");
assert_eq!(cmd.opt_cfgs()[0].names, &["foo"]);
assert_eq!(cmd.opt_cfgs()[0].has_arg, false);
assert_eq!(cmd.opt_cfgs()[0].is_array, false);
assert_eq!(cmd.opt_cfgs()[0].defaults, None);
assert_eq!(cmd.opt_cfgs()[0].desc, "");
assert_eq!(cmd.opt_cfgs()[0].arg_in_help, "");
assert_eq!(cmd.opt_cfgs()[1].store_key, "");
assert_eq!(cmd.opt_cfgs()[1].names, &["bar"]);
assert_eq!(cmd.opt_cfgs()[1].has_arg, true);
assert_eq!(cmd.opt_cfgs()[1].is_array, true);
assert_eq!(cmd.opt_cfgs()[1].defaults, None);
assert_eq!(cmd.opt_cfgs()[1].desc, "");
assert_eq!(cmd.opt_cfgs()[1].arg_in_help, "");
}

#[test]
Expand Down Expand Up @@ -953,10 +956,10 @@ mod tests_of_cmd {
}

let cmd = move_cmd();
assert_eq!(cmd.name, "app");
assert_eq!(cmd.args, &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opts.get("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opts.get("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(cmd.name(), "app");
assert_eq!(cmd.args(), &["baz", "qux", "quux", "corge"]);
assert_eq!(cmd.opt_args("foo").unwrap(), &Vec::<&str>::new());
assert_eq!(cmd.opt_args("bar").unwrap(), &["ABC", "DEF"]);
assert_eq!(
cmd._leaked_strs,
&[
Expand All @@ -972,21 +975,21 @@ mod tests_of_cmd {
"bar",
]
);
assert_eq!(cmd.cfgs.len(), 2);
assert_eq!(cmd.cfgs[0].store_key, "");
assert_eq!(cmd.cfgs[0].names, &["foo"]);
assert_eq!(cmd.cfgs[0].has_arg, false);
assert_eq!(cmd.cfgs[0].is_array, false);
assert_eq!(cmd.cfgs[0].defaults, None);
assert_eq!(cmd.cfgs[0].desc, "");
assert_eq!(cmd.cfgs[0].arg_in_help, "");
assert_eq!(cmd.cfgs[1].store_key, "");
assert_eq!(cmd.cfgs[1].names, &["bar"]);
assert_eq!(cmd.cfgs[1].has_arg, true);
assert_eq!(cmd.cfgs[1].is_array, true);
assert_eq!(cmd.cfgs[1].defaults, None);
assert_eq!(cmd.cfgs[1].desc, "");
assert_eq!(cmd.cfgs[1].arg_in_help, "");
assert_eq!(cmd.opt_cfgs().len(), 2);
assert_eq!(cmd.opt_cfgs()[0].store_key, "");
assert_eq!(cmd.opt_cfgs()[0].names, &["foo"]);
assert_eq!(cmd.opt_cfgs()[0].has_arg, false);
assert_eq!(cmd.opt_cfgs()[0].is_array, false);
assert_eq!(cmd.opt_cfgs()[0].defaults, None);
assert_eq!(cmd.opt_cfgs()[0].desc, "");
assert_eq!(cmd.opt_cfgs()[0].arg_in_help, "");
assert_eq!(cmd.opt_cfgs()[1].store_key, "");
assert_eq!(cmd.opt_cfgs()[1].names, &["bar"]);
assert_eq!(cmd.opt_cfgs()[1].has_arg, true);
assert_eq!(cmd.opt_cfgs()[1].is_array, true);
assert_eq!(cmd.opt_cfgs()[1].defaults, None);
assert_eq!(cmd.opt_cfgs()[1].desc, "");
assert_eq!(cmd.opt_cfgs()[1].arg_in_help, "");
}
}
}
2 changes: 1 addition & 1 deletion src/parse/parse_for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Cmd<'_> {
/// Err(err) => panic!("Invalid option: {}", err.option()),
/// }
///
/// let opt_cfgs = &cmd.cfgs;
/// let cfgs = cmd.opt_cfgs();
/// ```
pub fn parse_for<T: OptStore>(&mut self, opt_store: &mut T) -> Result<(), InvalidOption> {
let cfgs = opt_store.make_opt_cfgs();
Expand Down
3 changes: 1 addition & 2 deletions src/parse/parse_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::errors::InvalidOption;
use crate::Cmd;
use crate::OptCfg;
use std::collections::HashMap;
use std::mem;

impl<'a> Cmd<'a> {
/// Parses command line arguments with option configurations.
Expand Down Expand Up @@ -61,7 +60,7 @@ impl<'a> Cmd<'a> {
/// ```
pub fn parse_with(&mut self, opt_cfgs: Vec<OptCfg>) -> Result<(), InvalidOption> {
let result = self._parse_with(&opt_cfgs);
let _ = mem::replace(&mut self.cfgs, opt_cfgs);
self.cfgs = opt_cfgs;
result
}

Expand Down
16 changes: 8 additions & 8 deletions tests/parse_for_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ mod tests_of_parse_for {
assert_eq!(cmd.opt_arg("foo_bar"), None);
assert_eq!(cmd.opt_args("foo_bar"), Some(&[] as &[&str]));

assert_eq!(cmd.cfgs.len(), 1);
assert_eq!(cmd.cfgs[0].store_key, "foo_bar");
assert_eq!(cmd.opt_cfgs().len(), 1);
assert_eq!(cmd.opt_cfgs()[0].store_key, "foo_bar");
assert_eq!(
cmd.cfgs[0].names,
cmd.opt_cfgs()[0].names,
["f".to_string(), "b".to_string(), "foo-bar".to_string()]
);
assert_eq!(cmd.cfgs[0].has_arg, false);
assert_eq!(cmd.cfgs[0].is_array, false);
assert_eq!(cmd.cfgs[0].defaults, None);
assert_eq!(cmd.cfgs[0].desc, "The FooBar flag".to_string());
assert_eq!(cmd.cfgs[0].arg_in_help, "".to_string());
assert_eq!(cmd.opt_cfgs()[0].has_arg, false);
assert_eq!(cmd.opt_cfgs()[0].is_array, false);
assert_eq!(cmd.opt_cfgs()[0].defaults, None);
assert_eq!(cmd.opt_cfgs()[0].desc, "The FooBar flag".to_string());
assert_eq!(cmd.opt_cfgs()[0].arg_in_help, "".to_string());
}
}
6 changes: 3 additions & 3 deletions tests/parse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod tests_of_parse {
}
println!("cmd = {cmd:?}");
assert!(cmd.name().starts_with("parse_test-"));
assert!(cmd.cfgs.is_empty());
assert!(cmd.opt_cfgs().is_empty());
}

#[test]
Expand All @@ -37,7 +37,7 @@ mod tests_of_parse {
assert_eq!(cmd.has_opt("baz"), true);
assert_eq!(cmd.opt_arg("baz"), None);
assert_eq!(cmd.opt_args("baz"), Some(&[] as &[&str]));
assert!(cmd.cfgs.is_empty());
assert!(cmd.opt_cfgs().is_empty());
}

#[test]
Expand All @@ -63,7 +63,7 @@ mod tests_of_parse {
assert_eq!(cmd.has_opt("baz"), true);
assert_eq!(cmd.opt_arg("baz"), None);
assert_eq!(cmd.opt_args("baz"), Some(&[] as &[&str]));
assert!(cmd.cfgs.is_empty());
assert!(cmd.opt_cfgs().is_empty());
}
}

Expand Down
Loading

0 comments on commit 0f54c42

Please sign in to comment.