Skip to content

Commit 1e6403b

Browse files
committed
feat(Errors): allows consumers to write to stderr and exit on error
1 parent 56b95f3 commit 1e6403b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/app/errors.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::process;
12
use std::error::Error;
23
use std::fmt;
34

@@ -135,6 +136,24 @@ pub enum ClapErrorType {
135136
/// .get_matches_from_safe(vec![""]);
136137
/// ```
137138
MissingSubcommand,
139+
/// Occurs when no argument or subcommand has been supplied and
140+
/// `AppSettings::ArgRequiredElseHelp` was used
141+
///
142+
///
143+
/// # Example
144+
///
145+
/// ```no_run
146+
/// # use clap::{App, Arg, AppSettings, SubCommand};
147+
/// let result = App::new("myprog")
148+
/// .setting(AppSettings::ArgRequiredElseHelp)
149+
/// .subcommand(SubCommand::with_name("conifg")
150+
/// .about("Used for configuration")
151+
/// .arg(Arg::with_name("config_file")
152+
/// .help("The configuration file to use")
153+
/// .index(1)))
154+
/// .get_matches_from_safe(vec![""]);
155+
/// ```
156+
MissingArgumentOrSubcommand,
138157
/// Error occurs when clap find argument while is was not expecting any
139158
///
140159
///
@@ -170,6 +189,14 @@ pub struct ClapError {
170189
pub error_type: ClapErrorType,
171190
}
172191

192+
impl ClapError {
193+
/// Prints the error to `stderr` and exits with a status of `1`
194+
pub fn exit(&self) -> ! {
195+
wlnerr!("{}", self.error);
196+
process::exit(1);
197+
}
198+
}
199+
173200
impl Error for ClapError {
174201
fn description(&self) -> &str {
175202
&*self.error

0 commit comments

Comments
 (0)