@@ -14,55 +14,55 @@ Package `flag/cli` provides a very simple interface for building command-lines a
1414
1515``` go
1616func main () {
17- cmd := &cli.Command {
18- Name: os.Args [0 ],
19- Usage: " A sample application" ,
20- UsageArgs: " [arguments]" ,
21- Flags: func (fs *flag.FlagSet ) {
22- fs.Bool (" verbose" , false , " enable verbose output" )
23- },
24- RunContext: func (parent context.Context , run func (ctx context.Context ) error ) error {
25- ctx , cancel := signal.NotifyContext (parent, os.Interrupt )
26- defer cancel ()
27- return run (ctx)
28- },
29- Subcommands: []*cli.Command {
30- {
31- Name: " serve" ,
32- Usage: " Start the server" ,
33- Flags: func (fs *flag.FlagSet ) {
34- fs.String (" config" , " config.yaml" , " configuration file (env $MYAPP_CONFIG)" )
35- fs.Int (" port" , 8080 , " port to listen on" )
36- },
37- FlagsEnvMap: map [string ]string {
38- " config" : " MYAPP_CONFIG" ,
39- },
40- FlagsRequired: []string {" config" },
41- Func: func (ctx context.Context , args []string ) error {
42- fmt.Printf (" starting server on port %d with config %s (verbose: %v )\n " ,
43- cli.Get (ctx, " port" ).(int ),
44- cli.Get (ctx, " config" ).(string ),
45- cli.Get (ctx, " verbose" ).(bool ),
46- )
47- return nil
48- },
49- },
50- {
51- Name: " version" ,
52- Usage: " Show version information" ,
53- Func: func (ctx context.Context , args []string ) error {
54- fmt.Println (" vX.Y.Z" )
55- return nil
56- },
57- },
58- },
59- }
60-
61- err := cmd.Run (context.Background (), os.Args [1 :])
62- if err != nil && err != flag.ErrHelp {
63- fmt.Fprintf (os.Stderr , " error: %v \n " , err)
64- os.Exit (2 )
65- }
17+ cmd := &cli.Command {
18+ Name: os.Args [0 ],
19+ Usage: " A sample application" ,
20+ UsageArgs: " [arguments]" ,
21+ Flags: func (fs *flag.FlagSet ) {
22+ fs.Bool (" verbose" , false , " enable verbose output" )
23+ },
24+ RunContext: func (parent context.Context , run func (ctx context.Context ) error ) error {
25+ ctx , cancel := signal.NotifyContext (parent, os.Interrupt )
26+ defer cancel ()
27+ return run (ctx)
28+ },
29+ Subcommands: []*cli.Command {
30+ {
31+ Name: " serve" ,
32+ Usage: " Start the server" ,
33+ Flags: func (fs *flag.FlagSet ) {
34+ fs.String (" config" , " config.yaml" , " configuration file (env $MYAPP_CONFIG)" )
35+ fs.Int (" port" , 8080 , " port to listen on" )
36+ },
37+ FlagsEnvMap: map [string ]string {
38+ " config" : " MYAPP_CONFIG" ,
39+ },
40+ FlagsRequired: []string {" config" },
41+ Func: func (ctx context.Context , args []string ) error {
42+ fmt.Printf (" starting server on port %d with config %s (verbose: %v )\n " ,
43+ cli.Get (ctx, " port" ).(int ),
44+ cli.Get (ctx, " config" ).(string ),
45+ cli.Get (ctx, " verbose" ).(bool ),
46+ )
47+ return nil
48+ },
49+ },
50+ {
51+ Name: " version" ,
52+ Usage: " Show version information" ,
53+ Func: func (ctx context.Context , args []string ) error {
54+ fmt.Println (" vX.Y.Z" )
55+ return nil
56+ },
57+ },
58+ },
59+ }
60+
61+ err := cmd.Run (context.Background (), os.Args [1 :])
62+ if err != nil && err != flag.ErrHelp {
63+ fmt.Fprintf (os.Stderr , " error: %v \n " , err)
64+ os.Exit (2 )
65+ }
6666}
6767```
6868```
@@ -103,12 +103,12 @@ Package `flag/values` provides generic implementations of the standard Go `flag.
103103
104104``` go
105105func main () {
106- flag.Var (values.Basic [int ](), " count" , " number of items" )
107- flag.Var (values.BasicList [string ](), " tag" , " tags (can be specified multiple times)" )
108- flag.Var (values.BasicSlice [string ](" ," ), " regions" , " comma-separated list of regions" )
109- flag.Var (values.Stringer (url.Parse ), " endpoint" , " API endpoint URL" )
110- flag.Parse ()
111- flag.VisitAll (func (f *flag.Flag ) { fmt.Printf (" %s : %v \n " , f.Name , f.Value .(flag.Getter ).Get ()) })
106+ flag.Var (values.Basic [int ](), " count" , " number of items" )
107+ flag.Var (values.BasicList [string ](), " tag" , " tags (can be specified multiple times)" )
108+ flag.Var (values.BasicSlice [string ](" ," ), " regions" , " comma-separated list of regions" )
109+ flag.Var (values.Stringer (url.Parse ), " endpoint" , " API endpoint URL" )
110+ flag.Parse ()
111+ flag.VisitAll (func (f *flag.Flag ) { fmt.Printf (" %s : %v \n " , f.Name , f.Value .(flag.Getter ).Get ()) })
112112}
113113```
114114```
@@ -123,16 +123,16 @@ Alternatively, the `Registerer` provides an interface analogous to `flag.FlagSet
123123
124124``` go
125125func main () {
126- var (
127- reg = values.FlagSetRegisterer (flag.CommandLine )
128- count = reg.Int (" count" , 10 , " number of items" )
129- email = reg.MailAddr (" email" , &mail.Address {}, " contact email" )
130- bind = reg.IPAddrPort (" bind" , netip.MustParseAddrPort (" 0.0.0.0:8080" ), " binding address" )
131- )
132- flag.Parse ()
133- fmt.Printf (" Count: %d \n " , *count)
134- fmt.Printf (" Email: %v \n " , *email)
135- fmt.Printf (" Bind: %v \n " , *bind)
126+ var (
127+ reg = values.FlagSetRegisterer (flag.CommandLine )
128+ count = reg.Int (" count" , 10 , " number of items" )
129+ email = reg.MailAddr (" email" , &mail.Address {}, " contact email" )
130+ bind = reg.IPAddrPort (" bind" , netip.MustParseAddrPort (" 0.0.0.0:8080" ), " binding address" )
131+ )
132+ flag.Parse ()
133+ fmt.Printf (" Count: %d \n " , *count)
134+ fmt.Printf (" Email: %v \n " , *email)
135+ fmt.Printf (" Bind: %v \n " , *bind)
136136}
137137```
138138```
0 commit comments