add badflag test #38

Merged
vtolstov merged 1 commits from test into v3 2022-03-27 13:57:16 +03:00
3 changed files with 14 additions and 10 deletions
Showing only changes of commit ec564c021e - Show all commits

View File

@ -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 {

View File

@ -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()

View File

@ -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 {