Skip to content

Commit a25b21e

Browse files
committed
add number line flag
1 parent 296d98a commit a25b21e

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
const fs = require("fs");
22

3-
const files = process.argv.slice(2);
3+
const args = process.argv.slice(2);
44

5-
for (const file of files) {
6-
const content = fs.readFileSync(file, "utf8");
5+
const numberLines = args[0] === "-n";
6+
7+
const filename = numberLines ? args[1] : args[0];
8+
9+
const content = fs.readFileSync(filename, "utf8");
10+
11+
const lines = content.split("\n");
12+
13+
if (numberLines) {
14+
lines.forEach((line, index) => {
15+
console.log(`${String(index + 1).padStart(6)}\t${line}`);
16+
});
17+
} else {
718
process.stdout.write(content);
819
}

0 commit comments

Comments
 (0)