Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Type() into its own interface #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ type Flag struct {
type Value interface {
String() string
Set(string) error
}

// Typed is an interface of Values that can communicate their type.
type Typed interface {
Type() string
}

Expand Down Expand Up @@ -378,8 +382,8 @@ func (f *FlagSet) getFlagType(name string, ftype string, convFunc func(sval stri
return nil, err
}

if flag.Value.Type() != ftype {
err := fmt.Errorf("trying to get %s value of flag of type %s", ftype, flag.Value.Type())
if v, ok := flag.Value.(Typed); ok && v.Type() != ftype {
err := fmt.Errorf("trying to get %s value of flag of type %s", ftype, v.Type())
return nil, err
}

Expand Down Expand Up @@ -579,24 +583,27 @@ func UnquoteUsage(flag *Flag) (name string, usage string) {
}
}

name = flag.Value.Type()
switch name {
case "bool":
name = ""
case "float64":
name = "float"
case "int64":
name = "int"
case "uint64":
name = "uint"
case "stringSlice":
name = "strings"
case "intSlice":
name = "ints"
case "uintSlice":
name = "uints"
case "boolSlice":
name = "bools"
name = "value" // compatibility layer to be a drop-in replacement
if v, ok := flag.Value.(Typed); ok {
name = v.Type()
switch name {
case "bool":
name = ""
case "float64":
name = "float"
case "int64":
name = "int"
case "uint64":
name = "uint"
case "stringSlice":
name = "strings"
case "intSlice":
name = "ints"
case "uintSlice":
name = "uints"
case "boolSlice":
name = "bools"
}
}

return
Expand Down Expand Up @@ -697,18 +704,22 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
line += " " + varname
}
if flag.NoOptDefVal != "" {
switch flag.Value.Type() {
case "string":
line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal)
case "bool":
if flag.NoOptDefVal != "true" {
if v, ok := flag.Value.(Typed); ok {
switch v.Type() {
case "string":
line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal)
case "bool":
if flag.NoOptDefVal != "true" {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
case "count":
if flag.NoOptDefVal != "+1" {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
default:
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
case "count":
if flag.NoOptDefVal != "+1" {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
default:
} else {
line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
}
}
Expand All @@ -722,7 +733,7 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {

line += usage
if !flag.defaultIsZeroValue() {
if flag.Value.Type() == "string" {
if v, ok := flag.Value.(Typed); ok && v.Type() == "string" {
line += fmt.Sprintf(" (default %q)", flag.DefValue)
} else {
line += fmt.Sprintf(" (default %s)", flag.DefValue)
Expand Down