Skip to content

Commit

Permalink
feat(Errors): allows consumers to write to stderr and exit on error
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Sep 9, 2015
1 parent 56b95f3 commit 1e6403b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::process;
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -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
///
///
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1e6403b

Please sign in to comment.