Skip to content

Commit 43f5ada

Browse files
committed
leverage flag package for update-checks-doc script
1 parent 6a56410 commit 43f5ada

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

scripts/update-checks-doc/main.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"compress/zlib"
77
"encoding/base64"
88
"errors"
9+
"flag"
910
"fmt"
11+
"io"
1012
"log"
1113
"os"
1214
"strings"
@@ -204,13 +206,12 @@ func (u *Updater) Update() {
204206
// │init│─►│anchor│─►│heading│─►│input │─►│input│─►│after│────┐
205207
// └────┘ │ │ │ │ │header│ │block│ │input│ │
206208
// └──▲───┘ └───────┘ └──▲───┘ └─────┘ └─────┘ ┌──▼───┐
207-
// │ │ │output│
208-
// │ │ │header│
209-
// │ ┌─┴─┐ ┌──────┐ ┌──────┐ └──┬───┘
210-
// ┌────┐ └──────────────────┤end│◄─│output│◄─│after │◄───┘
211-
// │done│◄───────────────────────┤ │ │block │ │output│
212-
// └────┘ └─▲─┘ └──────┘ └───┬──┘
213-
// └──────────────────┘
209+
// next │ more │ skip │output│
210+
// section│ example│ ┌──────────────│header│
211+
// │ ┌─┴─┐ ┌───▼──┐ ┌──────┐ └──┬───┘
212+
// ┌────┐ └──────────────────┤end│◄─│after │◄─│output│◄───┘
213+
// │done│◄───────────────────────┤ │ │output│ │block │
214+
// └────┘ └───┘ └──────┘ └──────┘
214215
//
215216
switch u.cur {
216217
case stateInit, stateEnd:
@@ -309,21 +310,27 @@ func Update(in []byte) ([]byte, error) {
309310
return u.End()
310311
}
311312

313+
var stderr io.Writer = os.Stderr
314+
312315
func Main(args []string) error {
313-
var path string
314316
var check bool
315-
switch len(args) {
316-
case 2:
317-
path = args[1]
318-
case 3:
319-
if args[1] != "-check" && args[1] != "--check" {
320-
return errors.New("usage: update-checks-doc [-check] FILE")
317+
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
318+
flags.BoolVar(&check, "check", false, "check the document is up-to-date")
319+
flags.SetOutput(stderr)
320+
flags.Usage = func() {
321+
fmt.Fprintln(stderr, "Usage: update-checks-doc [FLAGS] FILE\n\nFlags:")
322+
flags.PrintDefaults()
323+
}
324+
if err := flags.Parse(args[1:]); err != nil {
325+
if err == flag.ErrHelp {
326+
return nil
321327
}
322-
path = args[2]
323-
check = true
324-
default:
325-
return errors.New("usage: update-checks-doc [-check] FILE")
328+
return err
329+
}
330+
if flags.NArg() != 1 {
331+
return fmt.Errorf("this command should take exact one file path but got %v", flags.Args())
326332
}
333+
path := flags.Arg(0)
327334

328335
in, err := os.ReadFile(path)
329336
if err != nil {
@@ -342,7 +349,7 @@ func Main(args []string) error {
342349
}
343350

344351
if check {
345-
return errors.New("checks document has some update. run `go run ./scripts/update-checks-doc ./docs/checks.md` and commit the changes. the diff:\n\n" + cmp.Diff(in, out))
352+
return fmt.Errorf("checks document has some update. run `go run ./scripts/update-checks-doc %s` and commit the changes. the diff:\n\n%s", path, cmp.Diff(in, out))
346353
}
347354

348355
log.Printf("Overwrite the file with the updated content (%d bytes) at %q", len(out), path)

scripts/update-checks-doc/main_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"github.com/google/go-cmp/cmp"
1414
)
1515

16-
// Disable trace logs while running tests. Commenting out this function is useful when debugging test cases.
16+
// Do not mess up the test output. Temporarily commenting out this function may be helpful when debugging some test cases.
1717
func init() {
1818
log.SetOutput(io.Discard)
19+
stderr = io.Discard
1920
}
2021

2122
func must[T any](v T, err error) T {
@@ -72,6 +73,12 @@ func TestMainCheckOK(t *testing.T) {
7273
}
7374
}
7475

76+
func TestMainPrintHelp(t *testing.T) {
77+
if err := Main([]string{"exe", "-help"}); err != nil {
78+
t.Fatal(err)
79+
}
80+
}
81+
7582
func TestMainCheckError(t *testing.T) {
7683
if runtime.GOOS == "windows" {
7784
t.Skip("update-checks-doc doesn't support Windows")
@@ -85,11 +92,11 @@ func TestMainFileNotFound(t *testing.T) {
8592
}
8693

8794
func TestMainTooManyArgs(t *testing.T) {
88-
testErr(t, Main([]string{"exe", "a", "b", "c"}), "usage: update-checks-doc [-check] FILE")
95+
testErr(t, Main([]string{"exe", "a", "b", "c"}), "this command should take exact one file path but got")
8996
}
9097

9198
func TestMainInvalidCheckFlag(t *testing.T) {
92-
testErr(t, Main([]string{"exe", "-c", "foo.md"}), "usage: update-checks-doc [-check] FILE")
99+
testErr(t, Main([]string{"exe", "-c", "foo.md"}), "flag provided but not defined")
93100
}
94101

95102
func TestMainNoUpdate(t *testing.T) {

0 commit comments

Comments
 (0)