Skip to content

Commit 3758f99

Browse files
committed
feat: add -l, -w, and -c flags support to wc implementation
1 parent 383f3a6 commit 3758f99

1 file changed

Lines changed: 51 additions & 20 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.mjs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,68 @@
11
import { program } from "commander";
2-
import { error } from "node:console";
3-
import { promises as fs, link } from "node:fs";
2+
3+
import { promises as fs } from "node:fs";
44

55
program
66
.name("wc")
77
.description("wc implementation")
8-
.argument("<paths...>", "the file path to process");
8+
.argument("<paths...>", "the file path to process")
9+
.option("-l", "count lines")
10+
.option("-w", "count words")
11+
.option("-c", "count characters");
912
program.parse();
1013

1114
const paths = program.args;
15+
16+
if (paths.length === 0) {
17+
console.error("Expected at least one argument (a path)");
18+
process.exit(1);
19+
}
20+
const options = program.opts();
21+
1222
const total = {
1323
linesCounter: 0,
1424
wordsCounter: 0,
1525
characterCounter: 0,
1626
};
17-
try{
18-
for (const path of paths) {
19-
const content = await fs.readFile(path, "utf-8");
2027

21-
const linesCounter = content.split("\n").length - 1;
22-
const wordsCounter = content.trim().split(/\s+/).length;
23-
const characterCounter = content.length;
28+
try {
29+
for (const path of paths) {
30+
const content = await fs.readFile(path, "utf-8");
31+
32+
const linesCounter = content.split("\n").length - 1;
33+
const wordsCounter = content.trim().split(/\s+/).length;
34+
const characterCounter = content.length;
35+
36+
total.linesCounter += linesCounter;
37+
total.wordsCounter += wordsCounter;
38+
total.characterCounter += characterCounter;
2439

25-
total.linesCounter += linesCounter;
26-
total.wordsCounter += wordsCounter;
27-
total.characterCounter += characterCounter;
40+
let results = [];
41+
if (options.l) results.push(linesCounter);
42+
if (options.w) results.push(wordsCounter);
43+
if (options.c) results.push(characterCounter);
2844

29-
console.log(` ${linesCounter} ${wordsCounter} ${characterCounter} ${path}`);
45+
if (!options.l && !options.w && !options.c)
46+
console.log(
47+
` ${linesCounter} ${wordsCounter} ${characterCounter} ${path}`,
48+
);
49+
else {
50+
console.log(results.join(" ") + " " + path);
51+
}
52+
}
53+
if (paths.length > 1) {
54+
if (!options.l && !options.w && !options.c) {
55+
console.log(
56+
` ${total.linesCounter} ${total.wordsCounter} ${total.characterCounter} total`,
57+
);
58+
} else {
59+
const totalWithFlags = [];
60+
if (options.l) totalWithFlags.push(total.linesCounter);
61+
if (options.w) totalWithFlags.push(total.wordsCounter);
62+
if (options.c) totalWithFlags.push(total.characterCounter);
63+
console.log(totalWithFlags.join(" ") + " total");
64+
}
65+
}
66+
} catch (error) {
67+
console.error(error.message);
3068
}
31-
if (paths.length > 1)
32-
console.log(
33-
` ${total.linesCounter} ${total.wordsCounter} ${total.characterCounter} total`,
34-
);
35-
}catch(error){
36-
console.log(error.message);
37-
}

0 commit comments

Comments
 (0)