The input mapping is expressed on the command line as `key1:value1,key2:value2,...` We parse it here, but need to keep in mind that keys or values may contain commas and colons. We will allow escaping those using double quotes, so when passing in "key1":"value1", we will not look inside the quoted s
(src string)
| 10 | // commas and colons. We will allow escaping those using double quotes, so |
| 11 | // when passing in "key1":"value1", we will not look inside the quoted sections. |
| 12 | func ParseCommandlineMap(src string) (map[string]string, error) { |
| 13 | result := make(map[string]string) |
| 14 | tuples := splitString(src, ',') |
| 15 | for _, t := range tuples { |
| 16 | kv := splitString(t, ':') |
| 17 | if len(kv) != 2 { |
| 18 | return nil, fmt.Errorf("expected key:value, got :%s", t) |
| 19 | } |
| 20 | key := strings.TrimLeft(kv[0], `"`) |
| 21 | key = strings.TrimRight(key, `"`) |
| 22 | |
| 23 | value := strings.TrimLeft(kv[1], `"`) |
| 24 | value = strings.TrimRight(value, `"`) |
| 25 | |
| 26 | result[key] = value |
| 27 | } |
| 28 | return result, nil |
| 29 | } |
| 30 | |
| 31 | // ParseCommandLineList parses comma separated string lists which are passed |
| 32 | // in on the command line. Spaces are trimmed off both sides of result |