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

Function extractNestedArg

cli/plugins/args_extract.go:176–198  ·  view source on GitHub ↗

extractNestedArg pulls a string from inside the `args` field of the @coder envelope (or any other plugin that uses the same nested shape). Useful when a key collides with a top-level field — e.g. @coder's `{"cmd":"exec","args":{"cmd":"go test"}}` has "cmd" in both layers. extractStringArg returns th

(args []string, keys ...string)

Source from the content-addressed store, hash-verified

174// extractStringArg returns the OUTER cmd (which is the subcommand name);
175// this helper returns the INNER one (the actual user command).
176func extractNestedArg(args []string, keys ...string) string {
177 if len(args) == 0 {
178 return ""
179 }
180 raw := strings.TrimSpace(args[0])
181 if raw == "" || raw[0] != '{' {
182 // No JSON envelope to dig into — fall back to flag-style scan.
183 return stringFromFlagArgs(args, keys)
184 }
185 var top map[string]json.RawMessage
186 if err := json.Unmarshal([]byte(raw), &top); err != nil {
187 return stringFromFlagArgs(args, keys)
188 }
189 if inner, ok := top["args"]; ok {
190 var innerMap map[string]json.RawMessage
191 if err := json.Unmarshal(inner, &innerMap); err == nil {
192 if v := stringFromMap(innerMap, keys); v != "" {
193 return v
194 }
195 }
196 }
197 return stringFromFlagArgs(args, keys)
198}

Calls 2

stringFromFlagArgsFunction · 0.85
stringFromMapFunction · 0.85