Skip to content

Commit 032f41f

Browse files
TMHSDigitalclaude
andcommitted
Guard the flag trait maps and drop pacingFlags' bool copy (#402)
Six name-keyed maps decide what a flag does in config files, env vars, resume and completion, and only fileFlags had a test that its keys are real flags. A new test checks every key of noDefaultFlags, listFlags, repeatFlags, unsavedSettings, pacingFlags and valueChoices against the registered flags (-h and -help are the flag package's own), so a renamed or misspelled flag fails CI. pacingFlags' value restated whether each flag is boolean; it is now a plain set, and pacingOverrides reads IsBoolFlag from the flags themselves, as completion does. Steps 1-2 of the issue; the optional single flagTraits table (step 3) is not done. Fixes #402 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b9bc33c commit 032f41f

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎internal/cli/config_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"flag"
7+
"maps"
78
"os"
89
"path/filepath"
910
"slices"
@@ -330,6 +331,32 @@ func TestFileFlagsAreComplete(t *testing.T) {
330331
}
331332
}
332333

334+
// TestFlagTraitMapsNameRealFlags covers #402: every name-keyed table of
335+
// flag behaviour names a registered flag, so a renamed or misspelled flag
336+
// fails here instead of silently doing nothing.
337+
func TestFlagTraitMapsNameRealFlags(t *testing.T) {
338+
registered := map[string]bool{}
339+
for _, f := range flagInfos() {
340+
registered[f.name] = true
341+
}
342+
// The flag package answers -h and -help itself; they are never registered.
343+
builtin := map[string]bool{"h": true, "help": true}
344+
for table, names := range map[string][]string{
345+
"noDefaultFlags": slices.Collect(maps.Keys(noDefaultFlags)),
346+
"listFlags": slices.Collect(maps.Keys(listFlags)),
347+
"repeatFlags": slices.Collect(maps.Keys(repeatFlags)),
348+
"unsavedSettings": slices.Collect(maps.Keys(unsavedSettings)),
349+
"pacingFlags": slices.Collect(maps.Keys(pacingFlags)),
350+
"valueChoices": slices.Collect(maps.Keys(valueChoices)),
351+
} {
352+
for _, name := range names {
353+
if !registered[name] && !builtin[name] {
354+
t.Errorf("%s lists -%s, which is not a flag", table, name)
355+
}
356+
}
357+
}
358+
}
359+
333360
// -print-config shows the attempts value a scan will use, not the raw 0
334361
// sentinel (#153).
335362
func TestPrintConfigEffectiveAttempts(t *testing.T) {

‎internal/cli/resume.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,11 @@ var unsavedSettings = map[string]bool{"seed": true, "state": true, "v": true, "p
245245
// pacingFlags may be given next to -resume to change how fast the rest of
246246
// the scan goes, for example after timeouts piled up (#309). They change
247247
// neither the candidate list nor the wildcard fingerprint, so the saved
248-
// position keeps its meaning; the value says whether the flag is a boolean.
248+
// position keeps its meaning. Whether one is a boolean comes from the flag
249+
// itself (#402).
249250
var pacingFlags = map[string]bool{
250-
"rate": false, "rate-per-resolver": false, "t": false, "timeout": false,
251-
"attempts": false, "auto-rate": true, "no-abort": true, "max-duration": false,
251+
"rate": true, "rate-per-resolver": true, "t": true, "timeout": true,
252+
"attempts": true, "auto-rate": true, "no-abort": true, "max-duration": true,
252253
}
253254

254255
// pacingFlagList names the pacing flags for messages, in a fixed order.
@@ -354,12 +355,19 @@ scan:
354355
// pacingOverrides checks that the arguments given next to -resume are all
355356
// pacing flags, and returns them with each value joined to its flag.
356357
func pacingOverrides(args []string) ([]string, error) {
358+
if len(args) == 0 {
359+
return nil, nil
360+
}
361+
boolFlag := map[string]bool{}
362+
for _, f := range flagInfos() {
363+
boolFlag[f.name] = f.isBool
364+
}
357365
var out []string
358366
for i := 0; i < len(args); i++ {
359367
a := args[i]
360368
name, _, hasValue := strings.Cut(strings.TrimPrefix(strings.TrimPrefix(a, "-"), "-"), "=")
361-
isBool, ok := pacingFlags[name]
362-
if !ok || !strings.HasPrefix(a, "-") {
369+
isBool := boolFlag[name]
370+
if !pacingFlags[name] || !strings.HasPrefix(a, "-") {
363371
return nil, fmt.Errorf("-resume takes only the pacing flags %s next to the state file, which holds the other arguments; got %q", pacingFlagList(), a)
364372
}
365373
if !isBool && !hasValue {

0 commit comments

Comments
 (0)