-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (72 loc) · 1.52 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"gopress/commands"
"gopress/shared"
"os"
)
func usage() {
fmt.Println("Usage: gopress <input-file> (-d/-e) [flags]")
fmt.Println("-e - encode mode")
fmt.Println("-d - decode mode")
fmt.Println("-o - output file")
fmt.Println("-h/--help - show this message")
os.Exit(1)
}
func parse_args() (config shared.Config) {
args := os.Args[1:]
if len(args) < 1 {
fmt.Println("Input file is missing.")
usage()
}
config.Filename = args[0]
if config.Filename == "-h" || config.Filename == "--help" {
usage()
}
args = args[1:]
var is_encode, is_decode, is_output bool
for _, arg := range args {
if is_output {
config.Output = arg
is_output = false
}
if arg == "--help" || arg == "-h" {
usage()
}
if arg == "-o" {
if config.Output != "" {
fmt.Println("Argument -o provided multiple times.")
usage()
}
is_output = true
}
if arg == "-e" {
is_encode = true
}
if arg == "-d" {
is_decode = true
}
}
if !((is_encode || is_decode) && !(is_encode && is_decode)) { // NOT XOR
fmt.Println("You must provide either -d or -e.")
usage()
}
if is_encode {
config.Mode = shared.ENCODE_MODE
} else {
config.Mode = shared.DECODE_MODE
}
return
}
func main() {
config := parse_args()
input, err := os.ReadFile(config.Filename)
if err != nil {
panic(err)
}
if config.Mode == shared.ENCODE_MODE {
commands.Encode(config, input)
} else {
commands.Decode(config, input)
}
}