2015-09-09 02:38:44 +00:00
|
|
|
use std::process;
|
2016-01-11 08:59:56 +00:00
|
|
|
use std::error::Error as StdError;
|
2015-10-30 03:49:39 +00:00
|
|
|
use std::fmt as std_fmt;
|
2016-01-11 08:59:56 +00:00
|
|
|
use std::fmt::Display;
|
2015-10-30 03:49:39 +00:00
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::convert::From;
|
2016-01-11 08:59:56 +00:00
|
|
|
use std::result::Result as StdResult;
|
2015-10-30 03:49:39 +00:00
|
|
|
|
|
|
|
use fmt::Format;
|
2016-01-11 08:59:56 +00:00
|
|
|
use suggestions;
|
|
|
|
use args::any_arg::AnyArg;
|
2015-10-30 03:49:39 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
pub type Result<T> = StdResult<T, Error>;
|
2015-09-05 21:17:32 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Command line argument parser error
|
2015-10-31 05:19:24 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub enum ErrorKind {
|
|
|
|
/// Occurs when an `Arg` has a set of possible values, and the user provides a value which
|
|
|
|
/// isn't in that set.
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("speed")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .possible_value("fast")
|
|
|
|
/// .possible_value("slow"))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "other"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
2016-01-22 04:18:52 +00:00
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidValue);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
InvalidValue,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when a user provides a flag, option, or argument which wasn't defined
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::from_usage("--flag 'some flag'"))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--other"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::UnknownArgument);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2016-01-11 08:59:56 +00:00
|
|
|
UnknownArgument,
|
|
|
|
/// Occurs when the user provids an unrecognized subcommand which meets the threshold for being
|
|
|
|
/// similar enough to an existing subcommand so as to not cause the more general
|
|
|
|
/// `UnknownArgument` error
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind, SubCommand};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2015-11-01 14:02:37 +00:00
|
|
|
/// .subcommand(SubCommand::with_name("config")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .about("Used for configuration")
|
|
|
|
/// .arg(Arg::with_name("config_file")
|
|
|
|
/// .help("The configuration file to use")
|
|
|
|
/// .index(1)))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "other"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidSubcommand);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
InvalidSubcommand,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides an empty value for an option that does not allow empty values
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .empty_values(false))
|
2016-01-21 05:18:53 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--debug", ""]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::EmptyValue);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
EmptyValue,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides a value for an argument with a custom validation and the
|
|
|
|
/// value fails that validation.
|
2015-10-05 00:09:12 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2016-01-11 08:59:56 +00:00
|
|
|
/// fn is_numeric(val: String) -> Result<(), String> {
|
|
|
|
/// match val.parse::<i64>() {
|
|
|
|
/// Ok(..) => Ok(()),
|
|
|
|
/// Err(..) => Err(String::from("Value wasn't a number!")),
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2015-10-05 00:09:12 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("num")
|
|
|
|
/// .validator(is_numeric))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "NotANumber"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::ValueValidation);
|
2015-10-05 00:09:12 +00:00
|
|
|
/// ```
|
2016-01-11 08:59:56 +00:00
|
|
|
ValueValidation,
|
|
|
|
/// Occurs when a user provides more values for an argument than were defined by setting
|
|
|
|
/// `Arg::max_values`
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("some_opt")
|
|
|
|
/// .long("opt")
|
|
|
|
/// .takes_value(true)
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .max_values(2))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--opt", "too", "many", "values"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::TooManyValues);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-10-05 00:09:12 +00:00
|
|
|
TooManyValues,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides fewer values for an argument than were defined by setting
|
|
|
|
/// `Arg::min_values`
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("some_opt")
|
|
|
|
/// .long("opt")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .min_values(3))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--opt", "too", "few"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::TooFewValues);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
TooFewValues,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides a different number of values for an argument than what's
|
|
|
|
/// been defined by setting `Arg::number_of_values`
|
2015-10-05 00:09:12 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-10-05 00:09:12 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("some_opt")
|
|
|
|
/// .long("opt")
|
|
|
|
/// .takes_value(true)
|
|
|
|
/// .number_of_values(2))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--opt", "wrong", "number", "of", "vals"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::WrongNumberOfValues);
|
2015-10-05 00:09:12 +00:00
|
|
|
/// ```
|
2016-01-11 08:59:56 +00:00
|
|
|
WrongNumberOfValues,
|
|
|
|
/// Occurs when the user provides two values which conflict and can't be used together.
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .long("debug")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .conflicts_with("color"))
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .arg(Arg::with_name("color")
|
|
|
|
/// .long("color"))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--debug", "--color"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::ArgumentConflict);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
ArgumentConflict,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user does not provide one or more required arguments
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .required(true))
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog"]);
|
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
MissingRequiredArgument,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when a subcommand is required (as defined by `AppSettings::SubcommandRequired`), but
|
|
|
|
/// the user does not provide one
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, AppSettings, ErrorKind, SubCommand};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .setting(AppSettings::SubcommandRequired)
|
2015-11-01 14:02:37 +00:00
|
|
|
/// .subcommand(SubCommand::with_name("config")
|
2015-09-05 22:44:20 +00:00
|
|
|
/// .about("Used for configuration")
|
|
|
|
/// .arg(Arg::with_name("config_file")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .help("The configuration file to use")))
|
|
|
|
/// .get_matches_from_safe(vec!["myprog"]);
|
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingSubcommand);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-05 21:17:32 +00:00
|
|
|
MissingSubcommand,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when either an argument or subcommand is required, as defined by
|
|
|
|
/// `AppSettings::ArgRequiredElseHelp` but the user did not provide one
|
2015-09-09 02:38:44 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-09 02:38:44 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, AppSettings, ErrorKind, SubCommand};
|
2015-09-09 02:38:44 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .setting(AppSettings::ArgRequiredElseHelp)
|
2015-11-01 14:02:37 +00:00
|
|
|
/// .subcommand(SubCommand::with_name("config")
|
2015-09-09 02:38:44 +00:00
|
|
|
/// .about("Used for configuration")
|
|
|
|
/// .arg(Arg::with_name("config_file")
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .help("The configuration file to use")))
|
|
|
|
/// .get_matches_from_safe(vec!["myprog"]);
|
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingArgumentOrSubcommand);
|
2015-09-09 02:38:44 +00:00
|
|
|
/// ```
|
|
|
|
MissingArgumentOrSubcommand,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides an argument multiple times which has not been set to allow
|
|
|
|
/// multiple uses.
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-05 21:17:32 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-09-05 22:44:20 +00:00
|
|
|
/// let result = App::new("myprog")
|
|
|
|
/// .arg(Arg::with_name("debug")
|
|
|
|
/// .multiple(false))
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--debug", "--debug"]);
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::UnexpectedMultipleUsage);
|
2015-09-05 22:12:46 +00:00
|
|
|
/// ```
|
2015-09-07 01:07:46 +00:00
|
|
|
UnexpectedMultipleUsage,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Occurs when the user provides a value containing invalid UTF-8 for an argument and
|
|
|
|
/// `AppSettings::StrictUtf8` is set.
|
|
|
|
///
|
|
|
|
/// # Platform Speicific
|
2015-09-22 01:58:25 +00:00
|
|
|
///
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Non-Windows platforms only (such as Linux, Unix, OSX, etc.)
|
2015-09-22 01:58:25 +00:00
|
|
|
///
|
2015-09-22 02:06:15 +00:00
|
|
|
/// # Examples
|
2015-09-22 01:58:25 +00:00
|
|
|
///
|
2015-12-18 07:25:02 +00:00
|
|
|
/// ```ignore
|
2016-01-22 17:58:56 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind, AppSettings};
|
2015-09-22 01:58:25 +00:00
|
|
|
/// # use std::os::unix::ffi::OsStringExt;
|
|
|
|
/// # use std::ffi::OsString;
|
|
|
|
/// let result = App::new("myprog")
|
2016-01-22 17:58:56 +00:00
|
|
|
/// .setting(AppSettings::StrictUtf8)
|
|
|
|
/// .arg(Arg::with_name("utf8")
|
2015-09-22 01:58:25 +00:00
|
|
|
/// .short("u")
|
|
|
|
/// .takes_value(true))
|
2016-01-11 08:59:56 +00:00
|
|
|
/// .get_matches_from_safe(vec![OsString::from("myprog"),
|
|
|
|
/// OsString::from("-u")
|
2016-01-22 17:58:56 +00:00
|
|
|
/// OsString::from_vec(vec![0xE9])]);
|
2015-09-22 01:58:25 +00:00
|
|
|
/// assert!(result.is_err());
|
2016-01-11 08:59:56 +00:00
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
2015-09-22 01:58:25 +00:00
|
|
|
/// ```
|
2016-01-11 08:59:56 +00:00
|
|
|
InvalidUtf8,
|
|
|
|
/// Not a true "error" as it means `--help` or similar was used. The help message will be sent
|
|
|
|
/// to `stdout`.
|
2015-10-28 09:43:01 +00:00
|
|
|
///
|
2016-01-11 08:59:56 +00:00
|
|
|
/// **Note**: If the help is displayed due to an error (such as missing subcommands) it will
|
|
|
|
/// be sent to `stderr` instead of `stdout`
|
2015-10-28 09:43:01 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-10-28 09:43:01 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-22 04:18:52 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--help"]);
|
2015-10-28 09:43:01 +00:00
|
|
|
/// assert!(result.is_err());
|
2016-01-22 04:18:52 +00:00
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
2015-10-28 09:43:01 +00:00
|
|
|
/// ```
|
|
|
|
HelpDisplayed,
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Not a true "error" as it means `--version` or similar was used. The message will be sent
|
2015-10-28 13:57:47 +00:00
|
|
|
/// to `stdout`
|
2015-10-28 09:43:01 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2016-01-22 04:18:52 +00:00
|
|
|
/// # use clap::{App, Arg, ErrorKind};
|
2015-10-28 09:43:01 +00:00
|
|
|
/// let result = App::new("myprog")
|
2016-01-21 05:18:53 +00:00
|
|
|
/// .get_matches_from_safe(vec!["myprog", "--version"]);
|
2015-10-28 09:43:01 +00:00
|
|
|
/// assert!(result.is_err());
|
2016-01-22 04:18:52 +00:00
|
|
|
/// assert_eq!(result.unwrap_err().kind, ErrorKind::VersionDisplayed);
|
2015-10-28 09:43:01 +00:00
|
|
|
/// ```
|
2015-10-28 14:23:59 +00:00
|
|
|
VersionDisplayed,
|
2016-01-22 04:18:52 +00:00
|
|
|
/// Occurs when using the `value_t!` and `values_t!` macros to convert an argument value into
|
|
|
|
/// type `T`, but the argument you requested wasn't used. I.e. you asked for an argument with
|
|
|
|
/// name `config` to be converted, but `config` wasn't used by the user.
|
|
|
|
ArgumentNotFound,
|
2015-10-31 05:19:24 +00:00
|
|
|
/// Represents an I/O error, typically white writing to stderr or stdout
|
|
|
|
Io,
|
2015-11-08 15:29:45 +00:00
|
|
|
/// Represents an Rust Display Format error, typically white writing to stderr or stdout
|
|
|
|
Format,
|
2015-09-04 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
/// Command Line Argumetn Parser Error
|
2015-09-05 21:17:32 +00:00
|
|
|
#[derive(Debug)]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub struct Error {
|
2015-09-05 21:17:32 +00:00
|
|
|
/// Formated error message
|
2016-01-11 08:59:56 +00:00
|
|
|
pub message: String,
|
2015-10-31 05:19:24 +00:00
|
|
|
/// The type of error
|
2016-01-11 08:59:56 +00:00
|
|
|
pub kind: ErrorKind,
|
|
|
|
/// Any additional information passed along, such as the argument name that caused the error
|
|
|
|
pub info: Option<Vec<String>>,
|
2015-09-05 21:17:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl Error {
|
2015-10-31 05:19:24 +00:00
|
|
|
/// Should the message be written to `stdout` or not
|
|
|
|
pub fn use_stderr(&self) -> bool {
|
2016-01-11 08:59:56 +00:00
|
|
|
match self.kind {
|
|
|
|
ErrorKind::HelpDisplayed | ErrorKind::VersionDisplayed => false,
|
2015-11-09 07:22:12 +00:00
|
|
|
_ => true,
|
2015-10-31 05:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2015-09-09 02:38:44 +00:00
|
|
|
/// Prints the error to `stderr` and exits with a status of `1`
|
|
|
|
pub fn exit(&self) -> ! {
|
2015-10-31 05:19:24 +00:00
|
|
|
if self.use_stderr() {
|
2016-01-11 08:59:56 +00:00
|
|
|
wlnerr!("{}", self.message);
|
2015-11-03 03:56:05 +00:00
|
|
|
process::exit(1);
|
|
|
|
}
|
2015-10-30 03:49:39 +00:00
|
|
|
let out = io::stdout();
|
2016-01-11 08:59:56 +00:00
|
|
|
writeln!(&mut out.lock(), "{}", self.message).expect("Error writing Error to stdout");
|
2015-11-03 03:56:05 +00:00
|
|
|
process::exit(0);
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn argument_conflict<'a, 'b, A, O, U>(arg: &A, other: Option<O>, usage: U) -> Self
|
|
|
|
where A: AnyArg<'a, 'b>,
|
|
|
|
O: Into<String>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
let mut v = vec![arg.name().to_owned()];
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' cannot be used with {}\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
match other {
|
|
|
|
Some(name) => {
|
|
|
|
let n = name.into();
|
|
|
|
v.push(n.clone());
|
|
|
|
format!("'{}'", Format::Warning(n))
|
|
|
|
},
|
|
|
|
None => "one or more of the other specified arguments".to_owned(),
|
|
|
|
},
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::ArgumentConflict,
|
|
|
|
info: Some(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn empty_value<'a, 'b, A, U>(arg: &A, usage: U) -> Self
|
|
|
|
where A: AnyArg<'a, 'b>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' requires a value but none was supplied\
|
|
|
|
\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::EmptyValue,
|
|
|
|
info: Some(vec![arg.name().to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn invalid_value<'a, 'b, B, G, A, U>(bad_val: B, good_vals: &[G], arg: &A, usage: U) -> Self
|
|
|
|
where B: AsRef<str>,
|
|
|
|
G: AsRef<str> + Display,
|
|
|
|
A: AnyArg<'a, 'b>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
let suffix = suggestions::did_you_mean_suffix(bad_val.as_ref(),
|
|
|
|
good_vals.iter(),
|
|
|
|
suggestions::DidYouMeanMessageStyle::EnumValue);
|
|
|
|
|
|
|
|
let mut sorted = vec![];
|
|
|
|
for v in good_vals {
|
|
|
|
sorted.push(v.as_ref());
|
|
|
|
}
|
|
|
|
sorted.sort();
|
|
|
|
let valid_values = sorted.iter()
|
|
|
|
.fold(String::new(), |a, name| a + &format!( " {}", name)[..]);
|
|
|
|
Error {
|
|
|
|
message: format!("{} '{}' isn't a valid value for '{}'\n\t\
|
|
|
|
[values:{}]\n\
|
|
|
|
{}\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(bad_val.as_ref()),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
valid_values,
|
|
|
|
suffix.0,
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::InvalidValue,
|
|
|
|
info: Some(vec![arg.name().to_owned(), bad_val.as_ref().to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn invalid_subcommand<S, D, N, U>(subcmd: S, did_you_mean: D, name: N, usage: U) -> Self
|
|
|
|
where S: Into<String>,
|
|
|
|
D: AsRef<str> + Display,
|
|
|
|
N: Display,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
let s = subcmd.into();
|
|
|
|
Error {
|
|
|
|
message: format!("{} The subcommand '{}' wasn't recognized\n\t\
|
|
|
|
Did you mean '{}' ?\n\n\
|
|
|
|
If you believe you received this message in error, try \
|
|
|
|
re-running with '{} {} {}'\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(&*s),
|
|
|
|
Format::Good(did_you_mean.as_ref()),
|
|
|
|
name,
|
|
|
|
Format::Good("--"),
|
|
|
|
&*s,
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::InvalidSubcommand,
|
|
|
|
info: Some(vec![s]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn missing_required_argument<R, U>(required: R, usage: U) -> Self
|
|
|
|
where R: Display,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} The following required arguments were not provided:{}\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
required,
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::MissingRequiredArgument,
|
|
|
|
info: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn missing_subcommand<N, U>(name: N, usage: U) -> Self
|
|
|
|
where N: AsRef<str> + Display,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} '{}' requires a subcommand, but one was not provided\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(name),
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::MissingSubcommand,
|
|
|
|
info: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn invalid_utf8<U>(usage: U) -> Self
|
|
|
|
where U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} Invalid UTF-8 was detected in one or more arguments\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::InvalidUtf8,
|
|
|
|
info: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn too_many_values<'a, 'b, V, A, U>(val: V, arg: &A, usage: U) -> Self
|
|
|
|
where V: AsRef<str> + Display + ToOwned,
|
|
|
|
A: AnyArg<'a, 'b>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
let v = val.as_ref();
|
|
|
|
Error {
|
|
|
|
message: format!("{} The value '{}' was provided to '{}', but it wasn't expecting \
|
|
|
|
any more values\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(v),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::TooManyValues,
|
|
|
|
info: Some(vec![arg.name().to_owned(), v.to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn too_few_values<'a, 'b, A, U>(arg: &A, min_vals: u8, curr_vals: usize, usage: U) -> Self
|
|
|
|
where A: AnyArg<'a, 'b>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' requires at least {} values, but only {} w{} \
|
|
|
|
provided\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
Format::Warning(min_vals.to_string()),
|
|
|
|
Format::Warning(curr_vals.to_string()),
|
|
|
|
if curr_vals > 1 {
|
|
|
|
"ere"
|
|
|
|
} else {
|
|
|
|
"as"
|
|
|
|
},
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::TooFewValues,
|
|
|
|
info: Some(vec![arg.name().to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn value_validation(err: String) -> Self {
|
|
|
|
Error {
|
|
|
|
message: format!("{} {}", Format::Error("error:"), err),
|
|
|
|
kind: ErrorKind::ValueValidation,
|
|
|
|
info: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn wrong_number_of_values<'a, 'b, A, S, U>(arg: &A, num_vals: u8, curr_vals: usize, suffix: S, usage: U) -> Self
|
|
|
|
where A: AnyArg<'a, 'b>,
|
|
|
|
S: Display,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' requires {} values, but {} w{} \
|
|
|
|
provided\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
Format::Warning(num_vals.to_string()),
|
|
|
|
Format::Warning(curr_vals.to_string()),
|
|
|
|
suffix,
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::WrongNumberOfValues,
|
|
|
|
info: Some(vec![arg.name().to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn unexpected_multiple_usage<'a, 'b, A, U>(arg: &A, usage: U) -> Self
|
|
|
|
where A: AnyArg<'a, 'b>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' was provided more than once, but cannot \
|
|
|
|
be used multiple times\n\n\
|
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(arg.to_string()),
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::UnexpectedMultipleUsage,
|
|
|
|
info: Some(vec![arg.name().to_owned()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn unknown_argument<A, U>(arg: A, did_you_mean: &str, usage: U) -> Self
|
|
|
|
where A: Into<String>,
|
|
|
|
U: Display
|
|
|
|
{
|
|
|
|
let a = arg.into();
|
|
|
|
Error {
|
2016-01-25 20:45:50 +00:00
|
|
|
message: format!("{} Found argument '{}' which wasn't expected, or isn't valid in this context{}\n\
|
2016-01-11 08:59:56 +00:00
|
|
|
{}\n\n\
|
|
|
|
For more information try {}",
|
|
|
|
Format::Error("error:"),
|
|
|
|
Format::Warning(&*a),
|
|
|
|
if !did_you_mean.is_empty() {
|
|
|
|
format!("{}\n", did_you_mean)
|
|
|
|
} else {
|
|
|
|
"".to_owned()
|
|
|
|
},
|
|
|
|
usage,
|
|
|
|
Format::Good("--help")),
|
|
|
|
kind: ErrorKind::UnknownArgument,
|
|
|
|
info: Some(vec![a]),
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 04:18:52 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn argument_not_found<A>(arg: A) -> Self
|
|
|
|
where A: Into<String>,
|
|
|
|
{
|
|
|
|
let a = arg.into();
|
|
|
|
Error {
|
|
|
|
message: format!("{} The argument '{}' wasn't found", Format::Error("error:"), a.clone()),
|
|
|
|
kind: ErrorKind::ArgumentNotFound,
|
|
|
|
info: Some(vec![a]),
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 02:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl StdError for Error {
|
2015-09-05 21:17:32 +00:00
|
|
|
fn description(&self) -> &str {
|
2016-01-11 08:59:56 +00:00
|
|
|
&*self.message
|
2015-09-05 21:17:32 +00:00
|
|
|
}
|
2015-10-30 03:49:39 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
fn cause(&self) -> Option<&StdError> {
|
|
|
|
match self.kind {
|
2015-11-09 07:22:12 +00:00
|
|
|
_ => None,
|
2015-10-30 03:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-05 21:17:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl Display for Error {
|
2015-10-30 03:49:39 +00:00
|
|
|
fn fmt(&self, f: &mut std_fmt::Formatter) -> std_fmt::Result {
|
2016-01-11 08:59:56 +00:00
|
|
|
writeln!(f, "{}", self.message)
|
2015-09-05 21:17:32 +00:00
|
|
|
}
|
2015-09-07 01:07:46 +00:00
|
|
|
}
|
2015-10-31 05:19:24 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl From<io::Error> for Error {
|
2015-10-31 05:19:24 +00:00
|
|
|
fn from(e: io::Error) -> Self {
|
2016-01-11 08:59:56 +00:00
|
|
|
Error {
|
|
|
|
message: format!("{} {}", Format::Error("error:"), e.description()),
|
|
|
|
kind: ErrorKind::Io,
|
|
|
|
info: None,
|
2015-10-31 05:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 15:29:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
impl From<std_fmt::Error> for Error {
|
2015-11-08 15:29:45 +00:00
|
|
|
fn from(e: std_fmt::Error) -> Self {
|
2016-01-11 08:59:56 +00:00
|
|
|
Error {
|
|
|
|
message: format!("{} {}", Format::Error("error:"), e),
|
|
|
|
kind: ErrorKind::Format,
|
|
|
|
info: None,
|
2015-11-08 15:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|