1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::error::Error;
use std::fmt::Display;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Error as FmtError;
/// Returned when an error occurs during the parsing of an argument list.
#[derive(PartialEq, Eq)]
pub enum ParsingError {
/// Indicates that an option was specified which the program does not recognize.
///
/// The associated string is the name of the aforementioned option.
UnrecognizedOption(String),
/// Indicates that a specified option could mean multiple valid options.
///
/// The associated string is the name of the aformentioned option.
/// The associated vector of strings contains all the possible valid options.
AmbiguousOption(String, Vec<String>),
/// Indicates that a value was assigned to an option which does not take a value.
///
/// The associated string is the name of the aforementioned option.
AssignmentToFlag(String),
/// Indicates that a value was assigned to an option which does not take a value.
///
/// The first associated string is the name of the aforementioned option.
/// The second associated string is the name of the alias which was used to specify the option.
AssignmentToFlagAlias(String, String),
/// Indicates that a value was assigned to the same option more than once.
///
/// The associated string is the name of the aforementioned option.
ParameterDuplication(String),
/// Indicates that a value was assigned to the same option more than once.
///
/// The first associated string is the name of the aforementioned option.
/// The second associated string is the name of the alias which was used to specify the option.
ParameterDuplicationAlias(String, String),
/// Indicates that an option was specified which takes a value but didn't get one.
///
/// The associated string is the name of the aforementioned option.
MissingArgument(String),
/// Indicates that an option was specified which takes a value but didn't get one.
///
/// The first associated string is the name of the aforementioned option.
/// The second associated string is the name of the alias which was used to specify the option.
MissingArgumentAlias(String, String),
/// Indicates that a subcommand was specified which the program does not recognize.
///
/// The associated string is the name of the aforementioned subcommand.
UnrecognizedSubcommand(String),
/// Indicates that a specified subcommand could mean multiple valid subcommands.
///
/// The associated string is the name of the aformentioned subcommand.
/// The associated vector of strings contains all the possible valid subcommands.
AmbiguousSubcommand(String, Vec<String>),
/// Indicates that a required subcommand is missing from the argument list.
MissingRequiredSubcommand,
/// Indicates that one or more required options are missing from the argument list.
///
/// The associated vector of strings contains all the missing required options.
MissingRequiredParameters(Vec<String>),
}
impl Display for ParsingError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
match self {
ParsingError::UnrecognizedOption(name) => write!(f, "unrecognized option '{}'", name),
ParsingError::AmbiguousOption(name, matches) => {
write!(f, "option '{}' is ambiguous; possibilities:", name)?;
for value in matches {
write!(f, " '{}'", value)?;
}
Ok(())
},
ParsingError::AssignmentToFlag(name) => write!(f, "option '{}' doesn't allow an argument", name),
ParsingError::AssignmentToFlagAlias(name, alias) => write!(f, "option '{}' doesn't allow an argument; note that '{}' is an alias to '{0}'", name, alias),
ParsingError::ParameterDuplication(name) => write!(f, "parameter '{}' was set more than once", name),
ParsingError::ParameterDuplicationAlias(name, alias) => write!(f, "parameter '{}' was set more than once; note that '{}' is an alias to '{0}'", name, alias),
ParsingError::MissingArgument(name) => write!(f, "parameter '{}' is missing an argument", name),
ParsingError::MissingArgumentAlias(name, alias) => write!(f, "parameter '{}' is missing an argument; note that '{}' is an alias to '{0}'", name, alias),
ParsingError::UnrecognizedSubcommand(name) => write!(f, "unrecognized subcommand '{}'", name),
ParsingError::AmbiguousSubcommand(name, matches) => {
write!(f, "subcommand '{}' is ambiguous; possibilities:", name)?;
for value in matches {
write!(f, " '{}'", value)?;
}
Ok(())
},
ParsingError::MissingRequiredSubcommand => write!(f, "argument list is missing a required subcommand"),
ParsingError::MissingRequiredParameters(names) => match names.len() {
0 => write!(f, "argument list is missing a required parameter"),
1 => write!(f, "argument list is missing required parameter '{}'", names[0]),
_ => {
write!(f, "argument list is missing required parameters:")?;
for value in names {
write!(f, " '{}'", value)?;
}
Ok(())
},
},
}
}
}
impl Debug for ParsingError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
(self as &Display).fmt(f)
}
}
impl Error for ParsingError {
fn cause(&self) -> Option<&Error> {
None
}
}