-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
86 lines (78 loc) · 1.67 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
package main
import (
"fmt"
"os"
"strconv"
)
//Define default Parameters for server
const defaultWidth = 40
const defaultHeight = 40
const defaultNumOfPlayers = 2
const defaultMinDeadline = 5
const defaultDeadlineOffset = 5
// Config represents parsed configuration data
type Config struct {
Width int
Height int
Players int
}
func printUsageAndExit(status int) {
fmt.Printf(`Usage: %s [OPTION] ...
Host a server for spe_ed
-h height of the board
-p number of players (max: 63)
-w width of the board
-d set the minimal Deadline in seconds
-o set the maximal deadline offset in seconds
`, os.Args[0])
os.Exit(status)
}
func parseInt(arg string, minValue int) int {
i, err := strconv.Atoi(arg)
if err != nil {
printUsageAndExit(1)
}
if i < minValue {
printUsageAndExit(1)
}
return i
}
// GetConfig parses the program arguments and returns the configuration
func GetConfig() Config {
config := Config{defaultWidth, defaultHeight, defaultNumOfPlayers}
deadlineOffset = defaultDeadlineOffset
minDeadline = defaultMinDeadline
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "-w":
i++
config.Width = parseInt(os.Args[i], 2)
break
case "-h":
i++
config.Height = parseInt(os.Args[i], 2)
break
case "-p":
i++
config.Players = parseInt(os.Args[i], 2)
// We use a bitmask with the players' id, so the maximum id is 63
if config.Players > 63 {
printUsageAndExit(1)
}
break
case "-o":
i++
deadlineOffset = parseInt(os.Args[i], 1)
break
case "-d":
i++
minDeadline = parseInt(os.Args[i], 1)
break
case "--help":
printUsageAndExit(0)
default:
printUsageAndExit(1)
}
}
return config
}