[go] flag: add All iterators and Flag.IsSet

4 views
Skip to first unread message

qiu laidongfeng (Gerrit)

unread,
Jul 21, 2026, 11:35:00 PM (5 days ago) Jul 21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

qiu laidongfeng has uploaded the change for review

Commit message

flag: add All iterators and Flag.IsSet

Fixes #65675
Change-Id: I8d381326418b42ca282c84d829cafe907bb72171

Change diff

diff --git a/src/flag/flag.go b/src/flag/flag.go
index 71902f7..2bc7416 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -87,6 +87,7 @@
"errors"
"fmt"
"io"
+ "iter"
"os"
"reflect"
"runtime"
@@ -410,6 +411,7 @@
Usage string // help message
Value Value // value as set
DefValue string // default value (as text); for usage message
+ IsSet bool
}

// sortFlags returns the flags as a slice in lexicographical sorted order.
@@ -459,6 +461,22 @@
}
}

+// All yields the flags in lexicographical order.
+func (f *FlagSet) All() iter.Seq[*Flag] {
+ return func(yield func(*Flag) bool) {
+ for _, flag := range sortFlags(f.formal) {
+ if !yield(flag) {
+ break
+ }
+ }
+ }
+}
+
+// All yields all command-line flags, in lexicographical order.
+func All() iter.Seq[*Flag] {
+ return CommandLine.All()
+}
+
// VisitAll visits the command-line flags in lexicographical order, calling
// fn for each. It visits all flags, even those not set.
func VisitAll(fn func(*Flag)) {
@@ -1016,7 +1034,7 @@
}

// Remember the default value as a string; it won't change.
- flag := &Flag{name, usage, value, value.String()}
+ flag := &Flag{name, usage, value, value.String(), false}
_, alreadythere := f.formal[name]
if alreadythere {
var msg string
@@ -1114,6 +1132,7 @@
}
return false, f.failf("flag provided but not defined: -%s", name)
}
+ flag.IsSet = true

