2019-06-17 09:48:05 +00:00
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-09-13 17:10:04 +00:00
|
|
|
"regexp"
|
2019-06-17 09:48:05 +00:00
|
|
|
"strings"
|
2019-06-21 08:08:04 +00:00
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/parser"
|
2019-06-17 09:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parse parses the command-line flag arguments into a map,
|
|
|
|
// using the type information in element to discriminate whether a flag is supposed to be a bool,
|
|
|
|
// and other such ambiguities.
|
|
|
|
func Parse(args []string, element interface{}) (map[string]string, error) {
|
|
|
|
f := flagSet{
|
|
|
|
flagTypes: getFlagTypes(element),
|
|
|
|
args: args,
|
|
|
|
values: make(map[string]string),
|
2019-08-05 13:22:03 +00:00
|
|
|
keys: make(map[string]string),
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
seen, err := f.parseOne()
|
|
|
|
if seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type flagSet struct {
|
|
|
|
flagTypes map[string]reflect.Kind
|
|
|
|
args []string
|
|
|
|
values map[string]string
|
2019-08-05 13:22:03 +00:00
|
|
|
keys map[string]string
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *flagSet) parseOne() (bool, error) {
|
|
|
|
if len(f.args) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := f.args[0]
|
|
|
|
if len(s) < 2 || s[0] != '-' {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
numMinuses := 1
|
|
|
|
if s[1] == '-' {
|
|
|
|
numMinuses++
|
|
|
|
if len(s) == 2 { // "--" terminates the flags
|
|
|
|
f.args = f.args[1:]
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
name := s[numMinuses:]
|
|
|
|
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
|
|
|
|
return false, fmt.Errorf("bad flag syntax: %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's a flag. does it have an argument?
|
|
|
|
f.args = f.args[1:]
|
|
|
|
hasValue := false
|
|
|
|
value := ""
|
|
|
|
for i := 1; i < len(name); i++ { // equals cannot be first
|
|
|
|
if name[i] == '=' {
|
|
|
|
value = name[i+1:]
|
|
|
|
hasValue = true
|
|
|
|
name = name[0:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasValue {
|
|
|
|
f.setValue(name, value)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:10:04 +00:00
|
|
|
flagType := f.getFlagType(name)
|
|
|
|
if flagType == reflect.Bool || flagType == reflect.Ptr {
|
2019-06-17 09:48:05 +00:00
|
|
|
f.setValue(name, "true")
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.args) > 0 {
|
|
|
|
// value is the next arg
|
|
|
|
hasValue = true
|
|
|
|
value, f.args = f.args[0], f.args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasValue {
|
|
|
|
return false, fmt.Errorf("flag needs an argument: -%s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.setValue(name, value)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:42:03 +00:00
|
|
|
func (f *flagSet) setValue(name, value string) {
|
2019-08-05 13:22:03 +00:00
|
|
|
srcKey := parser.DefaultRootName + "." + name
|
|
|
|
neutralKey := strings.ToLower(srcKey)
|
2019-06-17 09:48:05 +00:00
|
|
|
|
2019-08-05 13:22:03 +00:00
|
|
|
key, ok := f.keys[neutralKey]
|
|
|
|
if !ok {
|
|
|
|
f.keys[neutralKey] = srcKey
|
|
|
|
key = srcKey
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := f.values[key]
|
2019-09-13 17:10:04 +00:00
|
|
|
if ok && f.getFlagType(name) == reflect.Slice {
|
2019-08-05 13:22:03 +00:00
|
|
|
f.values[key] = v + "," + value
|
2019-06-17 09:48:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:22:03 +00:00
|
|
|
f.values[key] = value
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
2019-09-13 17:10:04 +00:00
|
|
|
|
|
|
|
func (f *flagSet) getFlagType(name string) reflect.Kind {
|
|
|
|
neutral := strings.ToLower(name)
|
|
|
|
|
|
|
|
kind, ok := f.flagTypes[neutral]
|
|
|
|
if ok {
|
|
|
|
return kind
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, k := range f.flagTypes {
|
|
|
|
if strings.Contains(n, parser.MapNamePlaceholder) {
|
|
|
|
p := strings.NewReplacer(".", `\.`, parser.MapNamePlaceholder, `([^.]+)`).Replace(n)
|
|
|
|
if regexp.MustCompile(p).MatchString(neutral) {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.Invalid
|
|
|
|
}
|