Skip to content

Commit 6cedb45

Browse files
Add multiple file support to cat
1 parent bb6cc75 commit 6cedb45

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@ program
1010
.description("concatenate and print files")
1111
.option("-n, --number", "Number all output lines")
1212
.option("-b, --numberNonBlank", "Number non-blank output lines")
13-
.argument("<path>", "The file path to process");
13+
.argument("<path...>", "The file paths to process");
1414

1515
// here Parse command line arguments (reads process.argv and interprets it)
1616
program.parse();
1717

18-
// Get the parsed values
19-
const path = program.args[0]; // The filename user provided
20-
const hasNumberFlag = program.opts().number; // True if user used -n flag
21-
const hasBFlag = program.opts().numberNonBlank;
22-
23-
//read the file
24-
const content = await fs.readFile(path, "utf-8");
25-
26-
// Output with or without line numbers
27-
if (hasNumberFlag) {
28-
29-
// Add line numbers to each line
30-
const lines = content.split("\n"); // Split into array of lines
31-
const numberedLines = lines.map((line, index) => {
32-
const lineNumber = index + 1; // Convert 0-based index to 1-based line number
33-
return `${lineNumber} ${line}`; // Format: " 1 Hello"
34-
});
35-
console.log(numberedLines.join("\n")); // Join back with newlines
36-
} else if (hasBFlag) {
37-
console.log("Using -b flag!");
38-
let lineNumber = 0;
39-
const lines = content.split("\n");
40-
console.log("Total lines:", lines.length);
41-
const numberedLines = lines.map((line) => {
42-
console.log(`Line: "${line}", isEmpty: ${line.trim() === ""}`);
43-
if (line.trim() === "") {
44-
return line;
45-
} else {
18+
// Shared counter across all files for -n and -b flags
19+
let lineNumber = 0;
20+
21+
22+
// Process each file
23+
for (const path of program.args) {
24+
const hasNumberFlag = program.opts().number; // True if user used -n flag
25+
const hasBFlag = program.opts().numberNonBlank;
26+
27+
//read the file for each argument
28+
const content = await fs.readFile(path, "utf-8");
29+
// Output with or without line numbers
30+
if (hasNumberFlag) {
31+
// Add line numbers to each line
32+
const lines = content.split("\n"); // Split into array of lines
33+
const numberedLines = lines.map((line) => {
4634
lineNumber = lineNumber + 1;
47-
return ` ${lineNumber} ${line}`
48-
}
49-
});
50-
console.log(numberedLines.join("\n"));
51-
} else {
52-
// Just print the file as-is
53-
console.log(content);
35+
return `${lineNumber} ${line}`; // Format: " 1 Hello"
36+
});
37+
console.log(numberedLines.join("\n")); // Join back with newlines
38+
} else if (hasBFlag) {
39+
const lines = content.split("\n");
40+
41+
const numberedLines = lines.map((line) => {
42+
if (line.trim() === "") {
43+
return line;
44+
} else {
45+
lineNumber = lineNumber + 1;
46+
return ` ${lineNumber} ${line}`;
47+
}
48+
});
49+
console.log(numberedLines.join("\n"));
50+
} else {
51+
// Just print the file as-is
52+
console.log(content);
53+
}
5454
}

0 commit comments

Comments
 (0)