feat(Errors): allows consumers to write to stderr and exit on error

This commit is contained in:
Kevin K 2015-09-08 22:38:44 -04:00
parent 56b95f3208
commit 1e6403b6a8

View file

@ -1,3 +1,4 @@
use std::process;
use std::error::Error;
use std::fmt;
@ -135,6 +136,24 @@ pub enum ClapErrorType {
/// .get_matches_from_safe(vec![""]);
/// ```
MissingSubcommand,
/// Occurs when no argument or subcommand has been supplied and
/// `AppSettings::ArgRequiredElseHelp` was used
///
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings, SubCommand};
/// let result = App::new("myprog")
/// .setting(AppSettings::ArgRequiredElseHelp)
/// .subcommand(SubCommand::with_name("conifg")
/// .about("Used for configuration")
/// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use")
/// .index(1)))
/// .get_matches_from_safe(vec![""]);
/// ```
MissingArgumentOrSubcommand,
/// Error occurs when clap find argument while is was not expecting any
///
///
@ -170,6 +189,14 @@ pub struct ClapError {
pub error_type: ClapErrorType,
}
impl ClapError {
/// Prints the error to `stderr` and exits with a status of `1`
pub fn exit(&self) -> ! {
wlnerr!("{}", self.error);
process::exit(1);
}
}
impl Error for ClapError {
fn description(&self) -> &str {
&*self.error