-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharguments.go
86 lines (72 loc) · 1.89 KB
/
arguments.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 shireikan
import "strconv"
// Argument extends string to provide general
// transformation functionality.
type Argument string
// AsString returns the argument as string.
func (a Argument) AsString() string {
return string(a)
}
// AsInt tries to parse the given argument
// as integer. If this fails, an error is
// returned.
func (a Argument) AsInt() (int, error) {
return strconv.Atoi(a.AsString())
}
// AsFloat64 tries to parse the given argument
// as float64. If this fails, an error is
// returned.
func (a Argument) AsFloat64() (float64, error) {
return strconv.ParseFloat(a.AsString(), 64)
}
// AsBool tries to parse the given argument
// as bool. If this fails, an error is
// returned.
//
// As described in the strconv.ParseBool docs,
// the following values are accepted:
// "It accepts 1, t, T, TRUE, true, True, 0, f, F,
// FALSE, false, False. Any other value returns
// an error."
func (a Argument) AsBool() (bool, error) {
return strconv.ParseBool(a.AsString())
}
// ArgumentList wraps a string list to get
// arguments in that list as Argument object.
type ArgumentList []string
// Get returns the Argument at the given Index.
// If there is no argument at that index, an
// empty string is returned.
func (al ArgumentList) Get(i int) Argument {
if i < 0 || i >= len(al) {
return Argument("")
}
return Argument(al[i])
}
// IndexOf returns the index of v in arr.
// If not found, the returned index is -1.
func (al ArgumentList) IndexOf(v string) int {
for i, s := range al {
if v == s {
return i
}
}
return -1
}
// Contains returns true when v is included
// in arr.
func (al ArgumentList) Contains(v string) bool {
return al.IndexOf(v) > -1
}
// Splice returns a new array sliced at i by
// the range of r.
func (al ArgumentList) Splice(i, r int) ArgumentList {
l := len(al)
if i >= l {
return al
}
if i+r >= l {
return al[:i]
}
return append(al[:i], al[i+r:]...)
}