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
// Copyright (c) 2021 James O. D. Hunt.
//
// SPDX-License-Identifier: Apache-2.0
//

use std::io;
use thiserror::Error;

/// The error type that represents all possible parse errors
/// and handler errors.
#[derive(Error, Debug)]
pub enum Error {
    //------------------------------
    // Incorrect API usage errors (programmer error)
    //------------------------------
    /// No handler means nothing will happen.
    #[error("no handler specified")]
    NoHandler,

    /// No registered arguments means nothing will happen.
    #[error("no arguments registered")]
    NoArgs,

    //------------------------------
    // Runtime errors (user error)
    //------------------------------
    /// User specified an unknown option.
    #[error("unknown option")]
    UnknownOpt,

    /// User specified an invalid argument that looks
    /// like an option but doesn't have a name.
    #[error("missing option name")]
    MissingOptName,

    /// Option argument was not specified.
    #[error("missing option argument")]
    MissingOptArg,

    /// An option that must be specified was not specified.
    #[error("missing required option")]
    MissingReqOpt,

    /// Positional arguments are not permitted by default.
    ///
    /// To allow them, either register an `Arg` for `POSITIONAL_HANDLER_OPT`,
    /// or set `Settings.ignore_unknown_posn_args`.
    #[error("positional arguments not allowed")]
    NoPosnArgs,

    //------------------------------
    // Limitations
    //------------------------------
    /// Options must be kept separate on the command line
    /// (`-d -a foo` rather than `-da foo`).
    #[error("option bundling not supported")]
    NoBundling,

    /// Option names can only be a single character long
    /// (`-v` rather than, say, `--verbose`).
    #[error("long options not supported")]
    NoLongOpts,

    /// Custom error that a handler can use if the other errors are not
    /// appropriate.
    #[error("handler error: {0:?}")]
    HandlerError(String),

    /// An unknown error (used for testing).
    #[error("generic error: {0:?}")]
    GenericError(String),

    /// An I/O error
    #[error("I/O error: {0:?}")]
    IOError(#[from] io::Error),
}

/// Convenience type that allows a function to be defined as returning a
/// [Result], but which only requires the success type to be specified,
/// defaulting the error type to this crates `Error` type.
pub type Result<T, E = Error> = std::result::Result<T, E>;