-
Rust newbie alert! For complex command structures, how can I construct the handler once for multiple reuse? Testing the various commands and options would be much simpler using something like a singleton. A test would look a bit like the following (based on the example for Arg::last in clap docs for 2.33.1)
Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To test your CLI I would look into something like If you're wanting to do something closer to your example then I usually use a non-test function to setup the fn build() -> App<'static, 'static> {
App::new("prog")
.arg(Arg::with_name("first"))
.arg(Arg::with_name("second"))
.arg(Arg::with_name("third").last(true))
}
#[test]
fn verify_three_params() {
let res = build().get_matches_from_safe(vec![
"prog", "one", "--", "three"
]);
assert!(res.is_ok());
let m = res.unwrap();
assert_eq!(m.value_of("third"), Some("three"));
assert!(m.value_of("second").is_none());
} You can look in the |
Beta Was this translation helpful? Give feedback.
To test your CLI I would look into something like
assert_cli
orassert_cmd
.If you're wanting to do something closer to your example then I usually use a non-test function to setup the
App
instance each time. This is because setting up theApp
instance is very fast (ns fast), and during the parsing phase, it'll get mutated so it's best to start clean for each test. It would look something like: