Skip to content

Commit

Permalink
Enforced camel-case flags, even for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Feb 29, 2020
1 parent c912ebd commit b071e09
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,23 @@ func applyFlags(flagCfg interface{}) error {
var stringPtrFlags []stringVar

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

dotVal, _ := dot.Get(flagCfg, key)
if dotVal == nil {
continue
}

keys := strings.Split(key, ".")
for i, k := range keys {
keys[i] = strings.ToLower(string(k[0])) + k[1:]
}
camelKey := strings.Join(keys, ".")

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

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

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

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

0 comments on commit b071e09

Please sign in to comment.