add badflag test #38
2
flag.go
2
flag.go
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -44,6 +45,7 @@ func (c *flagConfig) Init(opts ...config.Option) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
|
||||||
for _, sf := range fields {
|
for _, sf := range fields {
|
||||||
tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag)
|
tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
16
flag_test.go
16
flag_test.go
@ -18,21 +18,23 @@ func TestLoad(t *testing.T) {
|
|||||||
os.Args = append(os.Args, "-time", time.RFC822)
|
os.Args = append(os.Args, "-time", time.RFC822)
|
||||||
os.Args = append(os.Args, "-metadata", "key=20")
|
os.Args = append(os.Args, "-metadata", "key=20")
|
||||||
os.Args = append(os.Args, "-components", "all=info,api=debug")
|
os.Args = append(os.Args, "-components", "all=info,api=debug")
|
||||||
|
os.Args = append(os.Args, "-addr", "33,44")
|
||||||
|
os.Args = append(os.Args, "-badflag", "test")
|
||||||
type NestedConfig struct {
|
type NestedConfig struct {
|
||||||
Value string `flag:"name=nested_value"`
|
Value string `flag:"name=nested_value"`
|
||||||
}
|
}
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
|
|
||||||
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
|
|
||||||
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
|
|
||||||
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
|
|
||||||
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
|
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
|
||||||
|
Components map[string]string `flag:"name=components,desc='components logging'"`
|
||||||
Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"`
|
Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"`
|
||||||
WithoutDefault string `flag:"name=without_default,desc='with'"`
|
Nested *NestedConfig
|
||||||
|
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
|
||||||
WithoutDesc string `flag:"name=without_desc,default='without_default'"`
|
WithoutDesc string `flag:"name=without_desc,default='without_default'"`
|
||||||
WithoutAll string `flag:"name=without_all"`
|
WithoutAll string `flag:"name=without_all"`
|
||||||
Components map[string]string `flag:"name=components,desc='components logging'"`
|
WithoutDefault string `flag:"name=without_default,desc='with'"`
|
||||||
Nested *NestedConfig
|
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
|
||||||
|
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
|
||||||
|
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
4
util.go
4
util.go
@ -10,9 +10,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type mapValue struct {
|
type mapValue struct {
|
||||||
v reflect.Value
|
|
||||||
delim string
|
delim string
|
||||||
def string
|
def string
|
||||||
|
v reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v mapValue) String() string {
|
func (v mapValue) String() string {
|
||||||
@ -67,9 +67,9 @@ func (v mapValue) Set(s string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type sliceValue struct {
|
type sliceValue struct {
|
||||||
v reflect.Value
|
|
||||||
delim string
|
delim string
|
||||||
def string
|
def string
|
||||||
|
v reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v sliceValue) String() string {
|
func (v sliceValue) String() string {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user