github.com/annwntech/go-micro/v2@v2.9.5/config/source/cli/README.md (about) 1 # cli Source 2 3 The cli source reads config from parsed flags via a cli.Context. 4 5 ## Format 6 7 We expect the use of the `micro/cli` package. Upper case flags will be lower cased. Dashes will be used as delimiters for nesting. 8 9 ### Example 10 11 ```go 12 micro.Flags( 13 cli.StringFlag{ 14 Name: "database-address", 15 Value: "127.0.0.1", 16 Usage: "the db address", 17 }, 18 cli.IntFlag{ 19 Name: "database-port", 20 Value: 3306, 21 Usage: "the db port", 22 }, 23 ) 24 ``` 25 26 Becomes 27 28 ```json 29 { 30 "database": { 31 "address": "127.0.0.1", 32 "port": 3306 33 } 34 } 35 ``` 36 37 ## New and Load Source 38 39 Because a cli.Context is needed to retrieve the flags and their values, it is recommended to build your source from within a cli.Action. 40 41 ```go 42 43 func main() { 44 // New Service 45 service := micro.NewService( 46 micro.Name("example"), 47 micro.Flags( 48 cli.StringFlag{ 49 Name: "database-address", 50 Value: "127.0.0.1", 51 Usage: "the db address", 52 }, 53 ), 54 ) 55 56 var clisrc source.Source 57 58 service.Init( 59 micro.Action(func(c *cli.Context) { 60 clisrc = cli.NewSource( 61 cli.Context(c), 62 ) 63 // Alternatively, just setup your config right here 64 }), 65 ) 66 67 // ... Load and use that source ... 68 conf := config.NewConfig() 69 conf.Load(clisrc) 70 } 71 ```