Skip to main content

getargs_derive/
error.rs

1/// An option as it was written on the command line, without its leading dashes.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub enum Opt<'a> {
4    /// A short option, like `-z` (stored as `'z'`).
5    Short(char),
6    /// A long option, like `--bogus` (stored as `"bogus"`).
7    Long(&'a str),
8}
9
10impl core::fmt::Display for Opt<'_> {
11    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12        match self {
13            Opt::Short(short) => write!(f, "-{short}"),
14            Opt::Long(long) => write!(f, "--{long}"),
15        }
16    }
17}
18
19/// Errors that can occur when parsing CLI arguments with [`GetArgs`](crate::GetArgs).
20///
21/// The lifetime `'a` is the lifetime of the parsed arguments: variants describing what the
22/// user typed borrow it from the command line instead of copying it, so no allocation is needed.
23///
24/// Names of the struct's own arguments (`&'static str`) are written as in the generated help:
25/// `<INPUT>` for a positional argument, `--output <OUTPUT>` for an option taking a value
26/// (`-o <OUTPUT>` when it has no long name).
27///
28/// The enum is `#[non_exhaustive]`: new variants can be added in a minor release, so a `match` on
29/// it needs a wildcard arm.
30#[must_use]
31#[non_exhaustive]
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum Error<'a> {
34    /// A required positional argument or named option was not provided on the command line.
35    ///
36    /// The string is the argument name, e.g. `"<INPUT>"` or `"--output <OUTPUT>"`.
37    MissingArgument(&'static str),
38
39    /// An option that the struct does not define was given, e.g. `--bogus` or `-z`.
40    UnknownOption {
41        /// The unknown option, as written by the user.
42        option: Opt<'a>,
43        /// The closest long option name (without `--`) when an unknown long option looks like a
44        /// typo of a known one, e.g. `Some("verbose")` for `--verbos`.
45        suggestion: Option<&'static str>,
46    },
47
48    /// A positional argument was given but the struct expects no more of them.
49    ///
50    /// The string is the extra argument, as written by the user.
51    UnexpectedArgument(&'a str),
52
53    /// A value was attached to an option that does not take one, e.g. `--verbose=yes` for a flag.
54    ///
55    /// The option is the one written by the user.
56    UnexpectedValue(Opt<'a>),
57
58    /// An option taking a value was given without one, e.g. `--output` at the end of the
59    /// command line.
60    ///
61    /// The string is the option name, e.g. `"--output <OUTPUT>"`.
62    MissingOptionValue(&'static str),
63
64    /// Two arguments that cannot be used together were both given, because of `conflicts_with`
65    /// or because they belong to the same group.
66    ArgumentConflict {
67        /// The argument declaring the conflict (or the first member of the group), e.g.
68        /// `"--quiet"`.
69        name: &'static str,
70        /// The other argument, e.g. `"--verbose"`.
71        other: &'static str,
72    },
73
74    /// An argument was given without another argument it `requires`.
75    MissingRequiredArgument {
76        /// The missing argument, e.g. `"--output <OUTPUT>"`.
77        name: &'static str,
78        /// The given argument that requires it, e.g. `"--compress"`.
79        required_by: &'static str,
80    },
81
82    /// None of a set of arguments was given although one of them is required, because of
83    /// `required_unless_present` or a `required` group.
84    ///
85    /// The slice lists the arguments, e.g. `["--config <CONFIG>", "--stdin"]`.
86    MissingOneOf(&'static [&'static str]),
87
88    /// A required subcommand was not given.
89    ///
90    /// The slice lists the visible subcommands, e.g. `["add", "remove"]`. It is only returned for
91    /// a `#[command(subcommand)]` field that is not an `Option`.
92    MissingSubcommand(&'static [&'static str]),
93
94    /// A positional argument was given where a subcommand was expected, and it is not the name
95    /// or an alias of any subcommand.
96    UnknownSubcommand {
97        /// The unknown subcommand, as written by the user.
98        name: &'a str,
99        /// The closest visible subcommand name or alias when the unknown one looks like a typo of
100        /// it, e.g. `Some("remove")` for `remov`.
101        suggestion: Option<&'static str>,
102    },
103
104    /// The underlying `getargs` parser reported an error that this crate does not recognise.
105    ///
106    /// `getargs` may add error kinds in a compatible release; they are reported as this variant
107    /// instead of panicking. It is never returned with the `getargs` 0.5 releases known to this
108    /// crate.
109    InvalidArguments,
110
111    /// A value could not be parsed into the target field type, e.g. `abc` for a `u32` field.
112    ///
113    /// This is also returned when a `default_value` string does not parse.
114    InvalidValue {
115        /// The argument name, e.g. `"--port <PORT>"` or `"<COUNT>"`.
116        name: &'static str,
117        /// The value that failed to parse.
118        value: &'a str,
119    },
120
121    /// A value is not one of the argument's `possible_values`, e.g. `turbo` for
122    /// `possible_values = ["fast", "slow"]`.
123    ///
124    /// This is also returned when a `default_value` string is not one of them.
125    InvalidPossibleValue {
126        /// The argument name, e.g. `"--mode <MODE>"`.
127        name: &'static str,
128        /// The value that was given.
129        value: &'a str,
130        /// The accepted values, in declaration order, e.g. `["fast", "slow"]`.
131        possible_values: &'static [&'static str],
132    },
133
134    /// A value parsed into the field type but is outside the argument's `value_parser` range,
135    /// e.g. `0` for `value_parser = 1..=64`.
136    ///
137    /// This is also returned when a `default_value` string is outside the range.
138    ValueOutOfRange {
139        /// The argument name, e.g. `"--jobs <JOBS>"`.
140        name: &'static str,
141        /// The value that was given.
142        value: &'a str,
143        /// The range as written in the attribute, e.g. `"1..=64"`.
144        range: &'static str,
145    },
146
147    /// More values were given to a repeated argument than its collection can hold, or than its
148    /// `num_args` range accepts, e.g. a third `-I` for a `Values<&str, 2>` field.
149    ///
150    /// This is also returned when an environment variable or a default splits into too many
151    /// values.
152    TooManyValues {
153        /// The argument name, e.g. `"--include <INCLUDE>"` or `"<FILES>..."`.
154        name: &'static str,
155        /// The first value that did not fit.
156        value: &'a str,
157        /// The maximum number of values.
158        max: usize,
159    },
160
161    /// A positional argument collecting several values received fewer than the minimum of its
162    /// `num_args` range, but at least one. [`Error::MissingArgument`] is returned when it received
163    /// none.
164    TooFewValues {
165        /// The argument name, e.g. `"<FILES>..."`.
166        name: &'static str,
167        /// The minimum number of values.
168        min: usize,
169        /// The number of values given.
170        actual: usize,
171    },
172
173    /// An argument that is not valid UTF-8 was given where text is required, when parsing raw
174    /// bytes with `from_bytes`, `parse_bytes` or `from_args_os`.
175    ///
176    /// It is returned for a value of a `&str` field or of a field parsed from a string, and for an
177    /// argument that matches no field, such as an option name, a subcommand name or an extra
178    /// positional argument, since those are reported as text by the other variants.
179    InvalidUtf8 {
180        /// The argument name, e.g. `"--name <NAME>"`, or `None` for an argument that matches no
181        /// field.
182        name: Option<&'static str>,
183        /// The raw bytes of the argument, displayed with [`Lossy`](crate::Lossy).
184        value: &'a [u8],
185    },
186
187    /// Help was requested with `-h` or `--help`.
188    ///
189    /// This is not a parse failure: the string is the generated help text, the struct's `HELP`
190    /// constant for `-h` and its `LONG_HELP` constant for `--help`. After a subcommand, it is the
191    /// help of that subcommand instead. Applications should print it to standard output and exit
192    /// successfully.
193    /// It is never returned when help is disabled with `#[command(disable_help_flag)]`.
194    Help(&'static str),
195
196    /// The version was requested with `-V` or `--version`.
197    ///
198    /// This is not a parse failure: the string is the version line, the command name followed by
199    /// the version (the struct's `VERSION_TEXT` constant). Applications should print it to standard
200    /// output and exit successfully. It is only returned when `#[command(version)]` is set.
201    Version(&'static str),
202}
203
204impl<'a> Error<'a> {
205    /// Returns `true` if this is [`Error::Help`], i.e. the user asked for help rather than
206    /// passing invalid arguments.
207    #[must_use]
208    pub fn is_help(&self) -> bool {
209        matches!(self, Error::Help(_))
210    }
211
212    /// Returns `true` if this is [`Error::Version`], i.e. the user asked for the version rather
213    /// than passing invalid arguments.
214    #[must_use]
215    pub fn is_version(&self) -> bool {
216        matches!(self, Error::Version(_))
217    }
218
219    /// Pairs the error with the usage of the command, so that its `Display` ends with the usage
220    /// line and a hint to try `--help`, as Clap errors do. See [`WithUsage`].
221    ///
222    /// The usage of a struct deriving `GetArgs` is its `USAGE` constant. For an error raised
223    /// by a subcommand it is the usage of the struct holding the subcommand, not of the
224    /// subcommand.
225    ///
226    /// ```
227    /// # use getargs_derive::GetArgs;
228    /// #[derive(Debug, GetArgs)]
229    /// #[command(name = "copy")]
230    /// struct Cli<'a> {
231    ///     input: &'a str,
232    /// }
233    ///
234    /// let error = Cli::from_args(["a", "b"].into_iter()).unwrap_err();
235    /// assert_eq!(
236    ///     error.with_usage(Cli::USAGE).to_string(),
237    ///     "unexpected argument 'b' found\n\nUsage: copy [OPTIONS] <INPUT>\n\nFor more information, try '--help'."
238    /// );
239    /// ```
240    pub const fn with_usage(self, usage: Usage) -> WithUsage<'a> {
241        WithUsage { error: self, usage }
242    }
243}
244
245impl core::fmt::Display for Error<'_> {
246    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
247        match self {
248            Error::MissingArgument(name) => {
249                write!(
250                    f,
251                    "the following required argument was not provided: {name}"
252                )
253            }
254            Error::UnknownOption { option, suggestion } => {
255                write!(f, "unexpected argument '{option}' found")?;
256                match suggestion {
257                    Some(suggestion) => {
258                        write!(f, "\n\n  tip: a similar argument exists: '--{suggestion}'")
259                    }
260                    None => Ok(()),
261                }
262            }
263            Error::UnexpectedArgument(argument) => {
264                write!(f, "unexpected argument '{argument}' found")
265            }
266            Error::UnexpectedValue(option) => {
267                write!(
268                    f,
269                    "unexpected value for '{option}': it does not take a value"
270                )
271            }
272            Error::ArgumentConflict { name, other } => {
273                write!(f, "the argument '{name}' cannot be used with '{other}'")
274            }
275            Error::MissingRequiredArgument { name, required_by } => {
276                write!(
277                    f,
278                    "the following required argument was not provided: {name} (required by '{required_by}')"
279                )
280            }
281            Error::MissingOneOf(names) => {
282                f.write_str("the following required arguments were not provided: <")?;
283                for (index, name) in names.iter().enumerate() {
284                    if index > 0 {
285                        f.write_str("|")?;
286                    }
287                    f.write_str(name)?;
288                }
289                f.write_str(">")
290            }
291            Error::MissingSubcommand(names) => {
292                f.write_str(
293                    "a subcommand is required but one was not provided\n\n  [subcommands: ",
294                )?;
295                for (index, name) in names.iter().enumerate() {
296                    if index > 0 {
297                        f.write_str(", ")?;
298                    }
299                    f.write_str(name)?;
300                }
301                f.write_str("]")
302            }
303            Error::UnknownSubcommand { name, suggestion } => {
304                write!(f, "unrecognized subcommand '{name}'")?;
305                match suggestion {
306                    Some(suggestion) => {
307                        write!(f, "\n\n  tip: a similar subcommand exists: '{suggestion}'")
308                    }
309                    None => Ok(()),
310                }
311            }
312            Error::InvalidArguments => f.write_str("the arguments could not be parsed"),
313            Error::MissingOptionValue(name) => {
314                write!(f, "a value is required for '{name}' but none was supplied")
315            }
316            Error::InvalidValue { name, value } => {
317                write!(f, "invalid value '{value}' for '{name}'")
318            }
319            Error::InvalidPossibleValue {
320                name,
321                value,
322                possible_values,
323            } => {
324                write!(
325                    f,
326                    "invalid value '{value}' for '{name}'\n  [possible values: "
327                )?;
328                for (index, possible) in possible_values.iter().enumerate() {
329                    if index > 0 {
330                        f.write_str(", ")?;
331                    }
332                    if possible.is_empty() || possible.contains(char::is_whitespace) {
333                        write!(f, "{possible:?}")?;
334                    } else {
335                        f.write_str(possible)?;
336                    }
337                }
338                f.write_str("]")
339            }
340            Error::ValueOutOfRange { name, value, range } => {
341                write!(
342                    f,
343                    "invalid value '{value}' for '{name}': {value} is not in {range}"
344                )
345            }
346            Error::TooManyValues { name, value, .. } => {
347                write!(
348                    f,
349                    "unexpected value '{value}' for '{name}' found; no more were expected"
350                )
351            }
352            Error::TooFewValues { name, min, actual } => {
353                let verb = if *actual == 1 { "was" } else { "were" };
354                write!(
355                    f,
356                    "{min} values required by '{name}'; only {actual} {verb} provided"
357                )
358            }
359            Error::InvalidUtf8 {
360                name: Some(name),
361                value,
362            } => {
363                write!(
364                    f,
365                    "invalid UTF-8 in value '{}' for '{name}'",
366                    crate::Lossy(value)
367                )
368            }
369            Error::InvalidUtf8 { name: None, value } => {
370                write!(f, "invalid UTF-8 in argument '{}'", crate::Lossy(value))
371            }
372            Error::Help(help) => f.write_str(help),
373            Error::Version(version) => f.write_str(version),
374        }
375    }
376}
377
378impl core::error::Error for Error<'_> {}
379
380/// The usage line of a command and whether it has a help flag, to remind the user of both after
381/// an argument error, as Clap does.
382///
383/// The `GetArgs` derive generates one as the `USAGE` constant of the struct, built at compile
384/// time without allocation. Its `Display` shows the usage line alone, e.g.
385/// `Usage: copy [OPTIONS] <INPUT>`. Pass it to [`Error::with_usage`] to show it after an error.
386#[derive(Debug, Clone, Copy, PartialEq, Eq)]
387#[non_exhaustive]
388pub struct Usage {
389    /// The usage line, starting with `Usage: ` and without a trailing newline.
390    pub line: &'static str,
391    /// Whether the command has a help flag, which the hint after the usage line refers to.
392    pub help_flag: bool,
393}
394
395impl Usage {
396    /// A usage with the line `line` and a help flag.
397    #[must_use]
398    pub const fn new(line: &'static str) -> Self {
399        Usage {
400            line,
401            help_flag: true,
402        }
403    }
404
405    /// Sets [`help_flag`](Self::help_flag).
406    #[must_use]
407    pub const fn help_flag(mut self, help_flag: bool) -> Self {
408        self.help_flag = help_flag;
409        self
410    }
411}
412
413impl core::fmt::Display for Usage {
414    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
415        f.write_str(self.line)
416    }
417}
418
419/// An [`Error`] followed by a usage line and a help hint, returned by [`Error::with_usage`].
420///
421/// Its `Display` writes the message of the error, a blank line, the usage line, another blank
422/// line and `For more information, try '--help'.` (left out when the command has no help flag),
423/// like the end of a Clap error:
424///
425/// ```text
426/// invalid value 'x' for '--mode <MODE>'
427///   [possible values: fast, slow]
428///
429/// Usage: run [OPTIONS]
430///
431/// For more information, try '--help'.
432/// ```
433///
434/// [`Error::Help`] and [`Error::Version`] are not failures and are written alone.
435#[must_use]
436#[derive(Debug, Clone, Copy, PartialEq, Eq)]
437pub struct WithUsage<'a> {
438    error: Error<'a>,
439    usage: Usage,
440}
441
442impl<'a> WithUsage<'a> {
443    /// The error being reported.
444    pub const fn error(&self) -> &Error<'a> {
445        &self.error
446    }
447
448    /// The usage shown after the error.
449    pub const fn usage(&self) -> &Usage {
450        &self.usage
451    }
452}
453
454impl core::fmt::Display for WithUsage<'_> {
455    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
456        write!(f, "{}", self.error)?;
457        if self.error.is_help() || self.error.is_version() {
458            return Ok(());
459        }
460        write!(f, "\n\n{}", self.usage)?;
461        if self.usage.help_flag {
462            f.write_str("\n\nFor more information, try '--help'.")?;
463        }
464        Ok(())
465    }
466}
467
468impl core::error::Error for WithUsage<'_> {}