Skip to content

Commit 5558be7

Browse files
Montana Paynejlevy
authored andcommitted
feat: multiple file processing support
1 parent 6867d2c commit 5558be7

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

‎src/flowmark/cli.py‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# Format a Markdown file to stdout
2020
flowmark README.md
2121
22+
# Format multiple Markdown files in-place
23+
flowmark --inplace README.md CHANGELOG.md docs/*.md
24+
2225
# Format a Markdown file in-place without backups and all auto-formatting
2326
# options enabled
2427
flowmark --auto README.md
@@ -40,19 +43,21 @@
4043
For more details, see: https://github.com/jlevy/flowmark
4144
"""
4245

46+
from __future__ import annotations
47+
4348
import argparse
4449
import importlib.metadata
4550
import sys
4651
from dataclasses import dataclass
4752

48-
from flowmark.reformat_api import reformat_file
53+
from flowmark.reformat_api import reformat_files
4954

5055

5156
@dataclass
5257
class Options:
5358
"""Command-line options for the flowmark tool."""
5459

55-
file: str
60+
files: list[str]
5661
output: str
5762
width: int
5863
plaintext: bool
@@ -79,11 +84,11 @@ def _parse_args(args: list[str] | None = None) -> Options:
7984
formatter_class=argparse.RawDescriptionHelpFormatter,
8085
)
8186
parser.add_argument(
82-
"file",
83-
nargs="?",
87+
"files",
88+
nargs="*",
8489
type=str,
85-
default="-",
86-
help="Input file (use '-' for stdin)",
90+
default=["-"],
91+
help="Input files (use '-' for stdin, multiple files supported)",
8792
)
8893
parser.add_argument(
8994
"-o",
@@ -161,7 +166,7 @@ def _parse_args(args: list[str] | None = None) -> Options:
161166
opts.ellipses = True
162167

163168
return Options(
164-
file=opts.file,
169+
files=opts.files,
165170
output=opts.output,
166171
width=opts.width,
167172
plaintext=opts.plaintext,
@@ -197,8 +202,8 @@ def main(args: list[str] | None = None) -> int:
197202
return 0
198203

199204
try:
200-
reformat_file(
201-
path=options.file,
205+
reformat_files(
206+
files=options.files,
202207
output=options.output,
203208
width=options.width,
204209
inplace=options.inplace,

‎src/flowmark/reformat_api.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,88 @@ def reformat_file(
102102
else:
103103
with atomic_output_file(output, make_parents=make_parents) as tmp_path:
104104
tmp_path.write_text(result)
105+
106+
107+
def reformat_files(
108+
files: list[str],
109+
output: str | None = None,
110+
width: int = 88,
111+
inplace: bool = False,
112+
nobackup: bool = False,
113+
plaintext: bool = False,
114+
semantic: bool = False,
115+
cleanups: bool = True,
116+
smartquotes: bool = False,
117+
ellipses: bool = False,
118+
make_parents: bool = True,
119+
) -> None:
120+
"""
121+
Reformat multiple files with the same options.
122+
123+
Args:
124+
files: List of file paths to process, or ["-"] for stdin.
125+
output: Output file path (ignored when inplace=True, use "-" for stdout).
126+
width: The width to wrap lines to.
127+
inplace: Whether to write files back to their original paths.
128+
nobackup: Whether to not make backups of original files.
129+
plaintext: Use plaintext instead of Markdown mode wrapping.
130+
semantic: Use semantic line breaks (based on sentences) heuristic.
131+
cleanups: Enable (safe) cleanups for common issues.
132+
smartquotes: Convert straight quotes to typographic quotes.
133+
ellipses: Convert three dots to ellipsis character.
134+
make_parents: Whether to make parent directories if they don't exist.
135+
"""
136+
if len(files) == 1 and files[0] == "-":
137+
# Single stdin case - use original function
138+
reformat_file(
139+
path=files[0],
140+
output=output,
141+
width=width,
142+
inplace=inplace,
143+
nobackup=nobackup,
144+
plaintext=plaintext,
145+
semantic=semantic,
146+
cleanups=cleanups,
147+
smartquotes=smartquotes,
148+
ellipses=ellipses,
149+
make_parents=make_parents,
150+
)
151+
return
152+
153+
# Multiple files case
154+
if not inplace and output and output != "-":
155+
raise ValueError(
156+
"Cannot specify output file when processing multiple files (use --inplace instead)"
157+
)
158+
159+
for file_path in files:
160+
if inplace:
161+
# Process each file in-place
162+
reformat_file(
163+
path=file_path,
164+
output=None,
165+
width=width,
166+
inplace=True,
167+
nobackup=nobackup,
168+
plaintext=plaintext,
169+
semantic=semantic,
170+
cleanups=cleanups,
171+
smartquotes=smartquotes,
172+
ellipses=ellipses,
173+
make_parents=make_parents,
174+
)
175+
else:
176+
# Process each file to stdout
177+
reformat_file(
178+
path=file_path,
179+
output="-",
180+
width=width,
181+
inplace=False,
182+
nobackup=nobackup,
183+
plaintext=plaintext,
184+
semantic=semantic,
185+
cleanups=cleanups,
186+
smartquotes=smartquotes,
187+
ellipses=ellipses,
188+
make_parents=make_parents,
189+
)

0 commit comments

Comments
 (0)