Context: I'm making a CLI application and am having trouble with error handling in terms of filtering match arms by error type.
Let's say say I have a function which returns a Box<error::Error>
as follows:
use std::error;
fn cli(cli_args: Vec<String>) -> Result<(), Box<error::Error>> {
// A lot of code that can result in a variety of errors
Ok(())
}
For argument parsing, I use the popular clap-rs library, and use the ?
operator on get_matches_from_safe
. My main function is as follows:
use std::{env, process};
fn main() {
let exit_code = match cli(env::args().collect::<Vec<_>>()) {
Ok(_) => 0,
Err(err) => {
print!("{:?}", err);
1
}
};
process::exit(exit_code);
}
Currently, whenever an error occurs from clap, a clap::Error
is generated. Is there any way to match for any clap::Error
in the Err
arm? I don't want to individually check every variant of the enum, I just want to match on any of them.