From ec564c021e8e1052522019199eb88be1d616af4f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 27 Mar 2022 13:55:27 +0300 Subject: [PATCH] add badflag test Signed-off-by: Vasiliy Tolstov --- flag.go | 2 ++ flag_test.go | 18 ++++++++++-------- util.go | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/flag.go b/flag.go index f31cbbf..bfacf04 100644 --- a/flag.go +++ b/flag.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "fmt" + "os" "reflect" "time" @@ -44,6 +45,7 @@ func (c *flagConfig) Init(opts ...config.Option) error { return err } + flag.CommandLine.Init(os.Args[0], flag.ContinueOnError) for _, sf := range fields { tf, ok := sf.Field.Tag.Lookup(c.opts.StructTag) if !ok { diff --git a/flag_test.go b/flag_test.go index a702e46..94b9eb2 100644 --- a/flag_test.go +++ b/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, "-metadata", "key=20") 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 { Value string `flag:"name=nested_value"` } 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'"` - Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"` - WithoutDefault string `flag:"name=without_default,desc='with'"` - WithoutDesc string `flag:"name=without_desc,default='without_default'"` - WithoutAll string `flag:"name=without_all"` Components map[string]string `flag:"name=components,desc='components logging'"` + Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"` 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'"` + WithoutAll string `flag:"name=without_all"` + WithoutDefault string `flag:"name=without_default,desc='with'"` + 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() diff --git a/util.go b/util.go index 1ff1bf7..a59cf26 100644 --- a/util.go +++ b/util.go @@ -10,9 +10,9 @@ import ( ) type mapValue struct { - v reflect.Value delim string def string + v reflect.Value } func (v mapValue) String() string { @@ -67,9 +67,9 @@ func (v mapValue) Set(s string) error { } type sliceValue struct { - v reflect.Value delim string def string + v reflect.Value } func (v sliceValue) String() string { -- 2.45.2