pub fn run(
args: &[String],
out: &mut dyn Write,
err: &mut dyn Write,
) -> Result<ExitCode, String>Expand description
Run the CLI over args, writing to out and err.
Both streams are parameters rather than println! so that a test
or a benchmark can drive the real command path and read back what
it produced. Going through the binary instead measures process
spawn and pipe setup alongside the work, and cannot assert on
output without a subprocess.
§Errors
Returns the message the binary prints after oxml: , for usage
mistakes and unreadable inputs. A document that is merely invalid
is not an error here – it is a non-zero ExitCode.
Examples found in repository?
examples/run_a_command.rs (line 23)
9fn main() {
10 let document = std::env::temp_dir().join("oxml-cli-example.xml");
11 std::fs::write(&document, "<catalogue><book>Dune</book></catalogue>")
12 .expect("writing the example document");
13
14 let args = vec![
15 "query".to_owned(),
16 "//book".to_owned(),
17 document.to_string_lossy().into_owned(),
18 "--text".to_owned(),
19 ];
20
21 let mut out = Vec::new();
22 let mut err = Vec::new();
23 let code = oxml_cli::run(&args, &mut out, &mut err)
24 .expect("the command should run");
25
26 print!("{}", String::from_utf8_lossy(&out));
27 assert_eq!(
28 String::from_utf8_lossy(&out).trim(),
29 "Dune",
30 "querying //book --text should print the element's text"
31 );
32 assert!(matches!(code, c if c == ExitCode::SUCCESS));
33
34 std::fs::remove_file(&document).expect("removing the example document");
35}