Skip to content

Commit 11ce7fa

Browse files
committed
vmstat: fix parsing
1 parent 57cf05d commit 11ce7fa

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/uu/vmstat/src/vmstat.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod picker;
88

99
#[cfg(target_os = "linux")]
1010
use crate::picker::{get_pickers, Picker};
11+
use clap::value_parser;
1112
#[allow(unused_imports)]
1213
use clap::{arg, crate_version, ArgMatches, Command};
1314
#[allow(unused_imports)]
@@ -37,36 +38,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3738

3839
let one_header = matches.get_flag("one-header");
3940
let no_first = matches.get_flag("no-first");
40-
let delay = matches.get_one::<String>("delay");
41-
let mut delay = if let Some(delay) = delay {
42-
let delay = delay.parse::<i64>().unwrap();
43-
if delay <= 0 {
44-
return Err(USimpleError::new(-1, "delay must be greater than 0"));
45-
}
46-
delay
47-
} else {
48-
-1
49-
};
50-
let count = matches.get_one::<String>("count");
41+
42+
let delay = matches.get_one::<u64>("delay");
43+
let count = matches.get_one::<u64>("count");
5144
let mut count = if let Some(count) = count {
52-
let mut count = count.parse::<i64>().unwrap();
53-
if count < 0 {
54-
return Err(USimpleError::new(-1, "count must be greater than 0"));
55-
}
56-
if count == 0 {
57-
count = 1;
45+
if *count == 0 {
46+
Some(1)
47+
} else {
48+
Some(*count)
5849
}
59-
count
6050
} else {
61-
-1
51+
None
6252
};
63-
if count < 0 && delay < 0 {
64-
count = 1;
65-
if no_first {
66-
delay = 1;
67-
count = 1;
53+
let delay = if let Some(delay) = delay {
54+
*delay
55+
} else {
56+
if count.is_none() {
57+
count = Some(1);
6858
}
69-
}
59+
1
60+
};
7061

7162
let pickers = get_pickers(&matches);
7263
let mut proc_data = ProcData::new();
@@ -82,10 +73,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8273
.map(|size| size.1 .0)
8374
.unwrap_or(0);
8475

85-
while count < 0 || line_count < count {
86-
std::thread::sleep(std::time::Duration::from_secs(delay as u64));
76+
while count.is_none() || line_count < count.unwrap() {
77+
std::thread::sleep(std::time::Duration::from_secs(delay));
8778
let proc_data_now = ProcData::new();
88-
if !one_header && term_height > 0 && ((line_count + 3) % term_height as i64 == 0) {
79+
if !one_header && term_height > 0 && ((line_count + 3) % term_height as u64 == 0) {
8980
print_header(&pickers);
9081
}
9182
print_data(&pickers, &proc_data_now, Some(&proc_data), &matches);
@@ -96,6 +87,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
9687

9788
Ok(())
9889
}
90+
9991
#[cfg(target_os = "linux")]
10092
fn print_header(pickers: &[Picker]) {
10193
let mut section: Vec<&str> = vec![];
@@ -108,6 +100,7 @@ fn print_header(pickers: &[Picker]) {
108100
println!("{}", section.join(" "));
109101
println!("{}", title.join(" "));
110102
}
103+
111104
#[cfg(target_os = "linux")]
112105
fn print_data(
113106
pickers: &[Picker],
@@ -137,8 +130,12 @@ pub fn uu_app() -> Command {
137130
.override_usage(format_usage(USAGE))
138131
.infer_long_args(true)
139132
.args([
140-
arg!(<delay> "The delay between updates in seconds").required(false),
141-
arg!(<count> "Number of updates").required(false),
133+
arg!(<delay> "The delay between updates in seconds")
134+
.required(false)
135+
.value_parser(value_parser!(u64).range(1..)),
136+
arg!(<count> "Number of updates")
137+
.required(false)
138+
.value_parser(value_parser!(u64)),
142139
arg!(-a --active "Display active and inactive memory"),
143140
// arg!(-f --forks "switch displays the number of forks since boot"),
144141
// arg!(-m --slabs "Display slabinfo"),

0 commit comments

Comments
 (0)