Repository navigation
Expand file tree
/
Copy pathflags.go
More file actions
186 lines (153 loc) · 4.27 KB
/
Copy pathflags.go
File metadata and controls
186 lines (153 loc) · 4.27 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"os"
"strconv"
"strings"
"time"
)
type Flags struct {
// Blizzard flags
// DisplayMode is the display mode to use.
//
// * 0: windowed (default)
// * 1: fullscreen
DisplayMode int
// WindowWidth is the width of the window in pixels.
WindowWidth int
// WindowHeight is the height of the window in pixels
WindowHeight int
// WindowX is the horizontal placement of the window in pixels.
WindowX int
// WindowY is the vertical placement of the window in pixels.
WindowY int
// Listen is the IP address to listen on.
//
// Default: 127.0.0.1
Listen string
// Port is the port to listen on.
//
// Default: 8168
Port int
// Realtime is whether to run StarCraft II in real-time or not.
//
// - 0: Do not run in real-time (default)
// - 1: Run in real-time
Realtime bool
// Timeout for how long the library will block for a response.
Timeout time.Duration
// Replay is the path to a replay file.
Replay string
// Map is the name of a map to load.
Map string
}
func loadFlags() Flags {
parsed := ParseFlags()
flags := Flags{}
flags.Realtime = flagBool(parsed, "realtime", false)
flags.DisplayMode = flagInt(parsed, "displaymode", 0)
flags.Port = flagInt(parsed, "port", 8168)
flags.WindowHeight = flagInt(parsed, "windowheight", 0)
flags.WindowWidth = flagInt(parsed, "windowwidth", 0)
flags.WindowX = flagInt(parsed, "windowx", 0)
flags.WindowY = flagInt(parsed, "windowy", 0)
flags.Listen = flagString(parsed, "listen", "127.0.0.1")
flags.Replay = flagString(parsed, "replay", "")
flags.Map = flagString(parsed, "map", "")
twoMinutes, _ := time.ParseDuration("2m")
flags.Timeout = flagDuration(parsed, "timeout", twoMinutes)
return flags
}
// flagBool gets a boolean flag value from the provided flag map
func flagBool(flags map[string]string, name string, fallback bool) bool {
if val, ok := flags[name]; ok {
switch strings.ToLower(val) {
case "true", "1", "t", "yes", "y":
return true
case "false", "0", "f", "no", "n":
return false
}
}
return fallback
}
// flagInt gets an integer flag value from the provided flag map
func flagInt(flags map[string]string, name string, fallback int) int {
if value, ok := flags[name]; ok {
if i, err := strconv.Atoi(value); err == nil {
return i
}
}
return fallback
}
// flagString gets a string flag value from the provided flag map
func flagString(flags map[string]string, name, fallback string) string {
if value, ok := flags[name]; ok {
return value
}
return fallback
}
// flagDuration gets a duration flag value from the provided flag map
func flagDuration(flags map[string]string, name string, fallback time.Duration) time.Duration {
if value, ok := flags[name]; ok {
if d, err := time.ParseDuration(value); err == nil {
return d
}
}
return fallback
}
// ParseFlags parses command-line arguments directly and bypasses the flag
// package because some library decided it was a good idea to declare flags in
// `init` and keep them private.
func ParseFlags() map[string]string {
flags := make(map[string]string)
// Skip the program name
args := os.Args[1:]
for i := 0; i < len(args); i++ {
advance, name, value := ParseArg(args, i)
if name != "" {
flags[name] = value
}
// Skip any used arguments
i += advance
}
return flags
}
// ParseArg processes a single argument and returns:
// - how many additional arguments were consumed other than the current one
// - the flag name (if it's a flag)
// - the flag value
//
// Returns empty strings for name and value if the argument is not a flag
func ParseArg(args []string, index int) (advance int, name, value string) {
if index >= len(args) {
return 0, "", ""
}
arg := args[index]
// Check if it's a flag (starts with -)
if len(arg) <= 1 || arg[0] != '-' {
// Not a flag
return 0, "", ""
}
// Handle --flags
if arg[1] == '-' && len(arg) > 2 {
// Remove --
arg = arg[2:]
} else {
// Remove -
arg = arg[1:]
}
// Handle name=value
if idx := strings.Index(arg, "="); idx != -1 {
name = arg[:idx]
value = arg[idx+1:]
return 0, name, value
}
// Handle name value
name = arg
if index+1 < len(args) && !strings.HasPrefix(args[index+1], "-") {
value = args[index+1]
// Skip the next arg since we used it
return 1, name, value
}
// Flag without value (true)
return 0, name, "true"
}