-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.go
More file actions
63 lines (49 loc) · 1011 Bytes
/
args.go
File metadata and controls
63 lines (49 loc) · 1011 Bytes
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
package main
import (
"fmt"
"strconv"
"github.com/Fawers/dice-go/dice"
)
type args struct {
dieMaker func(uint64) dice.Die
maxValue uint64
numRolls uint
}
func parseArgs(argv []string) (*args, error) {
a := args{dice.New1Based, 6, 1}
switch len(argv) {
case 3:
rolls := argv[2]
numRolls, err := strconv.ParseUint(rolls, 10, 0)
if err != nil {
return nil, fmt.Errorf(
"failed to understand NUM_ROLLS: %q is not a uint", rolls)
}
a.numRolls = uint(numRolls)
fallthrough
case 2:
dieKind := argv[0]
switch dieKind {
case "1":
break
case "z":
a.dieMaker = dice.New0Based
case "L":
a.dieMaker = dice.NewLoaded
default:
return nil, fmt.Errorf(
"%q is not a valid die kind", dieKind)
}
argv[0] = argv[1]
fallthrough
case 1:
faces := argv[0]
var err error
a.maxValue, err = strconv.ParseUint(faces, 10, 64)
if err != nil {
return nil, fmt.Errorf(
"failed to understand FACES: %q is not a uint", faces)
}
}
return &a, nil
}