mirror of
https://github.com/clap-rs/clap
synced 2024-11-14 00:27:13 +00:00
feat(Errors): allows consumers to write to stderr and exit on error
This commit is contained in:
parent
56b95f3208
commit
1e6403b6a8
1 changed files with 27 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue