Skip to content

Commit

Permalink
Allow genvalues to be used with arbitrary types.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Dec 3, 2015
1 parent 98f5285 commit 9f1f817
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 138 deletions.
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kingpin
import (
"io/ioutil"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"

"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"
)

func TestArgRemainder(t *testing.T) {
Expand Down
24 changes: 16 additions & 8 deletions cmd/genvalues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ const (
{{range .}}
{{if not .NoValueParser}}
// -- {{.Type}} Value
type {{.Type}}Value {{.Type}}
type {{.|ValueName}} struct { v *{{.Type}} }
func new{{.|Name}}Value(p *{{.Type}}) *{{.Type}}Value {
return (*{{.Type}}Value)(p)
func new{{.|Name}}Value(p *{{.Type}}) *{{.|ValueName}} {
return &{{.|ValueName}}{p}
}
func (f *{{.Type}}Value) Set(s string) error {
func (f *{{.|ValueName}}) Set(s string) error {
v, err := {{.Parser}}
*f = {{.Type}}Value(v)
if err == nil {
*f.v = ({{.Type}})(v)
}
return err
}
func (f *{{.Type}}Value) Get() interface{} { return {{.Type}}(*f) }
func (f *{{.|ValueName}}) Get() interface{} { return ({{.Type}})(*f.v) }
func (f *{{.Type}}Value) String() string { return {{.|Format}} }
func (f *{{.|ValueName}}) String() string { return {{.|Format}} }
// {{.|Name}} parses the next command-line value as {{.Type}}.
func (p *parserMixin) {{.|Name}}() (target *{{.Type}}) {
Expand All @@ -53,7 +55,9 @@ func (p *parserMixin) {{.|Plural}}() (target *[]{{.Type}}) {
}
func (p *parserMixin) {{.|Plural}}Var(target *[]{{.Type}}) {
p.SetValue(newAccumulator(target, func(v interface{}) Value { return new{{.|Name}}Value(v.(*{{.Type}})) }))
p.SetValue(newAccumulator(target, func(v interface{}) Value {
return new{{.|Name}}Value(v.(*{{.Type}}))
}))
}
{{end}}
Expand Down Expand Up @@ -99,6 +103,10 @@ func main() {
}
return "fmt.Sprintf(\"%v\", *f)"
},
"ValueName": func(v *Value) string {
name := valueName(v)
return strings.ToLower(name[0:1]) + name[1:] + "Value"
},
"Name": valueName,
"Plural": func(v *Value) string {
if v.Plural != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kingpin
import (
"strings"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"

"testing"
)
Expand Down
2 changes: 1 addition & 1 deletion flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io/ioutil"
"os"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"

"testing"
)
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"
)

func TestParserExpandFromFile(t *testing.T) {
Expand Down
13 changes: 0 additions & 13 deletions parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"
"net/url"
"os"
"regexp"
"time"

"github.com/alecthomas/units"
Expand Down Expand Up @@ -211,15 +210,3 @@ 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))
}
2 changes: 1 addition & 1 deletion parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/url"
"os"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"

"testing"
)
Expand Down
2 changes: 1 addition & 1 deletion usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/alecthomas/assert"
)

func TestFormatTwoColumns(t *testing.T) {
Expand Down
29 changes: 0 additions & 29 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,32 +445,3 @@ 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()
}
4 changes: 2 additions & 2 deletions values.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{"type": "bool", "parser": "strconv.ParseBool(s)"},
{"type": "string", "parser": "s, error(nil)", "format": "string(*f)", "plural": "Strings"},
{"type": "string", "parser": "s, error(nil)", "format": "string(*f.v)", "plural": "Strings"},
{"type": "uint", "parser": "strconv.ParseUint(s, 0, 64)", "plural": "Uints"},
{"type": "uint8", "parser": "strconv.ParseUint(s, 0, 8)"},
{"type": "uint16", "parser": "strconv.ParseUint(s, 0, 16)"},
Expand All @@ -19,5 +19,5 @@
{"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": "Regexp", "Type": "*regexp.Regexp", "plural": "RegexpList", "no_value_parser": true}
{"name": "Regexp", "Type": "*regexp.Regexp", "plural": "RegexpList", "parser": "regexp.Compile(s)"}
]
Loading

0 comments on commit 9f1f817

Please sign in to comment.