(usage string)
| 191 | } |
| 192 | |
| 193 | func NewArgsParserWithUsage(usage string) *ArgsParser { |
| 194 | p := NewArgsParser() |
| 195 | f := `(-[a-zA-Z0-9@^]|--[a-z][a-z0-9-]+)(?:\[?[ =]([a-zA-Z_<>:=-]+\]?))?` |
| 196 | re := regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%s(?:,\s*%s)?$`, f, f)) |
| 197 | for _, match := range re.FindAllStringSubmatch(usage, -1) { |
| 198 | n1 := match[1] |
| 199 | n2 := match[3] |
| 200 | hasValue := !(match[2] == "" || strings.HasSuffix(match[2], "]")) || match[4] != "" |
| 201 | var aliases []string |
| 202 | if len(n1) == 2 && len(n2) > 2 { |
| 203 | aliases = []string{n1} |
| 204 | n1 = n2 |
| 205 | } else if n2 != "" { |
| 206 | aliases = []string{n2} |
| 207 | } |
| 208 | if hasValue { |
| 209 | p.RegisterValue(n1, aliases...) |
| 210 | } else { |
| 211 | p.RegisterBool(n1, aliases...) |
| 212 | } |
| 213 | } |
| 214 | return p |
| 215 | } |
searching dependent graphs…