File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ import { program } from "commander" ;
2+ import { promises as fs } from "node:fs"
3+
4+
5+
6+ //configuring the program here to run flags.
7+
8+ program
9+ . name ( "cat" )
10+ . description ( "Concatenate and print files" )
11+ . option ( "-n, --number" , "Number all output lines" )
12+ . option ( "-b, --number-nonblank" , "Number non-blank output lines" )
13+ . argument ( "<files...>" , "File paths to display" )
14+ . parse ( process . argv ) ; //Parse command line arguments (reads process.argv and interprets it)
15+
16+ const options = program . opts ( ) ;
17+ const files = program . args ; // Array of file paths passed as arguments
18+
19+ let lineNumber = 1 ; // shared across all files, like GNU cat -n)
20+
21+ for ( const file of files ) {
22+ const content = await fs . readFile ( file , "utf-8" ) ;
23+ const lines = content . split ( "\n" ) ;
24+
25+ for ( const line of lines ) {
26+
27+ if ( options . number ) {
28+ console . log ( `${ lineNumber . toString ( ) . padStart ( 6 ) } ${ line } ` ) ; // adding padding for formatting
29+ lineNumber ++ ;
30+ }
31+ else {
32+ console . log ( line ) ;
33+ }
34+ }
35+ }
36+
37+
38+
You can’t perform that action at this time.
0 commit comments