Skip to content

Commit 9b8f231

Browse files
authored
Merge pull request #201 from Harshit933/lscpu/hex
Add support for `--hex` flag for lscpu command
2 parents a6a99b9 + 1681794 commit 9b8f231

2 files changed

Lines changed: 20 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
}

tests/by-util/test_lscpu.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ use crate::common::util::TestScenario;
99
fn test_invalid_arg() {
1010
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
1111
}
12+
13+
#[test]
14+
fn test_lscpt_with_arg() {
15+
new_ucmd!().arg("--hex").succeeds().stdout_contains("0x");
16+
}

0 commit comments

Comments
 (0)