github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/flags/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/micro/cli/v2"
     8  	"github.com/micro/go-micro/v2"
     9  )
    10  
    11  func main() {
    12  	service := micro.NewService(
    13  		// Add runtime flags
    14  		// We could do this below too
    15  		micro.Flags(
    16  			&cli.StringFlag{
    17  				Name:  "string_flag",
    18  				Usage: "This is a string flag",
    19  			},
    20  			&cli.IntFlag{
    21  				Name:  "int_flag",
    22  				Usage: "This is an int flag",
    23  			},
    24  			&cli.BoolFlag{
    25  				Name:  "bool_flag",
    26  				Usage: "This is a bool flag",
    27  			},
    28  		),
    29  	)
    30  
    31  	// Init will parse the command line flags. Any flags set will
    32  	// override the above settings. Options defined here will
    33  	// override anything set on the command line.
    34  	service.Init(
    35  		// Add runtime action
    36  		// We could actually do this above
    37  		micro.Action(func(c *cli.Context) error {
    38  			fmt.Printf("The string flag is: %s\n", c.String("string_flag"))
    39  			fmt.Printf("The int flag is: %d\n", c.Int("int_flag"))
    40  			fmt.Printf("The bool flag is: %t\n", c.Bool("bool_flag"))
    41  			// let's just exit because
    42  			os.Exit(0)
    43  			return nil
    44  		}),
    45  	)
    46  
    47  	// Run the server
    48  	if err := service.Run(); err != nil {
    49  		fmt.Println(err)
    50  	}
    51  }