File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
1013program . parse ( ) ;
1114
@@ -17,9 +20,15 @@ if (argv.length != 1) {
1720 process . exit ( 1 ) ;
1821}
1922const 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
2129const content = await fs . readFile ( path , 'utf-8' ) ;
30+
2231const lineCount = content . split ( '\n' ) . filter ( Boolean ) . length ;
2332const wordCount = content . split ( ' ' ) . filter ( Boolean ) . length ;
2433const characterCount = content . length ;
25- console . log ( ` ${ lineCount } ${ wordCount } ${ characterCount } ${ path } ` ) ;
34+ console . log ( ` ${ showLines ? lineCount : '' } ${ showWords ? wordCount : '' } ${ showCharacters ? characterCount : '' } ${ path } ` ) ;
You can’t perform that action at this time.
0 commit comments