MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / parseArgs

Function parseArgs

internal/cmdhelp/json.go:272–363  ·  view source on GitHub ↗
(cmd *cobra.Command)

Source from the content-addressed store, hash-verified

270var argRE = regexp.MustCompile(`<([^<>]+)>(\.\.\.)?|\[([^\[\]]+)\](\.\.\.)?`)
271
272func parseArgs(cmd *cobra.Command) []Arg {
273 matches := argRE.FindAllStringSubmatch(cmd.Use, -1)
274 args := make([]Arg, 0, len(matches))
275 for _, m := range matches {
276 var inner, ellipsis string
277 var required bool
278 switch {
279 case m[1] != "":
280 inner = m[1]
281 ellipsis = m[2]
282 required = true
283 case m[3] != "":
284 inner = m[3]
285 ellipsis = m[4]
286 required = false
287 default:
288 continue
289 }
290 inner = strings.TrimSpace(inner)
291 if inner == "" {
292 continue
293 }
294
295 // Filter cobra-conventional placeholders that are not real args.
296 lower := strings.ToLower(inner)
297 if !required && (lower == "flags" || lower == "options" || lower == "command") {
298 continue
299 }
300 // Filter embedded flag-like fragments such as `[--status X]`.
301 if strings.HasPrefix(inner, "-") {
302 continue
303 }
304
305 // Alternation: <a|b|c> or [a|b|c] → enum-typed positional.
306 if strings.Contains(inner, "|") {
307 parts := strings.Split(inner, "|")
308 values := make([]interface{}, 0, len(parts))
309 for _, p := range parts {
310 p = strings.TrimSpace(p)
311 if p != "" {
312 values = append(values, p)
313 }
314 }
315 if len(values) > 0 {
316 args = append(args, Arg{
317 Name: "value", // synthesized; cobra Use doesn't carry a name here
318 Type: "enum",
319 Required: required,
320 Enum: values,
321 Repeatable: ellipsis != "",
322 })
323 continue
324 }
325 }
326
327 // Plain <name> / [name]. Reject anything that looks like prose
328 // (spaces, punctuation that wouldn't be in an arg identifier).
329 if !validArgName(inner) {

Callers 1

buildCommandFunction · 0.85

Calls 1

validArgNameFunction · 0.85

Tested by

no test coverage detected