cmd_args/
lib.rs

1mod error;
2mod group;
3mod help;
4
5pub mod arg;
6pub mod option;
7pub mod parser;
8
9pub use group::Group;
10pub use help::{HelpEntry, HelpPrinter};
11
12#[cfg(test)]
13mod tests {
14    use crate::{Group, option, arg, parser};
15
16    #[test]
17    fn simple() {
18        let group = Group::new(Box::new(|args, options| {
19            let test = args[0].str().unwrap();
20            assert_eq!(test, "I am a test text!");
21
22            let the_truth = options.get("the-truth").unwrap().int().unwrap();
23            assert_eq!(the_truth, 42);
24        }), "Simple group without nested sub-commands")
25            .add_option(option::Descriptor::new("the-truth", option::Type::Int { default: 42 }, "The truth about everything"))
26            .add_argument(arg::Descriptor::new(arg::Type::Str, "Test text"));
27
28        let args: Vec<&str> = vec!("dummy.exe", "I am a test text!");
29        let result = parser::parse_from(group, &args[..], None);
30
31        assert!(result.is_ok());
32    }
33
34    #[test]
35    fn group_alias_test() {
36        let group = Group::new(Box::new(|_, _| {
37            assert!(false);
38        }), "Simple group")
39            .add_child("test", Some(vec!("t")), Group::new(Box::new(|_, _| {
40                assert!(true);
41            }), "Group with aliases"));
42
43        let args: Vec<&str> = vec!("dummy.exe", "t");
44        let result = parser::parse_from(group, &args[..], None);
45
46        assert!(result.is_ok());
47    }
48
49    // TODO
50    // - Test help output
51    // - Test nested groups
52    // - Test all arg, option types and values
53}