-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (38 loc) · 950 Bytes
/
main.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/bryanhuhta/euler/internal/problem"
)
func main() {
id, params := mustParseArgs(os.Args[1:])
err := problem.Execute(id, params)
if err != nil {
exitWithMessage("error: %v", err)
}
}
func mustParseArgs(args []string) (int, problem.Params) {
if len(args) < 1 {
exitWithMessage("error: need at least 1 argument\n usage: euler id [key:value...]")
}
id, err := strconv.Atoi(args[0])
if err != nil {
exitWithMessage("error: id is not an integer: %v", err)
}
params := make(problem.Params, len(args[1:]))
for i, arg := range args[1:] {
tokens := strings.SplitN(arg, ":", 2)
if len(tokens) != 2 {
exitWithMessage("error: malformed parameter %d: %s", i, arg)
}
key, val := tokens[0], tokens[1]
params[key] = val
}
return id, params
}
func exitWithMessage(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}