2023-01-07 18:50:18 -05:00
|
|
|
use super::Cmd;
|
2023-01-08 01:12:36 -05:00
|
|
|
use clap::{Arg, ArgAction, Command};
|
|
|
|
use std::io;
|
2023-01-07 18:50:18 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Nproc {
|
|
|
|
name: &'static str,
|
|
|
|
path: Option<crate::Path>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Nproc {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "nproc",
|
|
|
|
path: Some(crate::Path::UsrBin),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cmd for Nproc {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new(self.name)
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.about("Print the number of processing units available")
|
|
|
|
.arg(
|
|
|
|
Arg::new("ALL")
|
|
|
|
.help("Print the number of installed processors")
|
|
|
|
.short('a')
|
|
|
|
.long("all")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let Some(matches) = matches else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
|
|
|
};
|
|
|
|
if matches.get_flag("ALL") {
|
|
|
|
println!("{}", num_cpus::get());
|
|
|
|
} else {
|
|
|
|
println!("{}", num_cpus::get_physical());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
|
|
self.path
|
|
|
|
}
|
|
|
|
}
|