Skip to content

Commit 50ff285

Browse files
authored
Fixed argument bug (#2)
- Arguments with dashes in the middle where interpreted as flags
1 parent 90106db commit 50ff285

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
module github.com/libgolang/props
2+
3+
go 1.12

lib.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ type propsStruct struct {
1313

1414
var (
1515
globalProps *propsStruct
16-
propEqRegex = regexp.MustCompile(`--([\w\-]+)=(.*)$`)
17-
propRegex = regexp.MustCompile(`--([\w\-]+)`)
18-
propFlagRegex = regexp.MustCompile(`-(\w+)`)
16+
propEqRegex = regexp.MustCompile(`^--([\w\-]+)=(.*)$`)
17+
propRegex = regexp.MustCompile(`^--([\w\-]+)`)
18+
propFlagRegex = regexp.MustCompile(`^-(\w+)`)
1919
)
2020

2121
func init() {

lib_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,36 @@ func TestGetProp(t *testing.T) {
4949
}
5050
}
5151
}
52+
53+
func TestInvalidProps(t *testing.T) {
54+
// given
55+
args := []string{
56+
"arg1",
57+
"x-x",
58+
"y-y",
59+
"bool--bool",
60+
"prop",
61+
"prop-val",
62+
}
63+
64+
// when
65+
globalProps = parseArgs(args)
66+
67+
resultArgs := GetArgs()
68+
69+
// then
70+
a := len(resultArgs)
71+
b := len(args)
72+
if a != b {
73+
t.Errorf("Expected `%d` arguments, but got `%d`", b, a)
74+
} else {
75+
var i int = 0
76+
for _, v := range args {
77+
if v != resultArgs[i] {
78+
t.Errorf("arg `%s` should match %s", v, resultArgs[i])
79+
}
80+
i++
81+
}
82+
}
83+
84+
}

0 commit comments

Comments
 (0)