MCPcopy Create free account
hub / github.com/diillson/chatcli / contextArgvToMap

Function contextArgvToMap

cli/plugins/builtin_context.go:538–593  ·  view source on GitHub ↗

contextArgvToMap converts `--flag value`, `--flag=value` and bare positional names into the raw map the JSON path produces.

(args []string)

Source from the content-addressed store, hash-verified

536// contextArgvToMap converts `--flag value`, `--flag=value` and bare positional
537// names into the raw map the JSON path produces.
538func contextArgvToMap(args []string) map[string]json.RawMessage {
539 // Repeated flags accumulate: the agent flattener emits array fields
540 // (paths, tags, sources) as one "--key value" pair per element, so
541 // overwriting would silently keep only the last element.
542 vals := map[string][]string{}
543 bools := map[string]bool{}
544 add := func(k, v string) { vals[k] = append(vals[k], v) }
545 var positional []string
546 for i := 0; i < len(args); i++ {
547 a := strings.TrimSpace(args[i])
548 switch {
549 case strings.HasPrefix(a, "--") && strings.Contains(a, "="):
550 kv := strings.SplitN(strings.TrimPrefix(a, "--"), "=", 2)
551 add(kv[0], trimQuotes(kv[1]))
552 case strings.HasPrefix(a, "--"):
553 key := strings.TrimPrefix(a, "--")
554 if i+1 < len(args) && !strings.HasPrefix(args[i+1], "--") {
555 add(key, trimQuotes(args[i+1]))
556 i++
557 } else {
558 bools[key] = true
559 }
560 case a != "":
561 positional = append(positional, a)
562 }
563 }
564 raw := map[string]json.RawMessage{}
565 for k := range bools {
566 raw[k] = json.RawMessage("true")
567 }
568 for k, vs := range vals {
569 var b []byte
570 var err error
571 if len(vs) == 1 {
572 b, err = json.Marshal(vs[0])
573 } else {
574 b, err = json.Marshal(vs)
575 }
576 if err == nil {
577 raw[k] = b
578 }
579 }
580 if len(positional) > 0 {
581 if _, ok := raw["name"]; !ok {
582 if b, err := json.Marshal(positional[0]); err == nil {
583 raw["name"] = b
584 }
585 }
586 if len(positional) > 1 {
587 if b, err := json.Marshal(positional[1:]); err == nil {
588 raw["paths"] = b
589 }
590 }
591 }
592 return raw
593}
594
595// jsonBool reads a boolean field, tolerating "true"/"1" strings.

Callers 1

parseContextInvocationFunction · 0.85

Calls 1

trimQuotesFunction · 0.85

Tested by

no test coverage detected