Skip to content

Commit fe2cf56

Browse files
committed
Merge similar code.
1 parent d7e6fc0 commit fe2cf56

22 files changed

+750
-889
lines changed

flags/args/args.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,47 @@ type Args map[string]interface {
1818
func New() Args {
1919
return make(Args)
2020
}
21+
22+
// Bool pulls a value as a boolean, this
23+
// supports *bool, and bool, because either
24+
// can be given depending on flags
25+
func (a Args) Bool(k string) bool {
26+
o, ok := a[k].(bool)
27+
if ok {
28+
return o
29+
}
30+
31+
v := *a[k].(*bool)
32+
return bool(v)
33+
}
34+
35+
// String pulls a value as a boolean, this
36+
// supports *bool, and bool, because either
37+
// can be given depending on flags
38+
func (a Args) String(k string) string {
39+
v, ok := a[k]
40+
if !ok {
41+
return ""
42+
}
43+
44+
if s, ok := v.(string); !ok {
45+
if s, ok := v.(*string); ok && s != nil {
46+
return string(*s)
47+
}
48+
} else {
49+
return s
50+
}
51+
52+
return ""
53+
}
54+
55+
// IsEmpty Allows you to check if a value was
56+
// given, this means that it wasn't nil, that it
57+
// exists on the map, and that it's not empty.
58+
func (a Args) IsEmpty(k string) bool {
59+
if v := a.String(k); v == "" {
60+
return true
61+
}
62+
63+
return false
64+
}

flags/args/args_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,90 @@ func TestNew(t *testing.T) {
1515
assert.IsType(t, expected,
1616
actual)
1717
}
18+
19+
func TestBool(t *testing.T) {
20+
type TestStruct struct {
21+
input *bool
22+
description string
23+
expected bool
24+
}
25+
26+
for _, ts := range []TestStruct{
27+
TestStruct{
28+
input: &[]bool{false}[0],
29+
description: "it's false when false",
30+
expected: false,
31+
},
32+
TestStruct{
33+
input: &[]bool{true}[0],
34+
description: "it's true when true",
35+
expected: true,
36+
},
37+
} {
38+
args := Args{
39+
"k": ts.input,
40+
}
41+
42+
actual := args.Bool("k")
43+
assert.Equal(t, ts.expected, actual,
44+
ts.description)
45+
}
46+
}
47+
48+
func TestString(t *testing.T) {
49+
type TestStruct struct {
50+
input *string
51+
description string
52+
expected string
53+
}
54+
55+
for _, ts := range []TestStruct{
56+
TestStruct{
57+
input: &[]string{""}[0],
58+
description: "it returns a string",
59+
expected: "",
60+
},
61+
} {
62+
args := Args{
63+
"k": ts.input,
64+
}
65+
66+
actual := args.String("k")
67+
assert.Equal(t, ts.expected, actual,
68+
ts.description)
69+
}
70+
}
71+
72+
func TestIsEmpty(t *testing.T) {
73+
type TestStruct struct {
74+
input *string
75+
description string
76+
expected bool
77+
}
78+
79+
for _, ts := range []TestStruct{
80+
TestStruct{
81+
expected: true,
82+
description: "is true on nil",
83+
input: nil,
84+
},
85+
TestStruct{
86+
input: &[]string{"string"}[0],
87+
description: "it's false on non-empty string",
88+
expected: false,
89+
},
90+
TestStruct{
91+
input: &[]string{""}[0],
92+
description: "it's true on \"\"",
93+
expected: true,
94+
},
95+
} {
96+
args := Args{
97+
"k": ts.input,
98+
}
99+
100+
actual := args.IsEmpty("k")
101+
assert.Equal(t, ts.expected, actual,
102+
ts.description)
103+
}
104+
}

flags/args/getters.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

flags/args/getters_test.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

flags/args/test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

flags/args/test_test.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

template/handle.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)