@@ -4,6 +4,16 @@ use std::num::ParseIntError;
4
4
use getopts;
5
5
6
6
7
+ /// A list of legal choices for an argument-taking option
8
+ #[ derive( PartialEq , Debug ) ]
9
+ pub struct Choices ( Vec < & ' static str > ) ;
10
+
11
+ impl fmt:: Display for Choices {
12
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
13
+ write ! ( f, "(choices: {})" , self . 0 . join( " " ) )
14
+ }
15
+ }
16
+
7
17
/// A **misfire** is a thing that can happen instead of listing files -- a
8
18
/// catch-all for anything outside the program’s normal execution.
9
19
#[ derive( PartialEq , Debug ) ]
@@ -12,6 +22,9 @@ pub enum Misfire {
12
22
/// The getopts crate didn’t like these arguments.
13
23
InvalidOptions ( getopts:: Fail ) ,
14
24
25
+ /// The user supplied an illegal choice to an argument
26
+ BadArgument ( getopts:: Fail , Choices ) ,
27
+
15
28
/// The user asked for help. This isn’t strictly an error, which is why
16
29
/// this enum isn’t named Error!
17
30
Help ( String ) ,
@@ -46,8 +59,10 @@ impl Misfire {
46
59
/// argument. This has to use one of the `getopts` failure
47
60
/// variants--it’s meant to take just an option name, rather than an
48
61
/// option *and* an argument, but it works just as well.
49
- pub fn bad_argument ( option : & str , otherwise : & str ) -> Misfire {
50
- Misfire :: InvalidOptions ( getopts:: Fail :: UnrecognizedOption ( format ! ( "--{} {}" , option, otherwise) ) )
62
+ pub fn bad_argument ( option : & str , otherwise : & str , legal : & [ & ' static str ] ) -> Misfire {
63
+ Misfire :: BadArgument ( getopts:: Fail :: UnrecognizedOption ( format ! (
64
+ "--{} {}" ,
65
+ option, otherwise) ) , Choices ( legal. into ( ) ) )
51
66
}
52
67
}
53
68
@@ -56,14 +71,15 @@ impl fmt::Display for Misfire {
56
71
use self :: Misfire :: * ;
57
72
58
73
match * self {
59
- InvalidOptions ( ref e) => write ! ( f, "{}" , e) ,
60
- Help ( ref text) => write ! ( f, "{}" , text) ,
61
- Version => write ! ( f, "exa {}" , env!( "CARGO_PKG_VERSION" ) ) ,
62
- Conflict ( a, b) => write ! ( f, "Option --{} conflicts with option {}." , a, b) ,
63
- Useless ( a, false , b) => write ! ( f, "Option --{} is useless without option --{}." , a, b) ,
64
- Useless ( a, true , b) => write ! ( f, "Option --{} is useless given option --{}." , a, b) ,
65
- Useless2 ( a, b1, b2) => write ! ( f, "Option --{} is useless without options --{} or --{}." , a, b1, b2) ,
66
- FailedParse ( ref e) => write ! ( f, "Failed to parse number: {}" , e) ,
74
+ InvalidOptions ( ref e) => write ! ( f, "{}" , e) ,
75
+ BadArgument ( ref e, ref c) => write ! ( f, "{} {}" , e, c) ,
76
+ Help ( ref text) => write ! ( f, "{}" , text) ,
77
+ Version => write ! ( f, "exa {}" , env!( "CARGO_PKG_VERSION" ) ) ,
78
+ Conflict ( a, b) => write ! ( f, "Option --{} conflicts with option {}." , a, b) ,
79
+ Useless ( a, false , b) => write ! ( f, "Option --{} is useless without option --{}." , a, b) ,
80
+ Useless ( a, true , b) => write ! ( f, "Option --{} is useless given option --{}." , a, b) ,
81
+ Useless2 ( a, b1, b2) => write ! ( f, "Option --{} is useless without options --{} or --{}." , a, b1, b2) ,
82
+ FailedParse ( ref e) => write ! ( f, "Failed to parse number: {}" , e) ,
67
83
}
68
84
}
69
85
}
0 commit comments