1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-29 19:47:45 +00:00

refac: use clap's builtin env reader

This commit is contained in:
Kevin Amado 2025-02-23 15:11:58 -07:00
parent 3ce9e7d115
commit d6ac7ad4d0
4 changed files with 15 additions and 18 deletions

View file

@ -7,6 +7,7 @@ alejandra = { path = "../alejandra" }
clap = { version = "*", default-features = false, features = [
"color",
"derive",
"env",
"std",
"strsim",
] }

View file

@ -1,4 +1,3 @@
use std::env;
use std::fs::read_to_string;
use std::io::Read;
@ -49,7 +48,11 @@ struct CLIArgs {
/// Number of formatting threads to spawn. Defaults to the number of
/// physical CPUs.
#[clap(long, short, value_parser = value_parser!(u8).range(1..))]
#[clap(
long, short,
env = "ALEJANDRA_THREADS",
value_parser = value_parser!(u8).range(1..),
)]
threads: Option<u8>,
/// Use once to hide informational messages,
@ -146,15 +149,8 @@ pub fn main() -> ! {
let include: Vec<&str> =
args.include.iter().map(String::as_str).collect::<Vec<&str>>();
// Try CLI value, then env var, then fall back to number of physical CPUs.
let threads: usize = if let Some(cli_threads) = args.threads {
cli_threads.into() // convert u8 to usize
} else {
env::var("ALEJANDRA_THREADS")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or_else(num_cpus::get_physical)
};
let threads =
args.threads.map_or_else(num_cpus::get_physical, Into::<usize>::into);
let verbosity = match args.quiet {
0 => Verbosity::Everything,

View file

@ -128,7 +128,7 @@ fn cases() {
output_got.push_str(&format!("args: {:?}\n", case.args));
let mut child = Command::new("cargo")
.env("ALEJANDRA_THREADS", "4")
.env("ALEJANDRA_THREADS", "1")
.args(["run", "--quiet", "--"])
.args(case.args)
.stdin(Stdio::piped())

View file

@ -33,7 +33,7 @@ stdout:
-t, --threads <THREADS>
Number of formatting threads to spawn. Defaults to the number of
physical CPUs
physical CPUs [env: ALEJANDRA_THREADS=1]
-V, --version
Print version information
@ -184,7 +184,7 @@ exit code: Some(1)
===
args: [".", "--exclude", "."]
stderr:
Checking style in 0 files using 4 threads.
Checking style in 0 files using 1 thread.
Congratulations! Your code complies with the Alejandra style.
@ -203,7 +203,7 @@ stdout:
Requires formatting: tests/inputs/changed.nix
stderr:
Checking style in 1 file using 4 threads.
Checking style in 1 file using 1 thread.
Alert! 1 file requires formatting.
@ -218,7 +218,7 @@ stdout:
Requires formatting: tests/inputs/changed.nix
stderr:
Checking style in 1 file using 4 threads.
Checking style in 1 file using 1 thread.
Alert! 1 file requires formatting.
@ -233,7 +233,7 @@ exit code: Some(2)
===
args: ["-c", "tests/inputs/unchanged.nix"]
stderr:
Checking style in 1 file using 4 threads.
Checking style in 1 file using 1 thread.
Congratulations! Your code complies with the Alejandra style.
@ -249,7 +249,7 @@ exit code: Some(0)
===
args: ["--check", "tests/inputs/error.nix"]
stderr:
Checking style in 1 file using 4 threads.
Checking style in 1 file using 1 thread.
Failed! 1 error found at: