renice: use clap for real
This commit is contained in:
@ -11,28 +11,29 @@ use std::str::FromStr;
|
|||||||
use uucore::{error::UResult, format_usage, help_about, help_usage};
|
use uucore::{error::UResult, format_usage, help_about, help_usage};
|
||||||
const ABOUT: &str = help_about!("renice.md");
|
const ABOUT: &str = help_about!("renice.md");
|
||||||
const USAGE: &str = help_usage!("renice.md");
|
const USAGE: &str = help_usage!("renice.md");
|
||||||
use clap::{crate_version, Command};
|
use clap::{crate_version, Arg, Command};
|
||||||
|
|
||||||
#[uucore::main]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
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 {
|
let nice_value = match matches.get_one::<i32>("nice_value") {
|
||||||
eprintln!("Usage: renice <nice value> <pid>");
|
Some(number) => number,
|
||||||
process::exit(1);
|
_ => {
|
||||||
}
|
eprintln!("Invalid nice value");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let nice_value = i32::from_str(&args[1]).unwrap_or_else(|_| {
|
let pid = match matches.get_one::<i32>("pid") {
|
||||||
eprintln!("Invalid nice value");
|
Some(number) => number,
|
||||||
process::exit(1);
|
_ => {
|
||||||
});
|
eprintln!("Invalid PID");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let pid = i32::from_str(&args[2]).unwrap_or_else(|_| {
|
if unsafe { libc::setpriority(PRIO_PROCESS, (*pid).try_into().unwrap(), *nice_value) } == -1 {
|
||||||
eprintln!("Invalid PID");
|
|
||||||
process::exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
if unsafe { libc::setpriority(PRIO_PROCESS, pid.try_into().unwrap(), nice_value) } == -1 {
|
|
||||||
eprintln!("Failed to set nice value: {}", Error::last_os_error());
|
eprintln!("Failed to set nice value: {}", Error::last_os_error());
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
@ -47,4 +48,18 @@ pub fn uu_app() -> Command {
|
|||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.override_usage(format_usage(USAGE))
|
.override_usage(format_usage(USAGE))
|
||||||
.infer_long_args(true)
|
.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),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user