Skip to content

Commit c2e3454

Browse files
committed
feat: --file-list support
1 parent 2b7af0e commit c2e3454

File tree

9 files changed

+73
-7
lines changed

9 files changed

+73
-7
lines changed

crates/typos-cli/src/bin/typos-cli/args.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ impl Format {
3939
#[command(group = clap::ArgGroup::new("mode").multiple(false))]
4040
pub(crate) struct Args {
4141
/// Paths to check with `-` for stdin
42-
#[arg(default_value = ".")]
42+
#[arg(default_value = ".", group = "source")]
4343
pub(crate) path: Vec<std::path::PathBuf>,
4444

45+
/// Read the list of newline separated paths from file or stdin (if `-`)
46+
#[arg(long, group = "source")]
47+
pub(crate) file_list: Option<std::path::PathBuf>,
48+
4549
/// The approximate number of threads to use.
4650
#[arg(short = 'j', long = "threads", default_value = "0")]
4751
pub(crate) threads: usize,

crates/typos-cli/src/bin/typos-cli/main.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io::Write;
1+
use std::io::{BufRead as _, BufReader, Write as _};
2+
use std::path::PathBuf;
23

34
use clap::Parser;
45

@@ -167,8 +168,34 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
167168

168169
let mut typos_found = false;
169170
let mut errors_found = false;
170-
for path in args.path.iter() {
171+
172+
let file_list = match args.file_list.as_deref() {
173+
Some(dash) if dash == PathBuf::from("-") => Some(
174+
std::io::stdin()
175+
.lines()
176+
.map(|res| res.map(PathBuf::from))
177+
.collect::<Result<_, _>>()
178+
.with_code(proc_exit::sysexits::IO_ERR)?,
179+
),
180+
Some(path) => Some(
181+
BufReader::new(std::fs::File::open(path).with_code(proc_exit::sysexits::IO_ERR)?)
182+
.lines()
183+
.map(|res| res.map(PathBuf::from))
184+
.collect::<Result<_, _>>()
185+
.with_code(proc_exit::sysexits::IO_ERR)?,
186+
),
187+
None => None,
188+
};
189+
190+
// Note: file_list and args.path are mutually exclusive, enforced by clap
191+
for path in file_list.as_ref().unwrap_or(&args.path) {
192+
// Note paths are passed through stdin, `-` is treated like a normal path
171193
let cwd = if path == std::path::Path::new("-") {
194+
if args.file_list.is_some() {
195+
return Err(proc_exit::sysexits::USAGE_ERR.with_message(
196+
"Can't use `-` (stdin) while using `--file_list` provided paths",
197+
));
198+
};
172199
global_cwd.clone()
173200
} else if path.is_file() {
174201
let mut cwd = path
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[files]
2+
extend-exclude = ["_typos.toml"]
3+
4+
[default.extend-identifiers]
5+
hello = "goodbye"
6+
7+
[type.fail]
8+
extend-glob = ["*.fail"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
bin.name = "typos"
2+
args = "--file-list -"
3+
stdin = """
4+
b.fail
5+
d.fail
6+
"""
7+
stdout = """
8+
error: `hello` should be `goodbye`
9+
--> b.fail:1:1
10+
|
11+
1 | hello
12+
| ^^^^^
13+
|
14+
error: `hello` should be `goodbye`
15+
--> d.fail:1:1
16+
|
17+
1 | hello
18+
| ^^^^^
19+
|
20+
"""
21+
stderr = ""
22+
status.code = 2

crates/typos-cli/tests/cmd/help.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Arguments:
99
[PATH]... Paths to check with `-` for stdin [default: .]
1010
1111
Options:
12-
-j, --threads <THREADS> The approximate number of threads to use [default: 0]
13-
--force-exclude Respect excluded files even for paths passed explicitly
14-
-h, --help Print help
15-
-V, --version Print version
12+
--file-list <FILE_LIST> Read the list of newline separated paths from file or stdin (if `-`)
13+
-j, --threads <THREADS> The approximate number of threads to use [default: 0]
14+
--force-exclude Respect excluded files even for paths passed explicitly
15+
-h, --help Print help
16+
-V, --version Print version
1617
1718
Config:
1819
-c, --config <CUSTOM_CONFIG> Custom config file

0 commit comments

Comments
 (0)