Skip to content

Commit ffe2ba6

Browse files
committed
feature: add --hex flag for lscpu
This commits add the `--hex` flag for lscpu in order to convert all the decimal values (for now only cores) to hexadecimal.
1 parent c00a4cb commit ffe2ba6

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/uu/lscpu/src/lscpu.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use clap::{crate_version, Command};
6+
use clap::{crate_version, Arg, ArgAction, Command};
77
use regex::Regex;
88
use std::fs;
99
use sysinfo::System;
@@ -16,9 +16,14 @@ const USAGE: &str = help_usage!("lscpu.md");
1616
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1717
let _matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
1818
let system = System::new_all();
19+
let hex= _matches.get_flag(options::HEX);
1920

2021
println!("Architecture: {}", get_architecture());
21-
println!("CPU(s): {}", system.cpus().len());
22+
if hex {
23+
println!("CPU(s): 0x{:x}", system.cpus().len());
24+
} else {
25+
println!("CPU(s): {}", system.cpus().len());
26+
}
2227
// Add more CPU information here...
2328

2429
if let Ok(contents) = fs::read_to_string("/proc/cpuinfo") {
@@ -31,6 +36,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3136
Ok(())
3237
}
3338

39+
// More options can be added here
40+
mod options {
41+
pub const HEX: &str = "hex";
42+
}
43+
3444
fn get_architecture() -> String {
3545
if cfg!(target_arch = "x86") {
3646
"x86".to_string()
@@ -46,5 +56,7 @@ pub fn uu_app() -> Command {
4656
.version(crate_version!())
4757
.about(ABOUT)
4858
.override_usage(format_usage(USAGE))
49-
.infer_long_args(true)
59+
.infer_long_args(true).arg(Arg::new(options::HEX).short('x').long("hex").action(ArgAction::SetTrue).help("Use hexadecimal masks for CPU sets (for example 'ff'). The default is to print the
60+
sets in list format (for example 0,1). Note that before version 2.30 the mask has been
61+
printed with 0x prefix.").required(false))
5062
}

0 commit comments

Comments
 (0)