2015-09-09 02:38:44 +00:00
|
|
|
use std::process;
|
2015-09-05 21:17:32 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
/// Command line argument parser error types
|
|
|
|
#[derive(PartialEq, Debug)]
|
2015-09-04 21:32:11 +00:00
|
|
|
pub enum ClapErrorType {
|
2015-09-05 21:17:32 +00:00
|
|
|
/// Error occurs when some possible values were set, but clap found unexpected value
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug").index(1)
|
|
|
|
/// .possible_value("fast")
|
|
|
|
/// .possible_value("slow"))
|
|
|
|
/// .get_matches_from_safe(vec!["", "other"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
InvalidValue,
|
|
|
|
/// Error occurs when clap found unexpected flag or option
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::from_usage("-f, --flag 'some flag'"))
|
|
|
|
/// .get_matches_from_safe(vec!["", "--other"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
InvalidArgument,
|
|
|
|
/// Error occurs when clap found unexpected subcommand
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg, SubCommand};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .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!["", "other"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
InvalidSubcommand,
|
|
|
|
/// Error occurs when option does not allow empty values but some was found
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .empty_values(false))
|
|
|
|
/// .arg(Arg::with_name("color"))
|
|
|
|
/// .get_matches_from_safe(vec!["", "--debug", "--color"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
EmptyValue,
|
|
|
|
/// Parser inner error
|
|
|
|
OptionError,
|
|
|
|
/// Parser inner error
|
|
|
|
ArgumentError,
|
|
|
|
/// Parser inner error
|
|
|
|
ValueError,
|
|
|
|
/// Error occurs when argument got more values then were expected
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug").index(1)
|
|
|
|
/// .max_values(2))
|
|
|
|
/// .get_matches_from_safe(vec!["", "too", "much", "values"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
TooMuchValues,
|
|
|
|
/// Error occurs when argument got less values then were expected
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug").index(1)
|
|
|
|
/// .min_values(3))
|
|
|
|
/// .get_matches_from_safe(vec!["", "too", "few"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
TooFewValues,
|
|
|
|
/// Error occurs when clap find two ore more conflicting arguments
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .conflicts_with("color"))
|
|
|
|
/// .get_matches_from_safe(vec!["", "--debug", "--color"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
ArgumentConflict,
|
|
|
|
/// Error occurs when one or more required arguments missing
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .required(true))
|
|
|
|
/// .get_matches_from_safe(vec![""]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
MissingRequiredArgument,
|
|
|
|
/// Error occurs when required subcommand missing
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg, AppSettings, SubCommand};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .setting(AppSettings::SubcommandRequired)
|
|
|
|
/// .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![""]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
MissingSubcommand,
|
2015-10-01 01:45:35 +00:00
|
|
|
/// Occurs when no argument or subcommand has been supplied and
|
2015-09-09 02:38:44 +00:00
|
|
|
/// `AppSettings::ArgRequiredElseHelp` was used
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-09 02:38:44 +00:00
|
|
|
///
|
|
|
|
/// ```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,
|
2015-09-05 21:17:32 +00:00
|
|
|
/// Error occurs when clap find argument while is was not expecting any
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .get_matches_from_safe(vec!["", "--arg"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
UnexpectedArgument,
|
|
|
|
/// Error occurs when argument was used multiple times and was not set as multiple.
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .multiple(false))
|
|
|
|
/// .get_matches_from_safe(vec!["", "--debug", "--debug"]);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-07 01:07:46 +00:00
|
|
|
UnexpectedMultipleUsage,
|
2015-09-22 01:58:25 +00:00
|
|
|
/// Error occurs when argument contains invalid unicode characters
|
|
|
|
///
|
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-22 01:58:25 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use clap::{App, Arg};
|
|
|
|
/// # use std::os::unix::ffi::OsStringExt;
|
|
|
|
/// # use std::ffi::OsString;
|
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .short("u")
|
|
|
|
/// .takes_value(true))
|
2015-10-01 01:45:35 +00:00
|
|
|
/// .get_matches_from_safe(vec![OsString::from_vec(vec![0x20]),
|
2015-09-22 01:58:25 +00:00
|
|
|
/// OsString::from_vec(vec![0xE9])]);
|
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// ```
|
|
|
|
InvalidUnicode
|
2015-09-04 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 21:17:32 +00:00
|
|
|
/// Command line argument parser error
|
|
|
|
#[derive(Debug)]
|
2015-09-04 21:32:11 +00:00
|
|
|
pub struct ClapError {
|
2015-09-05 21:17:32 +00:00
|
|
|
/// Formated error message
|
|
|
|
pub error: String,
|
|
|
|
/// Command line argument parser error type
|
2015-09-07 01:07:46 +00:00
|
|
|
pub error_type: ClapErrorType,
|
2015-09-05 21:17:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 02:38:44 +00:00
|
|
|
impl ClapError {
|
|
|
|
/// Prints the error to `stderr` and exits with a status of `1`
|
|
|
|
pub fn exit(&self) -> ! {
|
|
|
|
wlnerr!("{}", self.error);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 21:17:32 +00:00
|
|
|
impl Error for ClapError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
&*self.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ClapError {
|
2015-09-07 01:07:46 +00:00
|
|
|
fn fmt(&self,
|
2015-10-01 01:45:35 +00:00
|
|
|
f: &mut fmt::Formatter)
|
2015-09-07 01:07:46 +00:00
|
|
|
-> fmt::Result {
|
2015-09-05 21:17:32 +00:00
|
|
|
write!(f, "{}", self.error)
|
|
|
|
}
|
2015-09-07 01:07:46 +00:00
|
|
|
}
|