-
Notifications
You must be signed in to change notification settings - Fork 0
/
gowc.go
47 lines (37 loc) · 1.01 KB
/
gowc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"flag"
"fmt"
"gowc/countutils"
"gowc/fileutils"
)
func main() {
var (
bytesVar bool
linesVar bool
wordsVar bool
charVar bool
)
flag.BoolVar(&bytesVar, "c", false, "Count the number of bytes in the input file")
flag.BoolVar(&linesVar, "l", false, "Count the number of new line characters in the input file")
flag.BoolVar(&wordsVar, "w", false, "Count the number of words in the input file")
flag.BoolVar(&charVar, "m", false, "Count the number of characters in the input file")
flag.Parse()
filenames := flag.Args()
for fileIdx := 0; fileIdx < len(filenames); fileIdx++ {
fileContent, _ := fileutils.ReadTextFile(string(filenames[fileIdx]))
if linesVar {
lineCount := countutils.CountLines(fileContent)
fmt.Print(lineCount, " ")
}
if wordsVar {
wordCount := countutils.CountWords(fileContent)
fmt.Print(wordCount, " ")
}
if bytesVar {
charCount := countutils.CountBytes(fileContent)
fmt.Print(charCount, " ")
}
fmt.Println(filenames[fileIdx])
}
}