Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package flag |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "sort" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | // MapStringBool can be set from the command line with the format `--flag "string=bool"`. |
| 27 | // Multiple comma-separated key-value pairs in a single invocation are supported. For example: `--flag "a=true,b=false"`. |
| 28 | // Multiple flag invocations are supported. For example: `--flag "a=true" --flag "b=false"`. |
| 29 | type MapStringBool struct { |
| 30 | Map *map[string]bool |
| 31 | initialized bool |
| 32 | } |
| 33 | |
| 34 | // NewMapStringBool takes a pointer to a map[string]string and returns the |
| 35 | // MapStringBool flag parsing shim for that map |
| 36 | func NewMapStringBool(m *map[string]bool) *MapStringBool { |
| 37 | return &MapStringBool{Map: m} |
| 38 | } |
| 39 | |
| 40 | // String implements github.com/spf13/pflag.Value |
| 41 | func (m *MapStringBool) String() string { |
| 42 | if m == nil || m.Map == nil { |
| 43 | return "" |
| 44 | } |
| 45 | pairs := []string{} |
| 46 | for k, v := range *m.Map { |
| 47 | pairs = append(pairs, fmt.Sprintf("%s=%t", k, v)) |
| 48 | } |
| 49 | sort.Strings(pairs) |
| 50 | return strings.Join(pairs, ",") |
| 51 | } |
| 52 | |
| 53 | // Set implements github.com/spf13/pflag.Value |
| 54 | func (m *MapStringBool) Set(value string) error { |
| 55 | if m.Map == nil { |
| 56 | return fmt.Errorf("no target (nil pointer to map[string]bool)") |
| 57 | } |
| 58 | if !m.initialized || *m.Map == nil { |
| 59 | // clear default values, or allocate if no existing map |
| 60 | *m.Map = make(map[string]bool) |
| 61 | m.initialized = true |
| 62 | } |
| 63 | for _, s := range strings.Split(value, ",") { |
| 64 | if len(s) == 0 { |
| 65 | continue |
| 66 | } |
| 67 | arr := strings.SplitN(s, "=", 2) |
| 68 | if len(arr) != 2 { |
| 69 | return fmt.Errorf("malformed pair, expect string=bool") |
| 70 | } |
| 71 | k := strings.TrimSpace(arr[0]) |
| 72 | v := strings.TrimSpace(arr[1]) |
| 73 | boolValue, err := strconv.ParseBool(v) |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("invalid value of %s: %s, err: %v", k, v, err) |
| 76 | } |
| 77 | (*m.Map)[k] = boolValue |
| 78 | } |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | // Type implements github.com/spf13/pflag.Value |
| 83 | func (*MapStringBool) Type() string { |
| 84 | return "mapStringBool" |
| 85 | } |
| 86 | |
| 87 | // Empty implements OmitEmpty |
| 88 | func (m *MapStringBool) Empty() bool { |
| 89 | return len(*m.Map) == 0 |
| 90 | } |