Skip to content

Commit 7693032

Browse files
committed
feat(cat): add CLI flag support for line numbering
- Replace manual argv parsing with commander.js for robust CLI handling - Add -n/--number flag to number all output lines - Add -b/--number-nonblank flag to number only non-empty lines - Refactor variable names for clarity (filePathsToRead, concatenatedContent, etc) - Improve error handling with process.exitCode = 1 - Remove debug logging
1 parent de0bfda commit 7693032

3 files changed

Lines changed: 69 additions & 8 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
import process from "node:process";
2-
import { promises as fs, readFile } from "node:fs";
2+
import { promises as fs } from "node:fs";
3+
import { program } from "commander";
34

4-
const filesToRead = process.argv.slice(2);
5-
console.log(filesToRead);
5+
program
6+
.option("-n, --number", "number all output lines")
7+
.option("-b, --number-nonblank", "number only non-empty lines")
8+
.arguments("<files...>")
9+
.parse();
610

7-
async function readMultipleFiles() {
11+
const cliOptions = program.opts();
12+
const filePathsToRead = program.args;
13+
14+
async function readAndOutputFiles() {
815
try {
9-
const results = await Promise.all(
10-
filesToRead.map((file) => fs.readFile(file, "utf-8")),
16+
const fileContents = await Promise.all(
17+
filePathsToRead.map((filePath) => fs.readFile(filePath, "utf-8")),
1118
);
12-
process.stdout.write(results.join(""));
19+
const concatenatedContent = fileContents.join("");
20+
21+
if (cliOptions.number) {
22+
// apply -n logic: number all lines
23+
const contentLines = concatenatedContent.split("\n");
24+
const numberedOutput = contentLines
25+
.map((line, index) => {
26+
return `${String(index + 1).padStart(6)} ${line}`;
27+
})
28+
.join("\n");
29+
process.stdout.write(numberedOutput);
30+
} else if (cliOptions.numberNonblank) {
31+
// apply -b logic: number only non-empty lines
32+
const contentLines = concatenatedContent.split("\n");
33+
let nonblankLineNumber = 0;
34+
const numberedOutput = contentLines
35+
.map((line) => {
36+
if (line.trim() === "") {
37+
return line;
38+
}
39+
nonblankLineNumber++;
40+
return `${String(nonblankLineNumber).padStart(6)} ${line}`;
41+
})
42+
.join("\n");
43+
process.stdout.write(numberedOutput);
44+
} else {
45+
process.stdout.write(concatenatedContent);
46+
}
1347
} catch (err) {
1448
console.error("Error reading multiple files:", err);
1549
process.exitCode = 1;
1650
}
1751
}
1852

19-
readMultipleFiles();
53+
readAndOutputFiles();

implement-shell-tools/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

implement-shell-tools/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"commander": "^14.0.3"
5+
}
6+
}

0 commit comments

Comments
 (0)