if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
if hasValue {
diff --git a/src/flag/flag_test.go b/src/flag/flag_test.go
index 278ae7e..da6f77c 100644
--- a/src/flag/flag_test.go
+++ b/src/flag/flag_test.go
@@ -190,6 +190,11 @@
if err := f.Parse(args); err != nil {
t.Fatal(err)
}
+ for tmp := range f.All() {
+ if !tmp.IsSet {
+ t.Errorf("flag %s should be set, but is not", tmp.Name)
+ }
+ }
if !f.Parsed() {
t.Error("f.Parse() = false after Parse")
}

Change information

Files:
  • M src/flag/flag.go
  • M src/flag/flag_test.go
Change size: S
Delta: 2 files changed, 25 insertions(+), 1 deletion(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
Gerrit-Change-Number: 804020
Gerrit-PatchSet: 1
Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Jul 22, 2026, 10:02:09 AM (4 days ago) Jul 22
to qiu laidongfeng, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Rob Pike and qiu laidongfeng

Alan Donovan added 2 comments

File src/flag/flag.go
Line 464, Patchset 1 (Latest):// All yields the flags in lexicographical order.
Alan Donovan . unresolved

Let's keep this part:

// It visits all flags, even those not set.


Ditto at func All.

Line 1135, Patchset 1 (Latest): flag.IsSet = true
Alan Donovan . unresolved

There's an inconsistency between `flag.IsSet` and membership of `f.actual[flag]` that we should avoid. Given that we cannot change f.actual at this stage, I think that means we must interpret IsSet as "Flag.Set was called successfully" (and document IsSet as such). That means this statement should be moved to L1161.

Could you first add a test case that asserts this consistency, and fails with the current patchset? Thanks.

Open in Gerrit

Related details

Attention is currently required from:
  • Rob Pike
  • qiu laidongfeng
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
    Gerrit-Change-Number: 804020
    Gerrit-PatchSet: 1
    Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Rob Pike <r...@golang.org>
    Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: qiu laidongfeng <26454...@qq.com>
    Gerrit-Attention: Rob Pike <r...@golang.org>
    Gerrit-Comment-Date: Wed, 22 Jul 2026 14:02:00 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Jul 23, 2026, 12:44:54 PM (3 days ago) Jul 23
    to qiu laidongfeng, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Rob Pike and qiu laidongfeng

    Alan Donovan added 1 comment

    Patchset-level comments
    File-level comment, Patchset 1 (Latest):
    Alan Donovan . resolved

    In addition to the test of the `composite` analyzer you fixed in x/tools, there's another little test in src/cmd/vet/testdata/composite/composite.go that needs a similar tweak.

    Gerrit-Comment-Date: Thu, 23 Jul 2026 16:44:49 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    qiu laidongfeng (Gerrit)

    unread,
    Jul 24, 2026, 1:38:08 AM (2 days ago) Jul 24
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Rob Pike and qiu laidongfeng

    qiu laidongfeng uploaded new patchset

    qiu laidongfeng uploaded patch set #2 to this change.
    Following approvals got outdated and were removed:
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Rob Pike
    • qiu laidongfeng
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
    Gerrit-Change-Number: 804020
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    qiu laidongfeng (Gerrit)

    unread,
    Jul 24, 2026, 1:39:03 AM (2 days ago) Jul 24
    to goph...@pubsubhelper.golang.org, Alan Donovan, golang...@luci-project-accounts.iam.gserviceaccount.com, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Alan Donovan and Rob Pike

    qiu laidongfeng voted and added 3 comments

    Votes added by qiu laidongfeng

    Commit-Queue+1

    3 comments

    Patchset-level comments
    Alan Donovan . resolved

    In addition to the test of the `composite` analyzer you fixed in x/tools, there's another little test in src/cmd/vet/testdata/composite/composite.go that needs a similar tweak.

    qiu laidongfeng

    Done

    File src/flag/flag.go
    Line 464, Patchset 1:// All yields the flags in lexicographical order.
    Alan Donovan . resolved

    Let's keep this part:

    // It visits all flags, even those not set.


    Ditto at func All.

    qiu laidongfeng

    Done

    Line 1135, Patchset 1: flag.IsSet = true
    Alan Donovan . resolved

    There's an inconsistency between `flag.IsSet` and membership of `f.actual[flag]` that we should avoid. Given that we cannot change f.actual at this stage, I think that means we must interpret IsSet as "Flag.Set was called successfully" (and document IsSet as such). That means this statement should be moved to L1161.

    Could you first add a test case that asserts this consistency, and fails with the current patchset? Thanks.

    qiu laidongfeng

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    • Rob Pike
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
      Gerrit-Change-Number: 804020
      Gerrit-PatchSet: 2
      Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Rob Pike <r...@golang.org>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Fri, 24 Jul 2026 05:38:56 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Alan Donovan <adon...@google.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Alan Donovan (Gerrit)

      unread,
      Jul 24, 2026, 9:38:01 AM (2 days ago) Jul 24
      to qiu laidongfeng, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Rob Pike and qiu laidongfeng

      Alan Donovan added 4 comments

      Patchset-level comments
      File-level comment, Patchset 2 (Latest):
      Alan Donovan . resolved

      Just a few last things. Thanks.

      File doc/next/6-stdlib/99-minor/flag/65675.md
      Line 1, Patchset 2 (Latest):Add iterator-style APIs.
      Alan Donovan . unresolved

      Release notes use prose:

      The new [FlagSet.All] method returns an iterator over all the flags in the FlagSet;
      the function [All] does the same for the global [CommandLine] flag set.
      The new [Flag.IsSet] field indicates whether the flag has been set.

      File src/flag/flag.go
      Line 547, Patchset 2 (Latest): flag.IsSet = true
      Alan Donovan . unresolved

      Let's move this to L544 so that we keep Set and IsSet together, and the two operations on f.actual together.

      Line 1166, Patchset 2 (Latest): flag.IsSet = true
      Alan Donovan . unresolved

      ditto, move to L1163.

      (It's tempting to factor the common tails of Set + IsSet + f.actual into a function, but the error messages from Set have been carefully specialized here, so best not.)

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Rob Pike
      • qiu laidongfeng
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
        Gerrit-Change-Number: 804020
        Gerrit-PatchSet: 2
        Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
        Gerrit-Reviewer: Alan Donovan <adon...@google.com>
        Gerrit-Reviewer: Rob Pike <r...@golang.org>
        Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Attention: qiu laidongfeng <26454...@qq.com>
        Gerrit-Attention: Rob Pike <r...@golang.org>
        Gerrit-Comment-Date: Fri, 24 Jul 2026 13:37:57 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy

        qiu laidongfeng (Gerrit)

        unread,
        Jul 24, 2026, 9:43:29 AM (2 days ago) Jul 24
        to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
        Attention needed from Rob Pike and qiu laidongfeng

        qiu laidongfeng uploaded new patchset

        qiu laidongfeng uploaded patch set #3 to this change.
        Following approvals got outdated and were removed:

        Related details

        Attention is currently required from:
        • Rob Pike
        • qiu laidongfeng
        Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement is not satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: newpatchset
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
          Gerrit-Change-Number: 804020
          Gerrit-PatchSet: 3
          unsatisfied_requirement
          open
          diffy

          qiu laidongfeng (Gerrit)

          unread,
          Jul 24, 2026, 9:43:57 AM (2 days ago) Jul 24
          to goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Alan Donovan, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
          Attention needed from Alan Donovan and Rob Pike

          qiu laidongfeng voted and added 3 comments

          Votes added by qiu laidongfeng

          Commit-Queue+1

          3 comments

          File doc/next/6-stdlib/99-minor/flag/65675.md
          Line 1, Patchset 2:Add iterator-style APIs.
          Alan Donovan . resolved

          Release notes use prose:

          The new [FlagSet.All] method returns an iterator over all the flags in the FlagSet;
          the function [All] does the same for the global [CommandLine] flag set.
          The new [Flag.IsSet] field indicates whether the flag has been set.

          qiu laidongfeng

          Done

          File src/flag/flag.go
          Line 547, Patchset 2: flag.IsSet = true
          Alan Donovan . resolved

          Let's move this to L544 so that we keep Set and IsSet together, and the two operations on f.actual together.

          qiu laidongfeng

          Done

          Line 1166, Patchset 2: flag.IsSet = true
          Alan Donovan . resolved

          ditto, move to L1163.

          (It's tempting to factor the common tails of Set + IsSet + f.actual into a function, but the error messages from Set have been carefully specialized here, so best not.)

          qiu laidongfeng

          Done

          Open in Gerrit

          Related details

          Attention is currently required from:
          • Alan Donovan
          • Rob Pike
          Submit Requirements:
            • requirement is not satisfiedCode-Review
            • requirement satisfiedNo-Unresolved-Comments
            • requirement is not satisfiedReview-Enforcement
            • requirement is not satisfiedTryBots-Pass
            Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
            Gerrit-MessageType: comment
            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
            Gerrit-Change-Number: 804020
            Gerrit-PatchSet: 3
            Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
            Gerrit-Reviewer: Alan Donovan <adon...@google.com>
            Gerrit-Reviewer: Rob Pike <r...@golang.org>
            Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
            Gerrit-CC: Gopher Robot <go...@golang.org>
            Gerrit-Attention: Rob Pike <r...@golang.org>
            Gerrit-Attention: Alan Donovan <adon...@google.com>
            Gerrit-Comment-Date: Fri, 24 Jul 2026 13:43:48 +0000
            unsatisfied_requirement
            satisfied_requirement
            open
            diffy

            Alan Donovan (Gerrit)

            unread,
            Jul 24, 2026, 10:09:25 AM (2 days ago) Jul 24
            to qiu laidongfeng, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Rob Pike, Gopher Robot, golang-co...@googlegroups.com
            Attention needed from Rob Pike and qiu laidongfeng

            Alan Donovan voted

            Auto-Submit+1
            Code-Review+2
            Commit-Queue+1
            Open in Gerrit

            Related details

            Attention is currently required from:
            • Rob Pike
            • qiu laidongfeng
            Submit Requirements:
            • requirement satisfiedCode-Review
            • requirement satisfiedNo-Unresolved-Comments
            • requirement is not satisfiedReview-Enforcement
            • requirement satisfiedTryBots-Pass
            Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
            Gerrit-MessageType: comment
            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-Change-Id: I8d381326418b42ca282c84d829cafe907bb72171
            Gerrit-Change-Number: 804020
            Gerrit-PatchSet: 3
            Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
            Gerrit-Reviewer: Alan Donovan <adon...@google.com>
            Gerrit-Reviewer: Rob Pike <r...@golang.org>
            Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
            Gerrit-CC: Gopher Robot <go...@golang.org>
            Gerrit-Attention: qiu laidongfeng <26454...@qq.com>
            Gerrit-Attention: Rob Pike <r...@golang.org>
            Gerrit-Comment-Date: Fri, 24 Jul 2026 14:09:21 +0000
            Gerrit-HasComments: No
            Gerrit-Has-Labels: Yes
            satisfied_requirement
            unsatisfied_requirement
            open
            diffy
            Reply all
            Reply to author
            Forward
            0 new messages