Skip to content

Commit

Permalink
add the prefix for the name of cli.Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 1, 2019
1 parent a7611ce commit 90a3433
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions source_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 90a3433

Please sign in to comment.