77. description ( "displays the contents of a file" )
88. option ( "-n, --number" , "Number all output lines" )
99. option ( "-b, --number-nonblank" , "Number non-blank output lines only" )
10- . argument ( "<filepath >" ) ;
10+ . argument ( "<filepaths... >" ) ;
1111
1212program . parse ( ) ;
1313
@@ -20,21 +20,28 @@ if (args.length === 0) {
2020 program . help ( ) ;
2121}
2222
23- const path = args [ 0 ]
24- const content = await fs . readFile ( path , "utf-8" ) ;
25- const lines = content . split ( '\n' ) ;
26- if ( opts . number ) {
27- for ( let i = 0 ; i < lines . length ; i ++ ) {
28- console . log ( `${ i + 1 } \t${ lines [ i ] } ` ) ;
29- }
30- } else if ( opts . numberNonblank ) {
31- let lineNumber = 1 ;
32- for ( let i = 0 ; i < lines . length ; i ++ ) {
33- if ( lines [ i ] . trim ( ) !== '' ) {
34- console . log ( `${ lineNumber . toString ( ) . padStart ( 6 ) } \t${ lines [ i ] } ` ) ;
35- lineNumber ++ ;
23+ let globalLineNumber = 1 ;
24+
25+ for ( const path of args ) {
26+ try {
27+ const content = await fs . readFile ( path , "utf-8" ) ;
28+ const lines = content . split ( '\n' ) ;
29+
30+ if ( opts . number ) {
31+ lines . forEach ( ( line , idx ) => {
32+ console . log ( `${ idx + 1 } \t${ line } ` ) ;
33+ } ) ;
34+ } else if ( opts . numberNonblank ) {
35+ for ( const line of lines ) {
36+ if ( line . trim ( ) !== '' ) {
37+ console . log ( `${ globalLineNumber } \t${ line } ` ) ;
38+ globalLineNumber ++ ;
3639 }
37- }
38- } else console . log ( content )
39-
40-
40+ }
41+ } else {
42+ console . log ( content ) ;
43+ }
44+ } catch ( err ) {
45+ console . error ( `Error reading file "${ path } ": ${ err . message } ` ) ;
46+ }
47+ }
0 commit comments