2019-05-30 23:11:13 +01:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"strings"
|
|
|
|
|
2020-01-25 13:41:25 +03:00
|
|
|
"github.com/micro/cli/v2"
|
2019-05-30 23:11:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
|
|
|
switch ff.Value.(type) {
|
|
|
|
case *cli.StringSlice:
|
|
|
|
default:
|
|
|
|
set.Set(name, ff.Value.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeFlags(flags []cli.Flag, set *flag.FlagSet) error {
|
|
|
|
visited := make(map[string]bool)
|
|
|
|
set.Visit(func(f *flag.Flag) {
|
|
|
|
visited[f.Name] = true
|
|
|
|
})
|
|
|
|
for _, f := range flags {
|
2020-01-25 13:41:25 +03:00
|
|
|
parts := f.Names()
|
2019-05-30 23:11:13 +01:00
|
|
|
if len(parts) == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ff *flag.Flag
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
if visited[name] {
|
|
|
|
if ff != nil {
|
|
|
|
return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name)
|
|
|
|
}
|
|
|
|
ff = set.Lookup(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ff == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
if !visited[name] {
|
|
|
|
copyFlag(name, ff, set)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|