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

Commit

Permalink
simplified flags package
Browse files Browse the repository at this point in the history
using same logic as flag.Duration
  • Loading branch information
jorinvo committed Feb 9, 2017
1 parent 4092ba7 commit a2c3ecb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 52 deletions.
21 changes: 9 additions & 12 deletions flags/intlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
type intlist struct {
min int
max int
list []int
list *[]int
}

func (l *intlist) String() string {
s := make([]string, len(l.list))
for i := range l.list {
s[i] = strconv.Itoa(l.list[i])
s := make([]string, len(*l.list))
for i := range *l.list {
s[i] = strconv.Itoa((*l.list)[i])
}
return strings.Join(s, ",")
}
Expand All @@ -34,17 +34,14 @@ func (l *intlist) Set(s string) error {
if x > l.max {
return fmt.Errorf("%d is bigger than maximum %d", x, l.max)
}
l.list = append(l.list, x)
*l.list = append(*l.list, x)
}
return nil
}

// Intlist defines a flag for a comma-separated list of integers.
// Call the returned function after flag.Parse to get the value.
func Intlist(name, usage string, min, max int) func() []int {
l := &intlist{min: min, max: max}
flag.Var(l, name, usage)
return func() []int {
return l.list
}
func Intlist(name, usage string, min, max int) *[]int {
l := &[]int{}
flag.Var(&intlist{list: l, min: min, max: max}, name, usage)
return l
}
4 changes: 2 additions & 2 deletions flags/intlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestIntlist(t *testing.T) {
}

for i, tt := range tests {
l := intlist{min: 0, max: 10}
l := intlist{min: 0, max: 10, list: &[]int{}}
if err := l.Set(tt.text); err != nil {
if !tt.invalid {
t.Errorf("parsing %s failed unexpectedly: %v", tt.text, err)
Expand All @@ -31,7 +31,7 @@ func TestIntlist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equal(l.list, tt.parsed) {
if !equal(*l.list, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expand Down
23 changes: 9 additions & 14 deletions flags/monthlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"time"
)

type monthlist struct {
list []time.Month
}
type monthlist []time.Month

func (l *monthlist) String() string {
s := make([]string, len(l.list))
for i := range l.list {
s[i] = strconv.Itoa(int(l.list[i]))
s := make([]string, len(*l))
for i := range *l {
s[i] = strconv.Itoa(int((*l)[i]))
}
return strings.Join(s, ",")
}
Expand All @@ -31,18 +29,15 @@ func (l *monthlist) Set(s string) error {
if x < 1 || x > 12 {
return fmt.Errorf("invalid month: %d", x)
}
l.list = append(l.list, x)
*l = monthlist(append(*l, x))
}
return nil
}

// Monthlist defines a flag for a comma-separated list of months.
// Valid values are between 1 and 12.
// Call the returned function after flag.Parse to get the value.
func Monthlist(name, usage string) func() []time.Month {
l := &monthlist{}
flag.Var(l, name, usage)
return func() []time.Month {
return l.list
}
func Monthlist(name, usage string) *[]time.Month {
l := &[]time.Month{}
flag.Var((*monthlist)(l), name, usage)
return l
}
4 changes: 2 additions & 2 deletions flags/monthlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func TestMonthlist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equalMonth(l.list, tt.parsed) {
if !equalMonth(l, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expected: %v
Got %v`, i, tt.text, tt.parsed, l.list)
Got %v`, i, tt.text, tt.parsed, l)
}
}

Expand Down
23 changes: 9 additions & 14 deletions flags/weekdaylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ var strDays = map[string]time.Weekday{
"su": time.Sunday,
}

type weekdaylist struct {
list []time.Weekday
}
type weekdaylist []time.Weekday

func (l *weekdaylist) String() string {
s := make([]string, len(l.list))
for i := range l.list {
s[i] = dayToString(l.list[i])
s := make([]string, len(*l))
for i := range *l {
s[i] = dayToString((*l)[i])
}
return strings.Join(s, ",")
}
Expand All @@ -45,18 +43,15 @@ func (l *weekdaylist) Set(s string) error {
if !ok {
return fmt.Errorf("invalid day at index %d: %s", i, d)
}
l.list = append(l.list, x)
*l = weekdaylist(append(*l, x))
}
return nil
}

// Weekdaylist defines a flag for a comma-separated list of week days.
// Valid values are mo, tu, we, th, fr, sa, su.
// Call the returned function after flag.Parse to get the value.
func Weekdaylist(name, usage string) func() []time.Weekday {
l := &weekdaylist{}
flag.Var(l, name, usage)
return func() []time.Weekday {
return l.list
}
func Weekdaylist(name, usage string) *[]time.Weekday {
l := &[]time.Weekday{}
flag.Var((*weekdaylist)(l), name, usage)
return l
}
4 changes: 2 additions & 2 deletions flags/weekdaylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func TestWeekdaylist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equalWeekday(l.list, tt.parsed) {
if !equalWeekday(l, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expected: %v
Got %v`, i, tt.text, tt.parsed, l.list)
Got %v`, i, tt.text, tt.parsed, l)
}
}

Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func main() {
now := time.Now()

next := match.Next(now, match.Condition{
Month: month(),
Weekday: weekday(),
Day: day(),
Hour: hour(),
Minute: minute(),
Second: second(),
Month: *month,
Weekday: *weekday,
Day: *day,
Hour: *hour,
Minute: *minute,
Second: *second,
})

// No conditions specified
Expand Down

0 comments on commit a2c3ecb

Please sign in to comment.