Skip to content

Commit 0d1f8b1

Browse files
committed
Add options for wc
1 parent 9f574ca commit 0d1f8b1

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ program
66
.name('wc')
77
.description('Counts the number of lines, words, and characters in a file.')
88
.argument('<path>', 'The path to the file to analyze')
9+
.option('-l, --lines', 'Only count lines')
10+
.option('-w, --words', 'Only count words')
11+
.option('-c, --characters', 'Only count characters');
912

1013
program.parse();
1114

@@ -17,9 +20,15 @@ if (argv.length != 1) {
1720
process.exit(1);
1821
}
1922
const path = argv[0];
23+
const options = program.opts();
24+
25+
let showLines = options.lines || (!options.words && !options.characters);
26+
let showWords = options.words || (!options.lines && !options.characters);
27+
let showCharacters = options.characters || (!options.lines && !options.words);
2028

2129
const content = await fs.readFile(path, 'utf-8');
30+
2231
const lineCount = content.split('\n').filter(Boolean).length;
2332
const wordCount = content.split(' ').filter(Boolean).length;
2433
const characterCount = content.length;
25-
console.log(` ${lineCount} ${wordCount} ${characterCount} ${path}`);
34+
console.log(` ${showLines ? lineCount : ''} ${showWords ? wordCount : ''} ${showCharacters ? characterCount : ''} ${path}`);

0 commit comments

Comments
 (0)