Skip to content

Commit

Permalink
Even simpler flag-building code
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Feb 29, 2020
1 parent b071e09 commit 23d8d99
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ func applyFlags(flagCfg interface{}) error {
var stringPtrFlags []stringVar

for _, key := range keys {
valuePath := strings.ReplaceAll(key, ".", "-")

dotVal, _ := dot.Get(flagCfg, key)
if dotVal == nil {
continue
Expand All @@ -139,12 +137,12 @@ func applyFlags(flagCfg interface{}) error {
for i, k := range keys {
keys[i] = strings.ToLower(string(k[0])) + k[1:]
}
camelKey := strings.Join(keys, ".")
camelKey := strings.Join(keys, "-")

if ds, ok := dotVal.(string); ok {
strFlag := stringVar{
Name: valuePath,
Key: camelKey,
Name: camelKey,
Key: key,
Ptr: nil,
}
strFlag.Ptr = flag.String(strFlag.Name, ds, "")
Expand All @@ -154,8 +152,8 @@ func applyFlags(flagCfg interface{}) error {

if dv, ok := dotVal.(bool); ok {
boolFlag := boolVar{
Name: valuePath,
Key: camelKey,
Name: camelKey,
Key: key,
Ptr: nil,
}
boolFlag.Ptr = flag.Bool(boolFlag.Name, dv, "")
Expand All @@ -165,8 +163,8 @@ func applyFlags(flagCfg interface{}) error {

if dvp, ok := dotVal.(*bool); ok {
boolFlag := boolVar{
Name: valuePath,
Key: camelKey,
Name: camelKey,
Key: key,
Ptr: dvp,
}
flag.BoolVar(dvp, boolFlag.Name, false, "")
Expand All @@ -176,8 +174,8 @@ func applyFlags(flagCfg interface{}) error {

if svp, ok := dotVal.(*string); ok {
strFlag := stringVar{
Name: valuePath,
Key: camelKey,
Name: camelKey,
Key: key,
Ptr: svp,
}
flag.StringVar(svp, strFlag.Name, "", "")
Expand Down

0 comments on commit 23d8d99

Please sign in to comment.