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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::{
ffi::OsString,
fmt,
io::{self, IsTerminal, Write, sink, stderr, stdout},
process,
str::Utf8Error,
};
use crate::{
Context,
desc::{ArgumentDesc, ArgumentName},
types::Color,
writer::{BOLD, DEFAULT_LINE_WIDTH, ITALIC, RED, RESET, Writer, YELLOW},
};
/// Indicates that a [`Command`] could not be parsed.
///
/// This error is returned from the methods in [`Command`] when the implementing type could not be
/// parsed from the command-line arguments.
/// This can happen when the user passes `--help` or `--version` (requesting a help or version
/// output instead of normal program operation), or when the provided arguments are invalid.
///
/// [`Command`]: crate::Command
#[derive(Debug)]
pub struct Error {
pub(crate) kind: ErrorKind,
pub(crate) context: Context,
}
impl Error {
/// Reports the error to stdout or stderr, and exits the process with an appropriate status.
pub fn report_and_exit(&self) -> ! {
let mut reporter = self.reporter();
if self.is_fatal() {
reporter = reporter.output(stderr());
} else {
reporter = reporter.output(stdout());
};
reporter.report_and_exit()
}
/// Returns a [`Reporter`] that can write this error to a terminal and allows customization.
pub fn reporter(&self) -> Reporter<'_> {
Reporter {
error: self,
color: self.context.color(),
wrap_width: DEFAULT_LINE_WIDTH,
writer: Writer::io(sink()),
}
}
fn report_impl(&self, f: &mut Writer) -> io::Result<()> {
match &self.kind {
ErrorKind::VersionRequested => {
let fmt = self.context.root_desc.0.0.version_formatter;
let version = fmt(&self.context);
write!(f, "{version}")?;
if !version.ends_with('\n') {
writeln!(f)?;
}
}
ErrorKind::HelpRequested => {
self.context
.desc
.help()
.with_invocation(&self.context.command_chain)
.write_to(f)?;
}
_ => {
write!(f, "{RED}error{RESET}: ")?;
f.set_indentation("error: ".len());
self.write_error(f)?;
write!(f, "\n\n")?;
let mut usage = self
.context
.desc
.usage()
.with_invocation(&self.context.command_chain)
.with_prefix(format!("{BOLD}usage{RESET}: "));
if let Some(arg) = self.kind.arg_index() {
usage = usage.highlight_arg(arg);
}
if self.kind.related_to_subcommand() {
usage = usage.highlight_subcommand();
}
usage.write_to(f)?;
writeln!(f)?;
}
}
f.flush()
}
fn arg(&self, index: usize) -> &ArgumentDesc {
&self.context.desc.args()[index]
}
fn argname(&self, index: usize) -> impl fmt::Display {
struct Disp<'a>(&'a ArgumentName);
impl fmt::Display for Disp<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "`{YELLOW}{ITALIC}{}{RESET}`", self.0)
}
}
Disp(self.arg(index).name())
}
fn write_error(&self, f: &mut Writer) -> io::Result<()> {
match &self.kind {
ErrorKind::HelpRequested => write!(f, "help requested"),
ErrorKind::VersionRequested => write!(f, "version requested"),
ErrorKind::Utf8Error(utf8_error) => write!(f, "{utf8_error}"),
ErrorKind::ValueParseError { error, arg } => write!(
f,
"invalid value for argument {}: {error}",
self.argname(*arg)
),
ErrorKind::UnexpectedArgValue(arg) => {
write!(f, "flag {} does not take a value", self.argname(*arg))
}
ErrorKind::UnexpectedArg { arg } => {
write!(f, "unexpected {arg}")?;
Ok(())
}
ErrorKind::DuplicateArg(arg) => {
write!(f, "duplicate argument {}", self.argname(*arg))
}
ErrorKind::MissingArg(arg) => {
write!(f, "missing argument {}", self.argname(*arg))
}
ErrorKind::MissingArgValue(arg) => {
write!(f, "argument {} requires a value", self.argname(*arg))
}
ErrorKind::MissingSubcommand => write!(f, "a subcommand is required"),
ErrorKind::UnknownSubcommand(cmd) => {
write!(f, "unknown subcommand `{}`", cmd.display())
}
}
}
fn is_fatal(&self) -> bool {
match &self.kind {
ErrorKind::HelpRequested | ErrorKind::VersionRequested => false,
_ => true,
}
}
}
/// This is the internal error type used throughout this library and by the macro-generated code.
///
/// It is only converted to a user-facing [`Error`] at the boundary, where [`Error`] can be enhanced
/// with command-specific context.
#[derive(Debug)]
pub enum ErrorKind {
HelpRequested,
VersionRequested,
Utf8Error(Utf8Error),
/// Failed to parse an argument value.
///
/// The boxed error can be [`Utf8Error`], when the type expects a valid UTF-8 input, or any
/// type-specific parsing error, such as [`ParseIntError`][std::num::ParseIntError].
///
/// The [`ErrorKind::Utf8Error`] variant indicates that invalid UTF-8 was provided as part of
/// the *name* of an argument instead its value.
ValueParseError {
arg: usize,
error: Box<dyn std::error::Error>,
},
/// Argument doesn't take a value, but `--arg=...` was passed.
UnexpectedArgValue(usize),
UnexpectedArg {
arg: String,
},
/// A non-repeating argument was provided multiple times. The [`usize`] is the argument index in
/// the `CommandDesc`.
DuplicateArg(usize),
/// A required argument was not provided. The [`usize`] is the argument index in the
/// `CommandDesc`.
MissingArg(usize),
MissingArgValue(usize),
/// A subcommand is required, but none was provided.
MissingSubcommand,
/// A subcommand was provided, and a subcommand is accepted by this command, but the name
/// doesn't match any of the supported subcommands.
UnknownSubcommand(OsString),
}
impl ErrorKind {
fn arg_index(&self) -> Option<usize> {
match self {
ErrorKind::MissingArg(arg)
| ErrorKind::MissingArgValue(arg)
| ErrorKind::DuplicateArg(arg)
| ErrorKind::UnexpectedArgValue(arg)
| ErrorKind::ValueParseError { arg, .. } => Some(*arg),
ErrorKind::HelpRequested
| ErrorKind::VersionRequested
| ErrorKind::Utf8Error(..)
| ErrorKind::UnexpectedArg { .. }
| ErrorKind::MissingSubcommand
| ErrorKind::UnknownSubcommand(..) => None,
}
}
fn related_to_subcommand(&self) -> bool {
match self {
ErrorKind::MissingSubcommand | ErrorKind::UnknownSubcommand(..) => true,
ErrorKind::MissingArg(..)
| ErrorKind::MissingArgValue(..)
| ErrorKind::DuplicateArg(..)
| ErrorKind::HelpRequested
| ErrorKind::VersionRequested
| ErrorKind::Utf8Error(..)
| ErrorKind::ValueParseError { .. }
| ErrorKind::UnexpectedArgValue { .. }
| ErrorKind::UnexpectedArg { .. } => false,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.report_impl(&mut Writer::display(f))
.map_err(|_| fmt::Error)
}
}
impl std::error::Error for Error {}
/// Success status code.
const EX_OK: i32 = 0;
/// This is the exit code we return with when parsing the command line fails.
///
/// It is taken from the Linux/BSD/OS X `sysexits.h` and described as
///
/// > The command was used incorrectly, e.g., with the wrong number of arguments,
/// > a bad flag, bad syntax in a parameter, or whatever.
const EX_USAGE: i32 = 64;
/// Writes an [`Error`] to a destination.
///
/// Returned by [`Error::reporter`].
pub struct Reporter<'a> {
error: &'a Error,
color: Color,
wrap_width: usize,
writer: Writer<'a>,
}
impl<'a> Reporter<'a> {
/// Configures whether to use ANSI colors.
///
/// By default (if this function isn't called), [`Reporter`] will respect any
/// a `--color=<always|auto|never>` argument that was encountered on the command line
/// (this mechanism requires the [`Command`][crate::Command] implementor to have an argument
/// of type [`Color`]).
///
/// If no `--color` argument was passed, and this method *isn't* called, the default is
/// [`Color::Auto`].
pub fn color(self, color: Color) -> Self {
Self { color, ..self }
}
/// Sets the maximum line width.
///
/// Lines will be soft-wrapped if they exceed this width.
///
/// By default, lines will get wrapped after an unspecified, but typically reasonable width.
pub fn wrap_width(self, width: usize) -> Self {
Self {
wrap_width: width,
..self
}
}
/// Writes output to the given destination.
///
/// The provided type has to implement [`IsTerminal`] for automatic colorization to work.
/// To output to something that does not implement [`IsTerminal`], use [`Reporter::raw_output`].
pub fn output<W: io::Write + IsTerminal + 'a>(self, w: W) -> Self {
Self {
writer: Writer::fd(w),
..self
}
}
/// Writes the error to an [`io::Write`] implementor.
///
/// This will not use ANSI colors unless colors are forcibly enabled by passing `--color=always`
/// or calling [`Reporter::color`] with [`Color::Always`].
pub fn raw_output<W: io::Write + 'a>(self, w: W) -> Self {
Self {
writer: Writer::io(w),
..self
}
}
/// Reports the error to the configured destination.
pub fn report(mut self) -> io::Result<()> {
match self.color {
Color::Never => self.writer.force_color(false),
Color::Always => self.writer.force_color(true),
_ => {}
}
self.writer.set_max_line_width(self.wrap_width);
self.error.report_impl(&mut self.writer)
}
/// Reports the error to the configured destination, and exits the process with an appropriate
/// status.
///
/// If the [`Error`] is fatal (ie. the result of an incorrect command invocation), this will
/// exit with code `EX_USAGE` (64).
/// Otherwise, the [`Error`] indicates that the user requested `--help` or `--version` output,
/// and this method will exit the process with code 0.
pub fn report_and_exit(self) -> ! {
let code = if self.error.is_fatal() {
EX_USAGE
} else {
EX_OK
};
self.report().ok();
process::exit(code);
}
}