Top 5.8% dependent packages on proxy.golang.org
Top 4.7% dependent repos on proxy.golang.org
Top 9.9% forks on proxy.golang.org
proxy.golang.org : github.com/uber-go/flagoverride
Package flags provides an interface for automatically creating command line options from a struct. Typically, if one wants to load from a yaml, one has to define a proper struct, then yaml.Unmarshal(), this is all good. However, there are situations where we want to load most of the configs from the file but overriding some configs. Let's say we use a yaml to config our Db connections and upon start of the application we load from the yaml file to get the necessary parameters to create the connection. Our base.yaml looks like this we want to load all the configs from it but we want to provide some flexibility for the program to connect via a different db user. We could define a --user command flag then after loading the yaml file, we override the user field with what we get from --user flag. If there are many overriding like this, manual define these flags is tedious. This package provides an automatic way to define this override, which is, given a struct, it'll create all the flags which are name using the field names of the struct. If one of these flags are set via command line, the struct will be modified in-place to reflect the value from command line, therefore the values of the fields in the struct are overridden YAML is just used as an example here. In practice, one can use any struct to define flags. Let's say we have our configration object as the following. The following code will create the following flags flags to subcommands are naturally suported. One can set Flatten to true when calling NewFlagMakerAdv, in which case, flags are created without namespacing. For example, will create the following flags Please be aware that usual GoLang flag creation rules apply, i.e., if there are duplication in flag names (in the flattened case it's more likely to happen unless the caller make due dilligence to create the struct properly), it panics. Note that not all types can have command line flags created for. map, channel and function type will not defien a flag corresponding to the field. Pointer types are properly handled and slice type will create multi-value command line flags. That is, e.g. if a field foo's type is []int, one can use --foo 10 --foo 15 --foo 20 to override this field value to be []int{10, 15, 20}. For now, only []int, []string and []float64 are supported in this fashion.
Registry
-
Source
- Documentation
- JSON
- codemeta.json
purl: pkg:golang/github.com/uber-go/flagoverride
License: MIT
Latest release: almost 9 years ago
First release: almost 9 years ago
Namespace: github.com/uber-go
Dependent packages: 1
Dependent repositories: 1
Stars: 20 on GitHub
Forks: 3 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 18 days ago