Skip to content

Commit 820efc8

Browse files
committed
Implemenet option to output all content, output each line with line number or output total line none empty
1 parent 4a25210 commit 820efc8

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,34 @@ import { program } from 'commander'
22
import process from 'process'
33
import 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+
78
program
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

Comments
 (0)