-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
127 lines (117 loc) · 3.16 KB
/
config.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"strconv"
"strings"
)
const bUFFER = 1048576 // 1024x1024
const dISPLAY1 = 20 // Length of displayed encoded text in verbose mode
const dISPLAY2 = 16 // Length of displayed decoded bytes in verbose mode
type Config struct {
Command uint8 // 1 - encode; 2 - decode
Input string // nil - stdin
Output string // nil - stdout
Buffer int // buffer size
Verbose bool
}
func usage() string {
return "Usage:\n basesf [encode | decode | version | help]\n" +
" {-i FILE | --in=FILE}\n" +
" {-o FILE | --out=FILE}\n" +
" {-b SIZE | --buffer=SIZE}\n" +
" {-v | --verbose}"
}
func help() string {
return fmt.Sprintf("Usage: basesf [commands] {options}\n"+
" * commands:\n"+
" encode - convert input into base64 encoded string\n"+
" decode - convert base64 encoded string back to the original form\n"+
" version - display current version of 'basesf'\n"+
" help - display this message\n\n"+
" * options:\n"+
" -i FILE, --in=FILE\n"+
" name of the input file, omitting means input from stdin\n"+
" -o FILE, --out=FILE\n"+
" name of the output file, omitting means output to stdout\n"+
" -b SIZE, --buffer=SIZE\n"+
" size of the read buffer (SIZE default: %vKB)\n"+
" -v, --verbose\n"+
" display detail operation messages during processing\n\n"+
" NOTE: type a period (.) then press <enter> in a new line to finish\n when inputting interactively from stdin", bUFFER/1024)
}
func parse(args []string) (cfg *Config, err error) {
if len(args) < 2 {
return nil, &Err{1, "Command missing"}
}
cfg = &Config{
Buffer: bUFFER,
Verbose: false,
}
switch args[1] {
case "encode":
cfg.Command = 0
case "decode":
cfg.Command = 1
case "help":
cfg.Command = 2
case "version":
cfg.Command = 3
default:
return nil, &Err{1, fmt.Sprintf("Invalid command '%v'", args[1])}
}
var val int
for i := 2; i < len(args); i++ {
switch {
case args[i] == "-v" || args[i] == "--verbose":
cfg.Verbose = true
case args[i] == "-i":
i++
if i >= len(args) {
return nil, &Err{2, "Missing input filename argument"}
} else {
cfg.Input = args[i]
}
case strings.HasPrefix(args[i], "--in="):
if len(args[i]) <= 5 {
return nil, &Err{2, "Missing input filename"}
} else {
cfg.Input = args[i][5:]
}
case args[i] == "-o":
i++
if i >= len(args) {
return nil, &Err{3, "Missing output filename argument"}
} else {
cfg.Output = args[i]
}
case strings.HasPrefix(args[i], "--out="):
if len(args[i]) <= 6 {
return nil, &Err{3, "Missing out filename"}
} else {
cfg.Output = args[i][6:]
}
case args[i] == "-b":
i++
if i >= len(args) {
return nil, &Err{4, "Missing buffer size argument"}
} else {
val, err = strconv.Atoi(args[i])
if err == nil {
cfg.Buffer = val
}
}
case strings.HasPrefix(args[i], "--buffer="):
if len(args[i]) <= 9 {
return nil, &Err{4, "Missing buffer size"}
} else {
val, err = strconv.Atoi(args[i][9:])
if err == nil {
cfg.Buffer = val
}
}
default:
return nil, &Err{0, fmt.Sprintf("Invalid option '%v'", args[i])}
}
}
return
}