@@ -2,10 +2,34 @@ import { program } from 'commander'
22import process from 'process'
33import fs from 'fs'
44
5- const argv = process . argv [ 2 ]
6- const data = fs . readFileSync ( `sample-files/${ argv } ` , 'utf-8' )
5+ // const argv = process.argv[4]
6+ // const data = fs.readFileSync(`sample-files/${argv}`, 'utf-8')
7+
78program
8- . command ( 'cat' )
9+ . command ( 'cat <file> ' )
910 . description ( 'Output file content' )
10- . option ( '-a, --all' , "show all users' processes" )
11- . action ( console . log ( data ) )
11+ . option ( '-a, --all' , 'Output all content' )
12+ . option ( '-n, --line' , 'Output each line with number' )
13+ . option ( '-b, --nonempty' , 'Output total lines not empty' )
14+ . action ( ( file , options ) => {
15+ const data = fs . readFileSync ( `sample-files/${ file } ` , 'utf-8' ) . split ( '\n' )
16+ if ( options . all ) {
17+ console . log ( data . join ( ) )
18+ }
19+ if ( options . line ) {
20+ for ( let i = 0 ; i < data . length ; i ++ ) {
21+ console . log ( `${ i } - ${ data [ i ] } ` )
22+ }
23+ }
24+ if ( options . nonempty ) {
25+ let totalline = 0
26+ data . forEach ( ( line ) => {
27+ if ( line . length != 0 ) {
28+ totalline ++
29+ }
30+ } )
31+ console . log ( totalline )
32+ }
33+ } )
34+
35+ program . parse ( )
0 commit comments