Skip to content

Commit

Permalink
Merge pull request #201 from Harshit933/lscpu/hex
Browse files Browse the repository at this point in the history
Add support for `--hex` flag for lscpu command
  • Loading branch information
cakebaker authored Jan 29, 2025
2 parents a6a99b9 + 1681794 commit 9b8f231
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/uu/lscpu/src/lscpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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") {
Expand All @@ -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()
Expand All @@ -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))
}
5 changes: 5 additions & 0 deletions tests/by-util/test_lscpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 9b8f231

Please sign in to comment.