Skip to content

Commit c192601

Browse files
committed
fix wc flags and align behavior with Unix wc
1 parent 3c7c4f7 commit c192601

File tree

1 file changed

+23
-40
lines changed
  • implement-shell-tools/wc

1 file changed

+23
-40
lines changed

implement-shell-tools/wc/wc.js

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { program } from "commander";
22
import { promises as fs } from "node:fs";
3-
import process from "node:process";
43

54
program
65
.name("wc command")
76
.description("Implement wc command to count words and lines")
8-
.option("-l,", "show number of lines only")
9-
.option("-w,", "show number of words only")
10-
.option("-c,", "show number of characters only")
11-
.argument("[path...]", "The file path to process");
7+
.option("-l, --lines", "show number of lines only")
8+
.option("-w, --words", "show number of words only")
9+
.option("-c, --chars", "show number of characters only")
10+
.argument("[paths...]", "The file path to process");
1211

1312
program.parse(process.argv);
1413

1514
const opts = program.opts();
1615
let paths = program.args;
1716

1817
if (paths.length === 0) {
19-
console.error("wc: no file specified");
20-
process.exit(1);
18+
paths = ["."];
2119
}
2220

2321
let totals = { lines: 0, words: 0, chars: 0 };
@@ -28,53 +26,38 @@ for (const filePath of paths) {
2826
content = await fs.readFile(filePath, "utf8");
2927
} catch (err) {
3028
console.error(`wc: cannot read file "${filePath}": ${err.message}`);
31-
continue; // move on to next path
29+
continue;
3230
}
3331

34-
const lineCount = content.split("n").length;
35-
32+
const lineCount = content.split("\n").length;
3633
const wordCount = content.split(/\s+/).filter(Boolean).length;
37-
3834
const charCount = content.length;
3935

4036
totals.lines += lineCount;
4137
totals.words += wordCount;
4238
totals.chars += charCount;
4339

44-
// Decide what to print depending on flags
45-
if (!opts.l && !opts.w && !opts.c) {
46-
console.log(`${lineCount} ${wordCount} ${charCount} ${filePath}`);
47-
continue;
48-
}
49-
50-
if (opts.l) {
51-
console.log(`${lineCount} ${filePath}`);
52-
}
53-
54-
if (opts.w) {
55-
console.log(`${wordCount} ${filePath}`);
56-
}
40+
const output = [];
5741

58-
if (opts.c) {
59-
console.log(`${charCount} ${filePath}`);
42+
if (!opts.lines && !opts.words && !opts.chars) {
43+
output.push(lineCount, wordCount, charCount);
44+
} else {
45+
if (opts.lines) output.push(lineCount);
46+
if (opts.words) output.push(wordCount);
47+
if (opts.chars) output.push(charCount);
6048
}
49+
console.log(`${output.join(" ")} ${filePath}`);
6150
}
6251

63-
//Print totals if there are multiple files
6452
if (paths.length > 1) {
65-
if (!opts.l && !opts.w && !opts.c) {
66-
console.log(`${totals.lines} ${totals.words} ${totals.chars} total`);
67-
}
68-
69-
if (opts.l) {
70-
console.log(`${totals.lines} total`);
71-
}
72-
73-
if (opts.w) {
74-
console.log(`${totals.words} total`);
75-
}
53+
const totalOutput = [];
7654

77-
if (opts.c) {
78-
console.log(`${totals.chars} total`);
55+
if (!opts.lines && !opts.words && !opts.chars) {
56+
totalOutput.push(totals.lines, totals.words, totals.chars);
57+
} else {
58+
if (opts.lines) totalOutput.push(totals.lines);
59+
if (opts.words) totalOutput.push(totals.words);
60+
if (opts.chars) totalOutput.push(totals.chars);
7961
}
62+
console.log(`${totalOutput.join(" ")} total`);
8063
}

0 commit comments

Comments
 (0)