Browse Source

Using yaml funcionality of clap. Cleanup.

Joshua Rutschmann 7 years ago
parent
commit
134727749a
3 changed files with 63 additions and 63 deletions
  1. 1
    1
      hw8/task1/Cargo.toml
  2. 50
    0
      hw8/task1/src/cli.yml
  3. 12
    62
      hw8/task1/src/main.rs

+ 1
- 1
hw8/task1/Cargo.toml View File

5
 
5
 
6
 [dependencies]
6
 [dependencies]
7
 sha2 = "0.7.0"
7
 sha2 = "0.7.0"
8
-clap = "2.28"
8
+clap = {version = "~2.29.0", features = ["yaml"]}
9
 time = "0.1"
9
 time = "0.1"
10
 sys-info = "*"
10
 sys-info = "*"

+ 50
- 0
hw8/task1/src/cli.yml View File

1
+name: "task1"
2
+args:
3
+    - base:
4
+        value_name: "base"
5
+        help: "Sets the base to use"
6
+        takes_value: true
7
+        required: true
8
+
9
+    - difficulty:
10
+        value_name: "difficulty"
11
+        help: "Sets the difficulty to use"
12
+        takes_value: true
13
+        required: true
14
+         
15
+    - threads:
16
+        value_name: "threads"
17
+        help: "Sets the number of the threads to use (default = number of cpus)"
18
+        takes_value: true
19
+        required: false
20
+
21
+    - verbose:
22
+        short: "v"
23
+        help: "Prints help information"
24
+        multiple: true
25
+        required: false
26
+             
27
+    - sync:
28
+        short: "s"
29
+        long: "sync"
30
+        help: "enables sync when solution found"
31
+        required: false
32
+
33
+    - wait:
34
+        short: "w"
35
+        long: "wait"
36
+        help: "consumer waits for all producers"
37
+        required: false
38
+
39
+    - special:
40
+        long: "special"
41
+        value_name: "VALUE"
42
+        help: "sets special sync parameter"
43
+        takes_value: true
44
+
45
+subcommands:
46
+    - timings:
47
+        about: "controls timing features"
48
+        version: "1.0"
49
+        author: "Lorenz Bung & Joshua Rutschmann"
50
+

+ 12
- 62
hw8/task1/src/main.rs View File

1
+#[macro_use]
1
 extern crate clap;
2
 extern crate clap;
2
 extern crate time;
3
 extern crate time;
3
 extern crate task1;
4
 extern crate task1;
5
 
6
 
6
 use std::sync::mpsc::channel;
7
 use std::sync::mpsc::channel;
7
 use time::get_time;
8
 use time::get_time;
8
-use clap::{Arg, App, SubCommand};
9
+use clap::App;
9
 use std::{process, thread};
10
 use std::{process, thread};
10
 use task1::verify_product;
11
 use task1::verify_product;
11
 use std::sync::Arc;
12
 use std::sync::Arc;
12
 
13
 
13
 pub fn main() {
14
 pub fn main() {
14
-    let matches = create_app().get_matches();
15
+    let yaml = load_yaml!("cli.yml");
16
+    let matches = App::from_yaml(yaml).get_matches();
15
     let base = matches.value_of("base").unwrap_or("1");
17
     let base = matches.value_of("base").unwrap_or("1");
16
     let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
18
     let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
17
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
19
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
71
                         }
73
                         }
72
 
74
 
73
                         if let Some(solution) = verify_product(b, n, &d) {
75
                         if let Some(solution) = verify_product(b, n, &d) {
74
-                            let end = get_time();
75
                             found = sync;
76
                             found = sync;
76
-                            
77
-                            if time_measurement {
78
-                                let diff = end - start;
79
-                                let s = diff.num_seconds();
80
-                                let ms = diff.num_milliseconds();
81
-                                let us = diff.num_microseconds().unwrap_or(ms * 1000);
82
-                                println!("(Duration {}s / {}ms / {}us)", s, ms, us);
83
-                            }
84
                             tx.send(solution).unwrap();
77
                             tx.send(solution).unwrap();
85
                         }
78
                         }
86
                         n += t;
79
                         n += t;
91
 
84
 
92
             match rx.recv() {
85
             match rx.recv() {
93
                 Ok(sol) => {
86
                 Ok(sol) => {
87
+                    let end = get_time();
94
                     println!("Number: {} --> hash: {}", sol.number, sol.hash);
88
                     println!("Number: {} --> hash: {}", sol.number, sol.hash);
89
+                    if time_measurement {
90
+                        let diff = end - start;
91
+                        let s = diff.num_seconds();
92
+                        let ms = diff.num_milliseconds();
93
+                        let us = diff.num_microseconds().unwrap_or(ms * 1000);
94
+                        println!("(Duration {}s / {}ms / {}us)", s, ms, us);
95
+                    }
95
                 }
96
                 }
96
                 Err(_) => {}
97
                 Err(_) => {}
97
             }
98
             }
106
         }
107
         }
107
     };
108
     };
108
 }
109
 }
109
-
110
-fn create_app<'a, 'b>() -> App<'a, 'b> {
111
-    App::new("Hash256")
112
-        .version("1.0")
113
-        .author("Lorenz Bung & Joshua Rutschmann")
114
-        .about(
115
-            "Calculates the Hashvalue of the given base, number and difficulty.",
116
-        )
117
-        .arg(
118
-            Arg::with_name("base")
119
-                .value_name("base")
120
-                .help("Sets the base to use")
121
-                .takes_value(true)
122
-                .required(true),
123
-        )
124
-        .arg(
125
-            Arg::with_name("difficulty")
126
-                .value_name("difficulty")
127
-                .help("Sets the difficulty to use")
128
-                .takes_value(true)
129
-                .required(true),
130
-        )
131
-        .arg(
132
-            Arg::with_name("threads")
133
-                .value_name("threads")
134
-                .help(
135
-                    "Sets the number of the threads to use (default = number of cpus)",
136
-                )
137
-                .takes_value(true)
138
-                .required(false),
139
-        ).arg(Arg::with_name("verbose")
140
-              .short("v")
141
-              .multiple(true)
142
-              .required(false),
143
-        )
144
-    .arg(Arg::with_name("sync")
145
-            .short("s")
146
-            .required(false),
147
-    )
148
-    .arg(Arg::with_name("config")
149
-            .long("config")
150
-            .value_name("VALUE")
151
-            .help("sets special sync parameter")
152
-                               .takes_value(true))
153
-    .subcommand(
154
-            SubCommand::with_name("timings")
155
-                .about("controls timing features")
156
-                .version("1.0")
157
-                .author("Lorenz Bung & Joshua Rutschmann"),
158
-        )
159
-}

Loading…
Cancel
Save