-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (69 loc) · 1.74 KB
/
main.go
File metadata and controls
77 lines (69 loc) · 1.74 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"fmt"
"io"
"os"
)
func main() {
var countBytesFlag bool
var countLinesFlag bool
var countWordsFlag bool
var countCharactersFlag bool
var fileName string
var file *os.File
var err error
defer file.Close()
flag.BoolVar(&countBytesFlag, "c", false, "Count Bytes Flag")
flag.BoolVar(&countLinesFlag, "l", false, "Count Lines Flag")
flag.BoolVar(&countWordsFlag, "w", false, "Count Words Flag")
flag.BoolVar(&countCharactersFlag, "m", false, "Count Characters Flag")
flag.Parse()
if len(flag.Args()) == 0 {
file, err = os.CreateTemp("", "stdin-")
if err != nil {
fmt.Println("Error creating temporary file:", err)
return
}
_, err = io.Copy(file, os.Stdin)
if err != nil {
fmt.Println("Error copying data to temporary file:", err)
return
}
} else {
fileName = flag.Args()[0]
file, err = os.Open(fileName)
if err != nil {
fmt.Print("File not found")
return
}
}
var processors []Processor
if countBytesFlag {
processors = append(processors, &ByteCountProcessor{})
}
if countLinesFlag {
processors = append(processors, &LineCountProcessor{})
}
if countWordsFlag {
processors = append(processors, &WordCountProcessor{})
}
if countCharactersFlag {
processors = append(processors, &CharacterCountProcessor{})
}
if len(processors) == 0 {
processors = append(processors, &LineCountProcessor{})
processors = append(processors, &WordCountProcessor{})
processors = append(processors, &ByteCountProcessor{})
}
var output string
for _, processor := range processors {
err = processor.process(file)
if err != nil {
fmt.Print("Unable to print file")
return
}
output = output + fmt.Sprintf("%d", processor.getValue()) + "\t"
}
fmt.Printf("%s %s \n", output, fileName)
}