@@ -7,21 +7,21 @@ package flag_test
7
7
import (
8
8
"flag"
9
9
"fmt"
10
- "os"
11
10
"time"
12
11
)
13
12
14
13
func ExampleFlagSet () {
15
14
start := func (args []string ) {
16
- fs := flag .NewFlagSet ("start" , flag .ExitOnError )
17
- addr := fs .String ("address" , ":8080" , "address to listen on" )
15
+ fs := flag .NewFlagSet ("start" , flag .ContinueOnError )
16
+ addr := fs .String ("address" , ":8080" , "` address` to listen on" )
18
17
fs .Parse (args )
19
18
fmt .Printf ("starting server on %s\n " , * addr )
20
19
}
21
20
22
21
stop := func (args []string ) {
23
- fs := flag .NewFlagSet ("stop" , flag .ExitOnError )
24
- timeout := fs .Duration ("timeout" , time .Second , "stop timeout" )
22
+ // On regular program use `flag.ExitOnError`.
23
+ fs := flag .NewFlagSet ("stop" , flag .ContinueOnError )
24
+ timeout := fs .Duration ("timeout" , time .Second , "stop timeout in `seconds`" )
25
25
fs .Parse (args )
26
26
fmt .Printf ("stopping server (timeout=%v)\n " , * timeout )
27
27
}
@@ -34,15 +34,17 @@ func ExampleFlagSet() {
34
34
case "stop" :
35
35
stop (subArgs )
36
36
default :
37
- fmt .Fprintf ( os . Stderr , "error: unknown command - %q\n " , args [1 ])
38
- os .Exit ( 1 )
37
+ 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.
39
39
}
40
40
}
41
41
42
42
main ([]string {"httpd" , "start" , "-address" , ":9999" })
43
43
main ([]string {"httpd" , "stop" })
44
+ main ([]string {"http" , "info" })
44
45
45
46
// Output:
46
47
// starting server on :9999
47
48
// stopping server (timeout=1s)
49
+ // error: unknown command - "info"
48
50
}
0 commit comments