...
Source file
src/flag/example_flagset_test.go
Documentation: flag
1
2
3
4
5 package flag_test
6
7 import (
8 "flag"
9 "fmt"
10 "time"
11 )
12
13 func ExampleFlagSet() {
14
15 start := func(args []string) {
16
17 fs := flag.NewFlagSet("start", flag.ContinueOnError)
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 }
23 fmt.Printf("starting server on %s\n", *addr)
24 }
25
26 stop := func(args []string) {
27 fs := flag.NewFlagSet("stop", flag.ContinueOnError)
28 timeout := fs.Duration("timeout", time.Second, "stop timeout duration")
29 if err := fs.Parse(args); err != nil {
30 fmt.Printf("error: %s", err)
31 return
32 }
33 fmt.Printf("stopping server (timeout=%v)\n", *timeout)
34 }
35
36 main := func(args []string) {
37 subArgs := args[2:]
38 switch args[1] {
39 case "start":
40 start(subArgs)
41 case "stop":
42 stop(subArgs)
43 default:
44 fmt.Printf("error: unknown command - %q\n", args[1])
45
46 }
47 }
48
49 main([]string{"httpd", "start", "-addr", ":9999"})
50 main([]string{"httpd", "stop"})
51 main([]string{"http", "start", "-log-level", "verbose"})
52
53
54
55
56
57 }
58
View as plain text