Skip to content

Commit 43510be

Browse files
committed
Add -l -w -c flags to wc
1 parent d2e94fc commit 43510be

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@ import { promises as fs } from "node:fs";
33

44
const args = process.argv.slice(2);
55

6-
if (args.length === 0) {
6+
let option = "all";
7+
const paths = [];
8+
9+
// parse args
10+
for (const arg of args) {
11+
if (arg === "-l") option = "l";
12+
else if (arg === "-w") option = "w";
13+
else if (arg === "-c") option = "c";
14+
else paths.push(arg);
15+
}
16+
17+
if (paths.length === 0) {
718
console.error("Please provide at least one file");
819
process.exit(1);
920
}
1021

11-
for (const path of args) {
22+
for (const path of paths) {
1223
const content = await fs.readFile(path, "utf-8");
1324

1425
const lines = content.split("\n").length;
1526
const words = content.split(" ").length;
1627
const chars = content.length;
1728

18-
console.log(lines, words, chars, path);
29+
if (option === "l") {
30+
console.log(lines, path);
31+
} else if (option === "w") {
32+
console.log(words, path);
33+
} else if (option === "c") {
34+
console.log(chars, path);
35+
} else {
36+
console.log(lines, words, chars, path);
37+
}
1938
}

0 commit comments

Comments
 (0)