-
Notifications
You must be signed in to change notification settings - Fork 6
/
option.go
93 lines (83 loc) · 1.71 KB
/
option.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
package main
import (
"flag"
"log"
"strings"
)
type corner struct {
topL bool
topR bool
bottomL bool
bottomR bool
}
type option struct {
rate float64
owrite bool
output string
prefix string
suffix string
corner *corner
}
func defaultOptions() *option {
return &option{
rate: 0.25,
output: "",
prefix: "",
suffix: "_rounded",
}
}
func parseOptions() *option {
var err error
opts := defaultOptions()
rate := flag.Float64("r", opts.rate, "rounding rate. 1 means circular. (0~1)")
corner := flag.String("c", "tl,tr,bl,br", "comma separated corners to round.")
owrite := flag.Bool("w", false, "if true, will overwrite the original files.")
output := flag.String("o", opts.output, "output file name for a single file.")
prefix := flag.String("p", opts.prefix, "prefix for the output file names.")
suffix := flag.String("s", opts.suffix, "suffix for the output file names.")
flag.Parse()
opts.rate, err = parseRate(*rate)
if err != nil {
log.Fatalln(err)
}
opts.corner, err = parseCorner(*corner)
if err != nil {
log.Fatalln(err)
}
opts.owrite = *owrite
opts.output = *output
opts.prefix = *prefix
opts.suffix = *suffix
return opts
}
func parseRate(rate float64) (float64, error) {
if rate < 0 || rate > 1 {
return 0, errInvalidRate
}
return rate, nil
}
func parseCorner(corners string) (*corner, error) {
cn := new(corner)
cs := strings.Split(corners, ",")
if len(cs) == 0 {
cn.topL = true
cn.topR = true
cn.bottomL = true
cn.bottomR = true
}
for _, c := range cs {
switch c {
case "tl":
cn.topL = true
case "tr":
cn.topR = true
case "bl":
cn.bottomL = true
case "br":
cn.bottomR = true
default:
return nil, errInvalidCorner
}
}
return cn, nil
}