-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
41 lines (34 loc) · 1020 Bytes
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
)
func main() {
var commandLineOptions Options
flag.BoolVar(&commandLineOptions.printBytes, "c", false, "Count bytes")
flag.BoolVar(&commandLineOptions.printLines, "l", false, "Count lines")
flag.BoolVar(&commandLineOptions.printWords, "w", false, "Count words")
flag.BoolVar(&commandLineOptions.printChars, "m", false, "Count characters")
flag.Parse()
if !commandLineOptions.printBytes &&
!commandLineOptions.printLines &&
!commandLineOptions.printWords &&
!commandLineOptions.printChars {
commandLineOptions.printBytes = true
commandLineOptions.printWords = true
commandLineOptions.printLines = true
}
filenames := flag.CommandLine.Args()
run(filenames, commandLineOptions)
}
func run(filenames []string, options Options) {
if len(filenames) == 0 {
reader := bufio.NewReader(os.Stdin)
fileStats := CalculateStats(reader)
fmt.Println(formatStats(options, fileStats, ""))
} else {
CalculateStatsForFiles(filenames, options)
}
}