Skip to content

Commit e2a0265

Browse files
committed
using the commander library
1 parent c874ceb commit e2a0265

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

implement-shell-tools/cat/mycat.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import process from "node:process";
1+
import { program } from "commander";
22
import { promises as fs } from "node:fs";
3+
import process from "node:process";
4+
5+
program
6+
.name("mycat")
7+
.description("Outputs the content of the given file(s), like the cat command")
8+
.argument("<path>", "The file path to read");
39

4-
const argv = process.argv.slice(2);
5-
if (argv.length != 1) {
10+
program.parse();
11+
12+
const argv = program.args;
13+
if (argv.length !== 1) {
614
console.error(
715
`Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`
816
);
917
process.exit(1);
1018
}
19+
1120
const path = argv[0];
12-
1321
const content = await fs.readFile(path, "utf-8");
1422
process.stdout.write(content);

implement-shell-tools/cat/package-lock.json

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

implement-shell-tools/cat/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
},
1010
"keywords": [],
1111
"author": "",
12-
"license": "ISC"
12+
"license": "ISC",
13+
"dependencies": {
14+
"commander": "^14.0.0"
15+
}
1316
}

0 commit comments

Comments
 (0)