-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
75 lines (67 loc) · 1.96 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
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
)
func parseArguments() {
showError := func(msg string) {
fmt.Println(msg)
flag.Usage()
os.Exit(1)
}
helpAlgos := func() string {
as := make([]string, len(algos))
i := 0
for k := range algos {
as[i] = fmt.Sprintf("%q", string(k))
i++
}
sort.Strings(as)
return strings.Join(as, ", ")
}
var showHelp bool
var _algorithm string
var _windowWidth int
var _windowHeight int
flag.BoolVar(&showHelp, "help", false, "Show usage info")
flag.BoolVar(&showHelp, "h", false, "Show usage info")
flag.IntVar(&_windowWidth, "width", 800, "Window's width")
flag.IntVar(&_windowHeight, "height", 600, "Window's height")
flag.IntVar(&size, "size", 100, "Size of the array to sort")
flag.IntVar(&speed, "speed", 2, "Step duration in milliseconds")
flag.IntVar(&startDelay, "delay", 0, "Initial delay before running the animation")
flag.BoolVar(&debug, "debug", false, "Enable debug info")
flag.BoolVar(&dataAsc, "asc", false, "Data set is already sorted")
flag.BoolVar(&dataDesc, "desc", false, "Data set is in descending")
flag.Int64Var(&dataRndSeed, "seed", 0, "Random seed to generate the data set. 0 means autogenerated")
flag.StringVar(&_algorithm, "algo", string(quickSort), "One of: "+helpAlgos())
flag.Parse()
if showHelp {
flag.Usage()
os.Exit(0)
}
if _windowWidth <= 0 {
showError("Window's width must be positive")
}
windowWidth = float64(_windowWidth)
if _windowHeight <= 0 {
showError("Window0s height must be positive")
}
if size <= 1 {
showError("Size bust be at least 2")
}
if dataRndSeed < 0 {
showError("Random seed must be non negative")
}
if dataRndSeed > 0 && (dataAsc || dataDesc) || (dataAsc && dataDesc) {
showError("You can't combine ascending, descending options and random seed")
}
windowHeight = float64(_windowHeight)
algorithm = algo(_algorithm)
if _, found := algos[algorithm]; !found {
showError(fmt.Sprintf("Unknown algorithm %q", _algorithm))
}
}