From 7c0ed886ef0e4f88f60025ff69abdfb58e11bf36 Mon Sep 17 00:00:00 2001 From: Darya Kamynina Date: Thu, 10 Feb 2022 10:08:29 +0300 Subject: [PATCH] Populate tag name customization --- bind.go | 6 +++++- option.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bind.go b/bind.go index 53a9748..f0ab6fa 100644 --- a/bind.go +++ b/bind.go @@ -361,7 +361,11 @@ func (b bind) bind(fs FlagSet, v interface{}) (err error) { // short name. if !tag.HasExplicitName || (usePFlag && tag.Name == tag.ShortName) { - tag.Name = FromCamelCase(structField.Name, Separator) + if b.NamePopulator == nil { + tag.Name = FromCamelCase(structField.Name, Separator) + } else { + tag.Name = b.NamePopulator(structField.Name) + } } fieldV := val.Field(i) diff --git a/option.go b/option.go index 40317ec..34ed942 100644 --- a/option.go +++ b/option.go @@ -11,6 +11,7 @@ func newBind(opts ...Option) bind { type bind struct { Prefix string NoAutoFlatten bool + NamePopulator func(fieldName string) string } func (b bind) Option() Option { @@ -41,3 +42,9 @@ func NoAutoFlatten() Option { b.NoAutoFlatten = true } } + +func NamePopulator(fn func(fieldName string) string) Option { + return func(b *bind) { + b.NamePopulator = fn + } +}