parseExamples turns cobra's Example field (a single multiline string) into individual Example entries. Each non-empty, non-comment line is taken as one runnable invocation; comment lines (`# ...`) are dropped. The convention in pad's existing commands is two-space-indented lines like: pad item cr
(example string)
| 481 | // |
| 482 | // We trim leading/trailing whitespace per line. |
| 483 | func parseExamples(example string) []Example { |
| 484 | example = strings.TrimSpace(example) |
| 485 | if example == "" { |
| 486 | return nil |
| 487 | } |
| 488 | var examples []Example |
| 489 | for _, line := range strings.Split(example, "\n") { |
| 490 | line = strings.TrimSpace(line) |
| 491 | if line == "" || strings.HasPrefix(line, "#") { |
| 492 | continue |
| 493 | } |
| 494 | // A trailing same-line comment is dropped, exactly as |
| 495 | // parseExamplesFromLong drops it, so moving a block from Long to |
| 496 | // the Example field does not change what cmdhelp emits (TASK-2865). |
| 497 | if hashIdx := stripCommentIndex(line); hashIdx >= 0 { |
| 498 | line = strings.TrimRight(line[:hashIdx], " \t") |
| 499 | } |
| 500 | if line == "" { |
| 501 | continue |
| 502 | } |
| 503 | examples = append(examples, Example{Cmd: line}) |
| 504 | } |
| 505 | if len(examples) == 0 { |
| 506 | return nil |
| 507 | } |
| 508 | return examples |
| 509 | } |
| 510 | |
| 511 | // firstLine returns the first line of s with surrounding whitespace |
| 512 | // trimmed. Useful for collapsing a multi-line Long string into a Summary. |