MCPcopy Create free account
hub / github.com/dim-an/cod / Parse

Method Parse

parse_doc/argparse.go:318–361  ·  view source on GitHub ↗
(context parseContext)

Source from the content-addressed store, hash-verified

316}
317
318func (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}

Callers

nothing calls this directly

Calls 8

makeUsageLexerFunction · 0.85
parseArgparseUsageFunction · 0.85
tryParseFlagsParagraphFunction · 0.85
FindFirstLineMethod · 0.80
ParagraphEndMethod · 0.80
FindIndentedParagraphMethod · 0.80

Tested by

no test coverage detected