File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { program } from "commander" ;
22import * as fs from "node:fs/promises" ;
33
4- //*** TODO ***
5- // * Format results
6- // * Add optional flags
7-
84program
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
1321program . parse ( ) ;
1422
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 {
3037 } finally {
3138 await file . close ( ) ;
3239 }
33-
3440 results [ filePath ] = count ;
3541 }
3642
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}
You can’t perform that action at this time.
0 commit comments