Skip to main content

exit_with/
lib.rs

1fn content(text: &str, suffix: Option<&str>) -> String {
2  let path = std::path::Path::new(&text);
3  if path.is_file()
4    && let Ok(text) = std::fs::read_to_string(path)
5  {
6    return text;
7  }
8  format!("{}{}", text, suffix.unwrap_or_default())
9}
10
11fn p_out(text: &str) {
12  if !text.is_empty() {
13    print!("{}", content(text, Some("\n")));
14  }
15}
16
17fn p_err(text: &str) {
18  if !text.is_empty() {
19    eprint!("{}", content(text, Some("\n")));
20  }
21}
22
23pub fn run() -> i32 {
24  let mut args = std::env::args().skip(1);
25  if let Some(arg) = args.next() {
26    if let Ok(exit_code) = content(&arg, None).trim().parse::<i32>() {
27      if let Some(arg) = args.next() {
28        if exit_code == 0 {
29          p_out(&arg);
30        } else {
31          p_err(&arg);
32        }
33      }
34      for arg in args {
35        if exit_code == 0 {
36          p_err(&arg);
37        } else {
38          p_out(&arg);
39        }
40      }
41      return exit_code;
42    } else {
43      p_out(&arg);
44      for arg in args {
45        p_err(&arg);
46      }
47    }
48  }
49  0
50}