From 1dd7e718819ce536faec2a0a5a4e8f6a1df8d690 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 25 Mar 2022 14:06:21 +0300 Subject: [PATCH] add check for already defined flag Signed-off-by: Vasiliy Tolstov --- flag.go | 4 +++- flag_test.go | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/flag.go b/flag.go index 6448676..f31cbbf 100644 --- a/flag.go +++ b/flag.go @@ -60,7 +60,9 @@ func (c *flagConfig) Init(opts ...config.Option) error { if vi == nil { continue } - + if f := flag.Lookup(fn); f != nil { + return nil + } switch vi.(type) { case time.Duration: err = c.flagDuration(sf.Value, fn, fv, fd) diff --git a/flag_test.go b/flag_test.go index 1854744..a702e46 100644 --- a/flag_test.go +++ b/flag_test.go @@ -43,6 +43,11 @@ func TestLoad(t *testing.T) { t.Fatalf("init failed: %v", err) } + // double init test + if err := c.Init(); err != nil { + t.Fatalf("init failed: %v", err) + } + if err := c.Load(ctx); err != nil { t.Fatalf("load failed: %v", err) } -- 2.45.2