Skip to content
This repository was archived by the owner on Dec 13, 2022. It is now read-only.

Commit a2c3ecb

Browse files
committed
simplified flags package
using same logic as flag.Duration
1 parent 4092ba7 commit a2c3ecb

File tree

7 files changed

+39
-52
lines changed

7 files changed

+39
-52
lines changed

flags/intlist.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
type intlist struct {
1111
min int
1212
max int
13-
list []int
13+
list *[]int
1414
}
1515

1616
func (l *intlist) String() string {
17-
s := make([]string, len(l.list))
18-
for i := range l.list {
19-
s[i] = strconv.Itoa(l.list[i])
17+
s := make([]string, len(*l.list))
18+
for i := range *l.list {
19+
s[i] = strconv.Itoa((*l.list)[i])
2020
}
2121
return strings.Join(s, ",")
2222
}
@@ -34,17 +34,14 @@ func (l *intlist) Set(s string) error {
3434
if x > l.max {
3535
return fmt.Errorf("%d is bigger than maximum %d", x, l.max)
3636
}
37-
l.list = append(l.list, x)
37+
*l.list = append(*l.list, x)
3838
}
3939
return nil
4040
}
4141

4242
// Intlist defines a flag for a comma-separated list of integers.
43-
// Call the returned function after flag.Parse to get the value.
44-
func Intlist(name, usage string, min, max int) func() []int {
45-
l := &intlist{min: min, max: max}
46-
flag.Var(l, name, usage)
47-
return func() []int {
48-
return l.list
49-
}
43+
func Intlist(name, usage string, min, max int) *[]int {
44+
l := &[]int{}
45+
flag.Var(&intlist{list: l, min: min, max: max}, name, usage)
46+
return l
5047
}

flags/intlist_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestIntlist(t *testing.T) {
2020
}
2121

2222
for i, tt := range tests {
23-
l := intlist{min: 0, max: 10}
23+
l := intlist{min: 0, max: 10, list: &[]int{}}
2424
if err := l.Set(tt.text); err != nil {
2525
if !tt.invalid {
2626
t.Errorf("parsing %s failed unexpectedly: %v", tt.text, err)
@@ -31,7 +31,7 @@ func TestIntlist(t *testing.T) {
3131
t.Errorf("parsing %s should have failed", tt.text)
3232
continue
3333
}
34-
if !equal(l.list, tt.parsed) {
34+
if !equal(*l.list, tt.parsed) {
3535
t.Errorf(`
3636
%d.
3737
Input: %s

flags/monthlist.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import (
88
"time"
99
)
1010

11-
type monthlist struct {
12-
list []time.Month
13-
}
11+
type monthlist []time.Month
1412

1513
func (l *monthlist) String() string {
16-
s := make([]string, len(l.list))
17-
for i := range l.list {
18-
s[i] = strconv.Itoa(int(l.list[i]))
14+
s := make([]string, len(*l))
15+
for i := range *l {
16+
s[i] = strconv.Itoa(int((*l)[i]))
1917
}
2018
return strings.Join(s, ",")
2119
}
@@ -31,18 +29,15 @@ func (l *monthlist) Set(s string) error {
3129
if x < 1 || x > 12 {
3230
return fmt.Errorf("invalid month: %d", x)
3331
}
34-
l.list = append(l.list, x)
32+
*l = monthlist(append(*l, x))
3533
}
3634
return nil
3735
}
3836

3937
// Monthlist defines a flag for a comma-separated list of months.
4038
// Valid values are between 1 and 12.
41-
// Call the returned function after flag.Parse to get the value.
42-
func Monthlist(name, usage string) func() []time.Month {
43-
l := &monthlist{}
44-
flag.Var(l, name, usage)
45-
return func() []time.Month {
46-
return l.list
47-
}
39+
func Monthlist(name, usage string) *[]time.Month {
40+
l := &[]time.Month{}
41+
flag.Var((*monthlist)(l), name, usage)
42+
return l
4843
}

flags/monthlist_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func TestMonthlist(t *testing.T) {
3333
t.Errorf("parsing %s should have failed", tt.text)
3434
continue
3535
}
36-
if !equalMonth(l.list, tt.parsed) {
36+
if !equalMonth(l, tt.parsed) {
3737
t.Errorf(`
3838
%d.
3939
Input: %s
4040
Expected: %v
41-
Got %v`, i, tt.text, tt.parsed, l.list)
41+
Got %v`, i, tt.text, tt.parsed, l)
4242
}
4343
}
4444

flags/weekdaylist.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ var strDays = map[string]time.Weekday{
1717
"su": time.Sunday,
1818
}
1919

20-
type weekdaylist struct {
21-
list []time.Weekday
22-
}
20+
type weekdaylist []time.Weekday
2321

2422
func (l *weekdaylist) String() string {
25-
s := make([]string, len(l.list))
26-
for i := range l.list {
27-
s[i] = dayToString(l.list[i])
23+
s := make([]string, len(*l))
24+
for i := range *l {
25+
s[i] = dayToString((*l)[i])
2826
}
2927
return strings.Join(s, ",")
3028
}
@@ -45,18 +43,15 @@ func (l *weekdaylist) Set(s string) error {
4543
if !ok {
4644
return fmt.Errorf("invalid day at index %d: %s", i, d)
4745
}
48-
l.list = append(l.list, x)
46+
*l = weekdaylist(append(*l, x))
4947
}
5048
return nil
5149
}
5250

5351
// Weekdaylist defines a flag for a comma-separated list of week days.
5452
// Valid values are mo, tu, we, th, fr, sa, su.
55-
// Call the returned function after flag.Parse to get the value.
56-
func Weekdaylist(name, usage string) func() []time.Weekday {
57-
l := &weekdaylist{}
58-
flag.Var(l, name, usage)
59-
return func() []time.Weekday {
60-
return l.list
61-
}
53+
func Weekdaylist(name, usage string) *[]time.Weekday {
54+
l := &[]time.Weekday{}
55+
flag.Var((*weekdaylist)(l), name, usage)
56+
return l
6257
}

flags/weekdaylist_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func TestWeekdaylist(t *testing.T) {
3333
t.Errorf("parsing %s should have failed", tt.text)
3434
continue
3535
}
36-
if !equalWeekday(l.list, tt.parsed) {
36+
if !equalWeekday(l, tt.parsed) {
3737
t.Errorf(`
3838
%d.
3939
Input: %s
4040
Expected: %v
41-
Got %v`, i, tt.text, tt.parsed, l.list)
41+
Got %v`, i, tt.text, tt.parsed, l)
4242
}
4343
}
4444

main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ func main() {
7575
now := time.Now()
7676

7777
next := match.Next(now, match.Condition{
78-
Month: month(),
79-
Weekday: weekday(),
80-
Day: day(),
81-
Hour: hour(),
82-
Minute: minute(),
83-
Second: second(),
78+
Month: *month,
79+
Weekday: *weekday,
80+
Day: *day,
81+
Hour: *hour,
82+
Minute: *minute,
83+
Second: *second,
8484
})
8585

8686
// No conditions specified

0 commit comments

Comments
 (0)