-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'better-handling-boolean-flags' into dev
- Loading branch information
Showing
7 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package myflag | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// BoolFlagFn returns a convenient function for handling boolean option on CLI to be used as parameter of the flag.BoolFn. | ||
// It works has the flag.BoolVar but, the presence of the flag, without value, set the flag to True | ||
|
||
func BoolFlagFn(b *bool, defaultValue bool) func(string) error { | ||
*b = defaultValue | ||
return func(v string) error { | ||
|
||
switch strings.ToLower(v) { | ||
case "": | ||
*b = true | ||
return nil | ||
default: | ||
var err error | ||
*b, err = strconv.ParseBool(v) | ||
if err != nil { | ||
err = fmt.Errorf("can't parse the parameter value: %w", err) | ||
} | ||
return err | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package myflag | ||
|
||
import "testing" | ||
|
||
func Test_BoolFn(t *testing.T) { | ||
tc := []struct { | ||
name string | ||
defaultValue bool | ||
want bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "", | ||
want: true, | ||
defaultValue: true, | ||
}, | ||
{ | ||
name: "", | ||
want: true, | ||
defaultValue: false, | ||
}, | ||
{ | ||
name: "true", | ||
want: true, | ||
}, | ||
{ | ||
name: "false", | ||
want: false, | ||
}, | ||
{ | ||
name: "1", | ||
want: true, | ||
}, | ||
{ | ||
name: "T", | ||
want: true, | ||
}, | ||
{ | ||
name: "F", | ||
want: false, | ||
}, | ||
{ | ||
name: "0", | ||
want: false, | ||
}, | ||
{ | ||
name: "let's be affirmative", | ||
want: false, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, c := range tc { | ||
t.Run(c.name, func(t *testing.T) { | ||
var b bool | ||
fn := BoolFlagFn(&b, c.defaultValue) | ||
|
||
err := fn(c.name) | ||
if (err == nil && c.wantErr) || (err != nil && !c.wantErr) { | ||
t.Errorf("fn(%q)=%v, expecting error: %v", c.name, err, c.wantErr) | ||
return | ||
} | ||
if b != c.want { | ||
t.Errorf("fn(%q) set b to %v, expecting: %v", c.name, b, c.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters