Skip to content

Commit ad666a0

Browse files
committed
Updated clap example dependency
1 parent efe832b commit ad666a0

3 files changed

Lines changed: 36 additions & 54 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ win-sys = "0.2"
3939

4040
[dev-dependencies]
4141
raw_sync = "0.1"
42-
clap = "2.33"
42+
clap = {version = "3", features = ["derive"]}
4343
env_logger = "0"

examples/basic.rs

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
11
use std::thread;
22

3-
use clap::{App, Arg};
3+
use clap::Parser;
44
use shared_memory::*;
55

6+
/// Spawns N threads that increment a value to 100
7+
#[derive(Parser)]
8+
#[clap(author, version, about)]
9+
struct Args {
10+
/// Number of threads to spawn
11+
num_threads: usize,
12+
13+
/// Count to this value
14+
#[clap(long, short, default_value_t = 50)]
15+
count_to: u8,
16+
}
17+
618
fn main() {
719
env_logger::init();
20+
let args = Args::parse();
821

9-
// Get number of thread argument
10-
let matches = App::new("Basic Example")
11-
.about("Spawns N threads that increment a value to 100")
12-
.arg(
13-
Arg::with_name("num_threads")
14-
.help("Number of threads to spawn")
15-
.required(true)
16-
.takes_value(true),
17-
)
18-
.arg(
19-
Arg::with_name("count_to")
20-
.help("Count to this value")
21-
.short("c")
22-
.long("count")
23-
.default_value("50")
24-
.takes_value(true)
25-
)
26-
.get_matches();
27-
let num_threads: usize = matches
28-
.value_of("num_threads")
29-
.unwrap()
30-
.parse()
31-
.expect("Invalid number passed for num_threads");
32-
33-
let max: u8 = matches
34-
.value_of("count_to")
35-
.unwrap()
36-
.parse()
37-
.expect("Invalid number passed for count_to");
38-
39-
if num_threads < 1 {
22+
if args.num_threads < 1 {
4023
eprintln!("Invalid number of threads");
4124
return;
4225
}
4326

44-
let mut threads = Vec::with_capacity(num_threads);
27+
let mut threads = Vec::with_capacity(args.num_threads);
4528
let _ = std::fs::remove_file("basic_mapping");
46-
29+
let max = args.count_to;
4730
// Spawn N threads
48-
for i in 0..num_threads {
31+
for i in 0..args.num_threads {
4932
let thread_id = i + 1;
5033
threads.push(thread::spawn(move || {
5134
increment_value("basic_mapping", thread_id, max);

examples/mutex.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
use std::sync::atomic::{AtomicU8, Ordering};
22
use std::thread;
33

4-
use clap::{App, Arg};
4+
use clap::Parser;
55
use raw_sync::locks::*;
66
use shared_memory::*;
77

8+
/// Spawns N threads that increment a value to 10 using a mutex
9+
#[derive(Parser)]
10+
#[clap(author, version, about)]
11+
struct Args {
12+
/// Number of threads to spawn
13+
num_threads: usize,
14+
15+
/// Count to this value
16+
#[clap(long, short, default_value_t = 50)]
17+
count_to: u8,
18+
}
19+
820
fn main() {
921
env_logger::init();
10-
let matches = App::new("Mutex Example")
11-
.about("Spawns N threads that increment a value to 10 using a mutex")
12-
.arg(
13-
Arg::with_name("num_threads")
14-
.help("Number of threads to spawn")
15-
.required(true)
16-
.takes_value(true),
17-
)
18-
.get_matches();
22+
let args = Args::parse();
1923

20-
let num_threads: usize = matches
21-
.value_of("num_threads")
22-
.unwrap()
23-
.parse()
24-
.expect("Invalid number passed for num_threads");
25-
if num_threads < 1 {
24+
if args.num_threads < 1 {
2625
eprintln!("num_threads should be 2 or more");
2726
return;
2827
}
29-
let mut threads = Vec::with_capacity(num_threads);
28+
let mut threads = Vec::with_capacity(args.num_threads);
3029
let _ = std::fs::remove_file("mutex_mapping");
3130

3231
// Spawn N threads
33-
for i in 0..num_threads {
32+
for i in 0..args.num_threads {
3433
let thread_id = i + 1;
3534
threads.push(thread::spawn(move || {
3635
increment_value("mutex_mapping", thread_id);

0 commit comments

Comments
 (0)