1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

refactor(nproc): fix some bugs and use clap

This commit is contained in:
Sylvestre Ledru 2020-06-02 23:21:13 +02:00
parent c33f52baf1
commit 5963b15746
3 changed files with 36 additions and 43 deletions

1
Cargo.lock generated
View file

@ -1567,6 +1567,7 @@ dependencies = [
name = "uu_nproc" name = "uu_nproc"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -18,6 +18,7 @@ path = "src/nproc.rs"
getopts = "0.2.18" getopts = "0.2.18"
libc = "0.2.42" libc = "0.2.42"
num_cpus = "1.10" num_cpus = "1.10"
clap = "2.33"
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["fs"] } uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["fs"] }
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" } uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }

View file

@ -9,6 +9,7 @@
extern crate getopts; extern crate getopts;
extern crate num_cpus; extern crate num_cpus;
extern crate clap;
#[cfg(unix)] #[cfg(unix)]
extern crate libc; extern crate libc;
@ -17,6 +18,7 @@ extern crate libc;
extern crate uucore; extern crate uucore;
use std::env; use std::env;
use clap::{App, Arg};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub const _SC_NPROCESSORS_CONF: libc::c_int = 83; pub const _SC_NPROCESSORS_CONF: libc::c_int = 83;
@ -27,50 +29,38 @@ pub const _SC_NPROCESSORS_CONF: libc::c_int = 57;
#[cfg(target_os = "netbsd")] #[cfg(target_os = "netbsd")]
pub const _SC_NPROCESSORS_CONF: libc::c_int = 1001; pub const _SC_NPROCESSORS_CONF: libc::c_int = 1001;
static NAME: &str = "nproc"; static OPT_ALL: &str = "all";
static OPT_IGNORE: &str = "ignore";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Print the number of cores available to the current process.";
fn get_usage() -> String {
format!("{0} [OPTIONS]...", executable!())
}
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = getopts::Options::new(); let usage = get_usage();
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.arg(
Arg::with_name(OPT_ALL)
.short("")
.long("all")
.help("print the number of cores available to the system"),
)
.arg(
Arg::with_name(OPT_IGNORE)
.short("")
.long("ignore")
.takes_value(true)
.help("ignore up to N cores"),
)
.get_matches_from(&args);
opts.optflag( let mut ignore = match matches.value_of(OPT_IGNORE) {
"",
"all",
"print the number of cores available to the system",
);
opts.optopt("", "ignore", "ignore up to N cores", "N");
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(err) => {
show_error!("{}", err);
return 1;
}
};
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
}
if matches.opt_present("help") {
let msg = format!(
"{0} {1}
Usage:
{0} [OPTIONS]...
Print the number of cores available to the current process.",
NAME, VERSION
);
print!("{}", opts.usage(&msg));
return 0;
}
let mut ignore = match matches.opt_str("ignore") {
Some(numstr) => match numstr.parse() { Some(numstr) => match numstr.parse() {
Ok(num) => num, Ok(num) => num,
Err(e) => { Err(e) => {
@ -81,7 +71,8 @@ Print the number of cores available to the current process.",
None => 0, None => 0,
}; };
if !matches.opt_present("all") { if ! matches.is_present(OPT_ALL) {
// OMP_NUM_THREADS doesn't have an impact on --all
ignore += match env::var("OMP_NUM_THREADS") { ignore += match env::var("OMP_NUM_THREADS") {
Ok(threadstr) => match threadstr.parse() { Ok(threadstr) => match threadstr.parse() {
Ok(num) => num, Ok(num) => num,
@ -91,7 +82,7 @@ Print the number of cores available to the current process.",
}; };
} }
let mut cores = if matches.opt_present("all") { let mut cores = if matches.is_present(OPT_ALL) {
num_cpus_all() num_cpus_all()
} else { } else {
num_cpus::get() num_cpus::get()
@ -125,7 +116,7 @@ fn num_cpus_all() -> usize {
} }
} }
// Other platform(e.g., windows), num_cpus::get() directly. // Other platforms (e.g., windows), num_cpus::get() directly.
#[cfg(not(any( #[cfg(not(any(
target_os = "linux", target_os = "linux",
target_os = "macos", target_os = "macos",