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!: changed to simply move to cfgs field and add opt_cfgs method to Cmd #27

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
}
}

/// The option configurations which was used to parse command line arguments.
sttk marked this conversation as resolved.
Show resolved Hide resolved
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 opt_cfgs = &cmd.opt_cfgs();
sttk marked this conversation as resolved.
Show resolved Hide resolved
/// ```
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