Skip to content

Commit f8f1f65

Browse files
committed
Fix comments from Rob Pike
1 parent ca75650 commit f8f1f65

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/flag/example_flagset_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 The Go Authors. All rights reserved.
1+
// Copyright 2023 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -11,18 +11,25 @@ import (
1111
)
1212

1313
func ExampleFlagSet() {
14+
1415
start := func(args []string) {
16+
// A real program (not an example) would use flag.ExitOnError.
1517
fs := flag.NewFlagSet("start", flag.ContinueOnError)
16-
addr := fs.String("address", ":8080", "`address` to listen on")
17-
fs.Parse(args)
18+
addr := fs.String("addr", ":8080", "`address` to listen on")
19+
if err := fs.Parse(args); err != nil {
20+
fmt.Printf("error: %s", err)
21+
return
22+
}
1823
fmt.Printf("starting server on %s\n", *addr)
1924
}
2025

2126
stop := func(args []string) {
22-
// On regular program use `flag.ExitOnError`.
2327
fs := flag.NewFlagSet("stop", flag.ContinueOnError)
2428
timeout := fs.Duration("timeout", time.Second, "stop timeout in `seconds`")
25-
fs.Parse(args)
29+
if err := fs.Parse(args); err != nil {
30+
fmt.Printf("error: %s", err)
31+
return
32+
}
2633
fmt.Printf("stopping server (timeout=%v)\n", *timeout)
2734
}
2835

@@ -35,16 +42,16 @@ func ExampleFlagSet() {
3542
stop(subArgs)
3643
default:
3744
fmt.Printf("error: unknown command - %q\n", args[1])
38-
// On regular main print to `os.Stderr` and exit the program with non-zero value.
45+
// On a real program (not an example) print to os.Stderr and exit the program with non-zero value.
3946
}
4047
}
4148

42-
main([]string{"httpd", "start", "-address", ":9999"})
49+
main([]string{"httpd", "start", "-addr", ":9999"})
4350
main([]string{"httpd", "stop"})
44-
main([]string{"http", "info"})
51+
main([]string{"http", "start", "-log-level", "verbose"})
4552

4653
// Output:
4754
// starting server on :9999
4855
// stopping server (timeout=1s)
49-
// error: unknown command - "info"
56+
// error: flag provided but not defined: -log-level
5057
}

0 commit comments

Comments
 (0)