From 90a343323c1724e8691b7244aa801f75a8bf1c8f Mon Sep 17 00:00:00 2001 From: xgfone Date: Mon, 1 Jul 2019 09:23:23 +0800 Subject: [PATCH] add the prefix for the name of cli.Flag --- source_cli.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/source_cli.go b/source_cli.go index 68cb6a1..ce9534f 100644 --- a/source_cli.go +++ b/source_cli.go @@ -23,11 +23,26 @@ import ( // ConvertOptsToCliFlags converts the options from the group to flags of // github.com/urfave/cli. -func ConvertOptsToCliFlags(group *OptGroup) []cli.Flag { +// +// If prefix is not empty, it will add the prefix to the flag name, +// and join them with the character "-". +// +// Notice: the character "_" in the flag name will be converted to "-". +func ConvertOptsToCliFlags(group *OptGroup, prefix ...string) []cli.Flag { + var _prefix string + if len(prefix) > 0 && prefix[0] != "" { + _prefix = prefix[0] + } + opts := group.AllOpts() flags := make([]cli.Flag, len(opts)) for i, opt := range opts { - name := strings.Replace(opt.Name, "_", "-", -1) + name := opt.Name + if _prefix != "" { + name = fmt.Sprintf("%s-%s", _prefix, name) + } + name = strings.Replace(name, "_", "-", -1) + var flag cli.Flag switch v := opt.Default.(type) { case bool: