-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.go
64 lines (57 loc) · 1.1 KB
/
args.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 (
"fmt"
"strconv"
"strings"
)
const (
defaultWidth = 512
defaultHeight = 512
)
type options struct {
width int
height int
}
func parseArgs(args []string) options {
options := options{
defaultWidth,
defaultHeight,
}
option := ""
for _, arg := range args {
if len(option) > 0 {
switch option {
case "width":
options.width, _ = strconv.Atoi(arg)
if options.width <= 0 {
fmt.Printf("Width must be positive '" + string(options.width) + "'\n")
}
break
case "height":
options.height, _ = strconv.Atoi(arg)
if options.height <= 0 {
fmt.Printf("Height must be positive '" + string(options.width) + "'\n")
}
break
default:
fmt.Printf("Unsupported option '" + option + "'\n")
}
option = ""
continue
} else if strings.HasPrefix(arg, "-") {
switch strings.ToLower(arg) {
case "-w", "--width":
option = "width"
continue
case "-h", "--height":
option = "height"
continue
case "-v", "--verbose":
continue
default:
}
}
fmt.Printf("Unsupported argument '" + arg + "'\n")
}
return options
}