Skip to content

Commit

Permalink
renice: use clap for real
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Jan 26, 2024
1 parent 0747ccf commit dc7f34c
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/uu/renice/src/renice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ use std::str::FromStr;
use uucore::{error::UResult, format_usage, help_about, help_usage};
const ABOUT: &str = help_about!("renice.md");
const USAGE: &str = help_usage!("renice.md");
use clap::{crate_version, Command};
use clap::{crate_version, Arg, Command};

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args: Vec<String> = env::args().collect();
let matches = uu_app().try_get_matches_from(args)?;

if args.len() != 3 {
eprintln!("Usage: renice <nice value> <pid>");
process::exit(1);
}
let nice_value = match matches.get_one::<i32>("nice_value") {
Some(number) => number,
_ => {
eprintln!("Invalid nice value");
process::exit(1);
}
};

let nice_value = i32::from_str(&args[1]).unwrap_or_else(|_| {
eprintln!("Invalid nice value");
process::exit(1);
});

let pid = i32::from_str(&args[2]).unwrap_or_else(|_| {
eprintln!("Invalid PID");
process::exit(1);
});
let pid = match matches.get_one::<i32>("pid") {
Some(number) => number,
_ => {
eprintln!("Invalid PID");
process::exit(1);
}
};

if unsafe { libc::setpriority(PRIO_PROCESS, pid.try_into().unwrap(), nice_value) } == -1 {
if unsafe { libc::setpriority(PRIO_PROCESS, (*pid).try_into().unwrap(), *nice_value) } == -1 {
eprintln!("Failed to set nice value: {}", Error::last_os_error());
process::exit(1);
}
Expand All @@ -47,4 +48,18 @@ pub fn uu_app() -> Command {
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.arg(
Arg::new("nice_value")
.value_name("NICE_VALUE")
.help("The new nice value for the process")
.required(true)
.index(1),
)
.arg(
Arg::new("pid")
.value_name("PID")
.help("The PID of the process")
.required(true)
.index(2),
)
}

0 comments on commit dc7f34c

Please sign in to comment.