Skip to content

Commit 2691eee

Browse files
committed
Changed basic and addedn flan -n and -b
1 parent f929943 commit 2691eee

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

implement-shell-tools/cat/myCat.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1-
import fs from "node:fs";
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
import process from "node:process";
24

3-
const args = process.argv.slice(2);
5+
program
6+
.name("myCat")
7+
.description("Simple file viewer")
8+
.option("-n", "Number all output lines")
9+
.option("-b", "Number non-blank output lines")
10+
.argument("<path...>", "One or more file paths to show");
11+
12+
program.parse();
13+
14+
const args = program.args;
415
const files = args;
16+
const opts = program.opts();
17+
let lineNumber = 1;
518

619
for (const filename of files) {
7-
const content = fs.readFileSync(filename, "utf-8");
8-
process.stdout.write(content);
20+
const content = await fs.readFile(filename, "utf-8");
21+
22+
if (opts.n) {
23+
const lines = content.split("\n");
24+
25+
for (const line of lines) {
26+
console.log(lineNumber + " " + line);
27+
lineNumber++;
28+
}
29+
} else if (opts.b) {
30+
const lines = content.split("\n");
31+
32+
for (const line of lines) {
33+
if (line.trim() !== "") {
34+
console.log(lineNumber + " " + line);
35+
lineNumber++;
36+
}
37+
}
38+
} else {
39+
console.log(content);
40+
}
941
}

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.2"
5+
}
6+
}

0 commit comments

Comments
 (0)