neoneo-go/cli/flags/util.go
Ekaterina Pavlova 8503a9f54e cli: add Required flag for CLI parameters
Required field is added for CLI commands. MarkRequired function change
required parameter.

Closes #2861

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
2023-12-21 15:33:30 +03:00

40 lines
818 B
Go

package flags
import (
"strings"
"github.com/urfave/cli"
)
func eachName(longName string, fn func(string)) {
parts := strings.Split(longName, ",")
for _, name := range parts {
name = strings.Trim(name, " ")
fn(name)
}
}
// MarkRequired marks flags with specified names as required.
func MarkRequired(flagSet []cli.Flag, names ...string) []cli.Flag {
updatedflagSet := make([]cli.Flag, 0, len(flagSet))
for _, flag := range flagSet {
for _, n := range names {
if n == flag.GetName() {
switch f := (flag).(type) {
case cli.StringFlag:
f.Required = true
flag = f
case cli.IntFlag:
f.Required = true
flag = f
case cli.BoolFlag:
f.Required = true
flag = f
}
break
}
}
updatedflagSet = append(updatedflagSet, flag)
}
return updatedflagSet
}