@@ -13,13 +13,35 @@ use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite};
13
13
const NAME : & str = "dummy" ;
14
14
pub const DESCRIPTION : & str = "A dummy utility to demonstrate the framework" ;
15
15
16
+ type DummyResult < T > = :: std:: result:: Result < T , DummyError > ;
17
+
16
18
#[ derive( Fail , Debug ) ]
17
19
enum DummyError {
18
- #[ fail( display = "something wrong" ) ]
20
+ #[ fail( display = "oh no, something wrong" ) ]
19
21
SomethingWrong
20
22
}
21
23
22
- type DummyResult < T > = :: std:: result:: Result < T , DummyError > ;
24
+ struct DummyOptions {
25
+ verbose : bool
26
+ }
27
+
28
+ impl DummyOptions {
29
+ fn from_matches ( matches : & ArgMatches ) -> Self {
30
+ let mut options = Self :: default ( ) ;
31
+
32
+ options. verbose = matches. is_present ( "verbose" ) ;
33
+
34
+ options
35
+ }
36
+ }
37
+
38
+ impl Default for DummyOptions {
39
+ fn default ( ) -> Self {
40
+ Self {
41
+ verbose : false
42
+ }
43
+ }
44
+ }
23
45
24
46
struct Dummyer < O >
25
47
where
@@ -36,18 +58,23 @@ where
36
58
Dummyer { output }
37
59
}
38
60
39
- fn dummy ( & mut self ) -> DummyResult < ( ) > {
40
- writeln ! ( self . output, "write something to the output" ) ;
61
+ fn dummy ( & mut self , options : & DummyOptions ) -> DummyResult < ( ) > {
62
+ if options. verbose {
63
+ writeln ! ( self . output, "Hello, world! This is a dummy utility. I am very verbose :)" ) ;
64
+ return Err ( DummyError :: SomethingWrong )
65
+ } else {
66
+ writeln ! ( self . output, "Hello, world!" ) ;
67
+ }
41
68
Ok ( ( ) )
42
69
}
43
70
}
44
71
45
72
fn create_app ( ) -> App < ' static , ' static > {
46
73
util_app ! ( NAME )
47
- . arg ( Arg :: with_name ( "about " )
48
- . short ( "a " )
49
- . long ( "about " )
50
- . help ( "show about " ) )
74
+ . arg ( Arg :: with_name ( "verbose " )
75
+ . short ( "v " )
76
+ . long ( "verbose " )
77
+ . help ( "Say hello in verbose mode " ) )
51
78
}
52
79
53
80
pub fn execute < S , T > ( setup : & mut S , args : T ) -> Result < ( ) >
@@ -57,11 +84,12 @@ where
57
84
{
58
85
let app = create_app ( ) ;
59
86
let matches = app. get_matches_from_safe ( args) ?;
87
+ let options = DummyOptions :: from_matches ( & matches) ;
60
88
61
89
let output = setup. output ( ) ;
62
90
let mut output = output. lock ( ) ?;
63
91
64
92
let mut dummyer = Dummyer :: new ( output) ;
65
- dummyer. dummy ( ) ?;
93
+ dummyer. dummy ( & options ) ?;
66
94
Ok ( ( ) )
67
95
}
0 commit comments