#die #stderr #cmd #cmdline #exit

die-exit

Handle errors and exit in command line programs easily

1 unstable release

Uses old Rust 2015

0.3.3 Jan 16, 2021
0.3.2 Jan 15, 2021
0.3.1 Jan 15, 2021
0.3.0 Jan 15, 2021

#423 in Command line utilities

Download history 456/week @ 2021-02-25 369/week @ 2021-03-04 110/week @ 2021-03-11 55/week @ 2021-03-18 253/week @ 2021-03-25 450/week @ 2021-04-01 388/week @ 2021-04-08 18/week @ 2021-04-15 5/week @ 2021-04-22 5/week @ 2021-05-13 1/week @ 2021-05-20 1/week @ 2021-05-27 3/week @ 2021-06-03 10/week @ 2021-06-10

913 downloads per month
Used in pioneerctl

MIT license

13KB
145 lines

die-exit

GitHub GitHub Workflow Status Latest Version Documentation

die-exit is a simple Rust library to make it easy to handle errors and exit in command line programs. It is forked from the die library, but includes an additional test feature which will replace exit behaviour with a call to panic! in order to facilitate testing.

Cargo.toml

[dependencies]
die-exit = "0.3"

[dev-dependencies.die-exit]
version = "0.3"
features = ["test"]

Example usage:

use die_exit::*;
// Result:
Ok(1).die("no number"); // unwraps to 1 successfully
Err("failure").die("strange error"); // prints `strange error` to stderr then exits with code 1

// Option:
Some(1).die("no number"); // unwraps to 1 successfully
None.die("none option"); // prints `none option` to stderr then exits with code 1

// custom error codes:
Err("failure").die_code("strange error", 4); // prints `strange error` to stderr then exits with code 4
None.die_code("none option", 5); // prints `none option` to stderr then exits with code 5

// die! macro:
die!("argument to -e must be numeric"); // prints message to stderr then exits with code 1
die!(2; "argument to -e must be numeric"); // prints message to stderr then exits with code 2
die!("argument to -e must be numeric"; 3); // prints message to stderr then exits with code 3
die!("argument {} must be {}", "-e", 1; 4); // prints `argument -e must be 1` to stderr then exits with code 4
die!("argument {} must be {}", "-e", 1); // prints `argument -e must be 1` to stderr then exits with code 1
die!(2); // prints nothing, only exits with code 3
die!(); // prints nothing, only exits with code 1

Example testing:

Ensure that the test feature is turned on.

#[cfg(test)]
mod tests {
    use super::*;
    use die_exit::*;

    #[test]
    #[should_panic]
    fn die_test() {
        die!("Die works in tests!"; exit_codes::COMPILER_ERROR);
    }
}

cargo Features

  • test: Turn this on if you want to run tests where die might be used. This will change the behaviour of die and its variants to call panic!() instead of process::exit().

License

This project is licensed under the MIT license (LICENSE-MIT http://opensource.org/licenses/MIT)

No runtime deps

Features

  • test