Skip to content

Commit

Permalink
Merge pull request alecthomas#84 from zfjagann/regexp
Browse files Browse the repository at this point in the history
Added support for regexp flags
  • Loading branch information
alecthomas committed Nov 30, 2015
2 parents 6e0b900 + d07c944 commit 21551c2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
11 changes: 11 additions & 0 deletions flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ func TestRequiredWithEnvar(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 123, *flag)
}

func TestRegexp(t *testing.T) {
app := New("test", "")
flag := app.Flag("reg", "").Regexp()
_, err := app.Parse([]string{"--reg", "^abc$"})
assert.NoError(t, err)
assert.NotNil(t, *flag)
assert.Equal(t, "^abc$", (*flag).String())
assert.Regexp(t, *flag, "abc")
assert.NotRegexp(t, *flag, "abcd")
}
13 changes: 13 additions & 0 deletions parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"net/url"
"os"
"regexp"
"time"

"github.com/alecthomas/units"
Expand Down Expand Up @@ -210,3 +211,15 @@ func (p *parserMixin) Counter() (target *int) {
func (p *parserMixin) CounterVar(target *int) {
p.SetValue(newCounterValue(target))
}

// Regexp
func (p *parserMixin) Regexp() (target **regexp.Regexp) {
target = new(*regexp.Regexp)
p.RegexpVar(target)
return
}

// Regexp
func (p *parserMixin) RegexpVar(target **regexp.Regexp) {
p.SetValue(newRegexpValue(target))
}
29 changes: 29 additions & 0 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,32 @@ func (c *counterValue) Set(s string) error {
func (c *counterValue) Get() interface{} { return (int)(*c) }
func (c *counterValue) IsBoolFlag() bool { return true }
func (c *counterValue) String() string { return fmt.Sprintf("%d", *c) }

// -- regexp.Regexp value
type regexpValue struct {
r **regexp.Regexp
}

func newRegexpValue(r **regexp.Regexp) *regexpValue {
return &regexpValue{r}
}

func (r *regexpValue) Set(value string) error {
if re, err := regexp.Compile(value); err != nil {
return err
} else {
*r.r = re
return nil
}
}

func (r *regexpValue) Get() interface{} {
return (*regexp.Regexp)(*r.r)
}

func (r *regexpValue) String() string {
if *r.r == nil {
return "<nil>"
}
return (*r.r).String()
}
3 changes: 2 additions & 1 deletion values.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
{"name": "TCPAddr", "Type": "*net.TCPAddr", "plural": "TCPList", "no_value_parser": true},
{"name": "ExistingFile", "Type": "string", "plural": "ExistingFiles", "no_value_parser": true},
{"name": "ExistingDir", "Type": "string", "plural": "ExistingDirs", "no_value_parser": true},
{"name": "ExistingFileOrDir", "Type": "string", "plural": "ExistingFilesOrDirs", "no_value_parser": true}
{"name": "ExistingFileOrDir", "Type": "string", "plural": "ExistingFilesOrDirs", "no_value_parser": true},
{"name": "Regexp", "Type": "*regexp.Regexp", "plural": "RegexpList", "no_value_parser": true}
]
12 changes: 12 additions & 0 deletions values_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kingpin
import (
"fmt"
"net"
"regexp"
"strconv"
"time"
)
Expand Down Expand Up @@ -620,3 +621,14 @@ func (p *parserMixin) ExistingFilesOrDirs() (target *[]string) {
func (p *parserMixin) ExistingFilesOrDirsVar(target *[]string) {
p.SetValue(newAccumulator(target, func(v interface{}) Value { return newExistingFileOrDirValue(v.(*string)) }))
}

// RegexpList accumulates *regexp.Regexp values into a slice.
func (p *parserMixin) RegexpList() (target *[]*regexp.Regexp) {
target = new([]*regexp.Regexp)
p.RegexpListVar(target)
return
}

func (p *parserMixin) RegexpListVar(target *[]*regexp.Regexp) {
p.SetValue(newAccumulator(target, func(v interface{}) Value { return newRegexpValue(v.(**regexp.Regexp)) }))
}

0 comments on commit 21551c2

Please sign in to comment.