From 1e6403b6a863574fa3cb6946b1fb58f034e8664c Mon Sep 17 00:00:00 2001 From: Kevin K Date: Tue, 8 Sep 2015 22:38:44 -0400 Subject: [PATCH] feat(Errors): allows consumers to write to stderr and exit on error --- src/app/errors.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/app/errors.rs b/src/app/errors.rs index c03400dafc6..3ec193bc98d 100644 --- a/src/app/errors.rs +++ b/src/app/errors.rs @@ -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