Skip to content

Commit 2f0b82f

Browse files
authored
handle -repos flag values that are JSON arrays (#1235)
1 parent 4baaca0 commit 2f0b82f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

internal/mcp/mcp_args.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mcp
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"reflect"
@@ -16,6 +17,16 @@ type strSliceFlag struct {
1617
}
1718

1819
func (s *strSliceFlag) Set(v string) error {
20+
// The MCP Array properties accept JSON arrays so, if we get a value starting with "["
21+
// it's probably a JSON array
22+
if strings.HasPrefix(v, "[") {
23+
var arr []string
24+
if err := json.Unmarshal([]byte(v), &arr); err == nil {
25+
s.vals = append(s.vals, arr...)
26+
return nil
27+
}
28+
}
29+
// Otherwise treat as a single value
1930
s.vals = append(s.vals, v)
2031
return nil
2132
}

internal/mcp/mcp_args_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mcp
22

33
import (
4+
"slices"
45
"testing"
56
)
67

@@ -58,20 +59,22 @@ func TestFlagSetParse(t *testing.T) {
5859
t.Fatalf("vars from buildArgFlagSet should not be empty")
5960
}
6061

61-
args := []string{"-repos=A", "-repos=B", "-count=10", "-boolFlag", "-tag=testTag"}
62+
args := []string{"-repos=A", "-repos=B", `-repos=["repo1", "repo2"]`, "-count=10", "-boolFlag", "-tag=testTag"}
6263

6364
if err := flagSet.Parse(args); err != nil {
6465
t.Fatalf("flagset parsing failed: %v", err)
6566
}
6667
DerefFlagValues(flagSet, vars)
6768

68-
if v, ok := vars["repos"].([]string); ok {
69-
if len(v) != 2 {
70-
t.Fatalf("expected flag 'repos' values to have length %d but got %d", 2, len(v))
71-
}
72-
} else {
73-
t.Fatalf("expected flag 'repos' to have type of []string but got %T", v)
69+
expectedRepos := []string{"A", "B", "repo1", "repo2"}
70+
actualRepos, ok := vars["repos"].([]string)
71+
if !ok {
72+
t.Fatalf("failed to cast repos to []string, got %T", actualRepos)
7473
}
74+
if !slices.Equal(expectedRepos, actualRepos) {
75+
t.Fatalf("expected repos %v, got %v", expectedRepos, vars["repos"])
76+
}
77+
7578
if v, ok := vars["tag"].(string); ok {
7679
if v != "testTag" {
7780
t.Fatalf("expected flag 'tag' values to have value %q but got %q", "testTag", v)

0 commit comments

Comments
 (0)