-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (49 loc) · 1.28 KB
/
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
64
package main
import (
"flag"
"fmt"
"io"
"os"
)
func main() {
output := ""
byteFlag := flag.Bool("c", false, "Output the number of bytes in a file.")
lineFlag := flag.Bool("l", false, "Output the number of lines in a file.")
wordFlag := flag.Bool("w", false, "Output the number of words in a file.")
charFlag := flag.Bool("m", false, "Output the number of characters in a file.")
flag.Parse()
args := flag.Args()
var fileData []byte
if len(args) > 0 {
// Read from file
file, err := os.ReadFile(args[0])
if err != nil {
fmt.Println("Error reading file:", err)
return
}
fileData = file
} else {
// Read from standard input
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
fileData = stdin
}
switch {
case *charFlag:
output += fmt.Sprintf("%d ", CalculateNoOfChars(fileData))
case *lineFlag:
output += fmt.Sprintf("%d ", CalculateNoOfLines(fileData))
case * wordFlag:
output += fmt.Sprintf("%d ", CalculateNoOfWords(fileData))
case *byteFlag:
output += fmt.Sprintf("%d ", CalculateNoOfBytes(fileData))
default:
output += fmt.Sprintf("%d %d %d ", CalculateNoOfLines(fileData), CalculateNoOfWords(fileData), CalculateNoOfBytes(fileData))
}
if len(args) > 0 {
output += args[0]
}
fmt.Println(output)
}