Merge pull request from Harshit933/lscpu/hex

Add support for `--hex` flag for lscpu command
This commit is contained in:
Daniel Hofstetter 2025-01-29 16:33:41 +01:00 committed by GitHub
commit 9b8f231a5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions
src/uu/lscpu/src
tests/by-util

@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{crate_version, Command};
use clap::{crate_version, Arg, ArgAction, Command};
use regex::Regex;
use std::fs;
use sysinfo::System;
@ -16,9 +16,14 @@ const USAGE: &str = help_usage!("lscpu.md");
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
let system = System::new_all();
let hex = _matches.get_flag(options::HEX);
println!("Architecture: {}", get_architecture());
println!("CPU(s): {}", system.cpus().len());
if hex {
println!("CPU(s): 0x{:x}", system.cpus().len());
} else {
println!("CPU(s): {}", system.cpus().len());
}
// Add more CPU information here...
if let Ok(contents) = fs::read_to_string("/proc/cpuinfo") {
@ -31,6 +36,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(())
}
// More options can be added here
mod options {
pub const HEX: &str = "hex";
}
fn get_architecture() -> String {
if cfg!(target_arch = "x86") {
"x86".to_string()
@ -46,5 +56,7 @@ pub fn uu_app() -> Command {
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.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
sets in list format (for example 0,1). Note that before version 2.30 the mask has been
printed with 0x prefix.").required(false))
}

@ -9,3 +9,8 @@ use crate::common::util::TestScenario;
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_lscpt_with_arg() {
new_ucmd!().arg("--hex").succeeds().stdout_contains("0x");
}