(context parseContext)
| 316 | } |
| 317 | |
| 318 | func (argparseParser) Parse(context parseContext) (res *parseResult, err error) { |
| 319 | usageStartIndex := context.text.FindFirstLine("usage:") |
| 320 | if usageStartIndex < 0 { |
| 321 | err = fmt.Errorf("cannot find usage") |
| 322 | return |
| 323 | } |
| 324 | if usageStartIndex != 0 { |
| 325 | err = fmt.Errorf("usage is not at the beginning, doesn't look like argparse") |
| 326 | } |
| 327 | |
| 328 | usageEndIndex := context.text.ParagraphEnd(usageStartIndex) |
| 329 | usageLexer := makeUsageLexer(context.text.lines[usageStartIndex:usageEndIndex]) |
| 330 | |
| 331 | usage, err := parseArgparseUsage(&usageLexer) |
| 332 | if err != nil { |
| 333 | err = fmt.Errorf("error parsing usage: %v", err) |
| 334 | return |
| 335 | } |
| 336 | |
| 337 | if filepath.Base(usage.applicationName) != filepath.Base(context.args[0]) { |
| 338 | err = fmt.Errorf("application in usage doesn't match provided application") |
| 339 | return |
| 340 | } |
| 341 | |
| 342 | result := &parseResult{} |
| 343 | for start := 0; start < len(context.text.lines); { |
| 344 | par := context.text.FindIndentedParagraph("arguments:", start) |
| 345 | if par == nil { |
| 346 | break |
| 347 | } |
| 348 | start = par.lineEnd |
| 349 | if len(par.children) == 0 { |
| 350 | continue |
| 351 | } |
| 352 | switch { |
| 353 | case tryParseFlagsParagraph(par, &usage, result): |
| 354 | case tryParseNamedPositionalParagraph(par, &usage, result): |
| 355 | case tryParseUnnamedPositionalParagraph(par, &usage, result): |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | res = result |
| 360 | return |
| 361 | } |
nothing calls this directly
no test coverage detected