Skip to content

Commit 0071dc4

Browse files
committed
feat(wc): add optional flags for line, word, and byte counting
1 parent fafe5fd commit 0071dc4

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { program } from "commander";
22
import * as fs from "node:fs/promises";
33

4-
//*** TODO ***
5-
// * Format results
6-
// * Add optional flags
7-
84
program
95
.name("wc")
106
.description("word, line and byte count")
11-
.argument("<paths...>", "The file path(s) to process");
7+
.argument("<paths...>", "The file path(s) to process.")
8+
.option(
9+
"-l, --lines",
10+
"The number of lines in each input file is written to the standard output.",
11+
)
12+
.option(
13+
"-w, --words",
14+
"The number of words in each input file is written to the standard output.",
15+
)
16+
.option(
17+
"-c --bytes",
18+
"The number of bytes in each input file is written to the standard output.",
19+
);
1220

1321
program.parse();
1422

@@ -19,7 +27,6 @@ try {
1927
for (const filePath of filePaths) {
2028
const file = await fs.open(filePath);
2129
const stats = await fs.stat(filePath);
22-
2330
const count = { lines: 0, words: 0, bytes: stats.size };
2431

2532
try {
@@ -30,7 +37,6 @@ try {
3037
} finally {
3138
await file.close();
3239
}
33-
3440
results[filePath] = count;
3541
}
3642

@@ -44,7 +50,13 @@ try {
4450
results["total"] = total;
4551
}
4652

47-
console.table(results);
53+
const options = program.opts();
54+
const noOptionsProvided = !Object.keys(options).length;
55+
const selectedOptionKeys = [...Object.keys(options)];
56+
57+
noOptionsProvided
58+
? console.table(results)
59+
: console.table(results, selectedOptionKeys);
4860
} catch (err) {
4961
console.error(err.message);
5062
}

0 commit comments

Comments
 (0)