From 2a3087783096a470e9412ac7d7650a07d5839374 Mon Sep 17 00:00:00 2001 From: Xucheng Zhou Date: Mon, 12 Oct 2026 17:59:49 +0800 Subject: [PATCH 1/3] Preserve current string values when copying to Go flags Signed-off-by: Xucheng Zhou --- golangflag.go | 2 +- golangflag_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/golangflag.go b/golangflag.go index 88a5c499..6e653c26 100644 --- a/golangflag.go +++ b/golangflag.go @@ -127,7 +127,7 @@ func (f *FlagSet) CopyToGoFlagSet(newSet *goflag.FlagSet) { switch value := flag.Value.(type) { case *stringValue: - newSet.StringVar((*string)(value), flag.Name, flag.DefValue, usage) + newSet.StringVar((*string)(value), flag.Name, *(*string)(value), usage) case *intValue: newSet.IntVar((*int)(value), flag.Name, *(*int)(value), usage) case *int64Value: diff --git a/golangflag_test.go b/golangflag_test.go index 9ef6494a..539a6637 100644 --- a/golangflag_test.go +++ b/golangflag_test.go @@ -133,3 +133,49 @@ func TestToGoflags(t *testing.T) { } } } + +func TestCopyToGoFlagSetPreservesCurrentStringValue(t *testing.T) { + for _, tc := range []struct { + name string + args []string + want string + }{ + {name: "default", want: "default"}, + {name: "parsed", args: []string{"--name=parsed"}, want: "parsed"}, + {name: "empty", args: []string{"--name="}, want: ""}, + } { + t.Run(tc.name, func(t *testing.T) { + pfs := NewFlagSet("source", ContinueOnError) + value := pfs.String("name", "default", "a string flag") + if err := pfs.Parse(tc.args); err != nil { + t.Fatal(err) + } + gfs := goflag.NewFlagSet("destination", goflag.ContinueOnError) + pfs.CopyToGoFlagSet(gfs) + if *value != tc.want { + t.Fatalf("copy changed source value: got %q, want %q", *value, tc.want) + } + if got := gfs.Lookup("name").Value.String(); got != tc.want { + t.Errorf("copied value = %q, want %q", got, tc.want) + } + if got := pfs.Lookup("name").DefValue; got != "default" { + t.Errorf("source default = %q, want %q", got, "default") + } + if got := gfs.Lookup("name").DefValue; got != tc.want { + t.Errorf("copied default = %q, want current value %q", got, tc.want) + } + if err := gfs.Set("name", "from-go"); err != nil { + t.Fatal(err) + } + if *value != "from-go" { + t.Errorf("Go flag update not shared with source: got %q", *value) + } + if err := pfs.Set("name", "from-pflag"); err != nil { + t.Fatal(err) + } + if got := gfs.Lookup("name").Value.String(); got != "from-pflag" { + t.Errorf("pflag update not shared with copy: got %q", got) + } + }) + } +} From a532d804f749c7e813ad379216323377abdcd96f Mon Sep 17 00:00:00 2001 From: Xucheng Zhou Date: Thu, 15 Oct 2026 02:04:28 +0800 Subject: [PATCH 2/3] ci: bump golangci-lint to v2.14 Signed-off-by: Xucheng Zhou --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7230c766..eda4f025 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -51,4 +51,4 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0 with: - version: v2.13 + version: v2.14 From 5079f6eea00368939fa94c56834308fa4bada8e3 Mon Sep 17 00:00:00 2001 From: Xucheng Zhou Date: Thu, 15 Oct 2026 11:48:02 +0800 Subject: [PATCH 3/3] test: check copied Go flags against the source set Signed-off-by: Xucheng Zhou --- golangflag_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golangflag_test.go b/golangflag_test.go index 539a6637..55d4cb67 100644 --- a/golangflag_test.go +++ b/golangflag_test.go @@ -116,7 +116,7 @@ func TestToGoflags(t *testing.T) { // Check for unexpected additional flags. gfs.VisitAll(func(gf *goflag.Flag) { - pf := gfs.Lookup(gf.Name) + pf := pfs.Lookup(gf.Name) if pf == nil { t.Errorf("%s: not found in pflag flag set", gf.Name) return