-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (47 loc) · 935 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/flan6/gwc/count"
"github.com/flan6/gwc/options"
)
func main() {
op := (&options.ArgOptions{}).Parse()
filesArgs := flag.Args()
if len(filesArgs) < 1 {
printRes(count.Count(os.Stdin), *op, "")
return
}
totalRes := count.Counts{}
for _, filePath := range filesArgs {
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
res := count.Count(file)
printRes(res, *op, file.Name())
totalRes.Add(res)
}
if len(filesArgs) > 1 {
printRes(totalRes, *op, "total")
}
}
func printRes(res count.Counts, op options.ArgOptions, name string) {
out := ""
if op.L {
out += fmt.Sprintf("%d\t", res.Lines)
}
if op.W {
out += fmt.Sprintf("%d\t", res.Words)
}
if op.C {
out += fmt.Sprintf("%d\t", res.Bytes)
}
if op.M {
out += fmt.Sprintf("%d\t", res.Characters)
}
fmt.Printf("%s%s\n", out, name)
}