Code Monkey home page Code Monkey logo

die's Introduction

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.5"

[dev-dependencies.die-exit]
version = "0.5"
features = ["test", "red"]

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

// with function (Result only):
Err("failure").die_with(|error| format!("strange error: {}", error)); // prints `strange error: failure` to stderr then exits with code 1
Err("failure").die_with(|error| (format!("strange error: {}", error), 6)); // prints `strange error: failure` to stderr then exits with code 6

// 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().
  • red: Red and bold output.

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.