aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorTim Hockin <[email protected]>2015-04-23 17:52:45 -0700
committerbep <[email protected]>2015-04-24 02:52:39 +0200
commitbeb8d730f2afc6c3fe75312ad6f61a3dc47a64aa (patch)
tree6ae7af66faa4a58ba82e979f58f502366e42706a /flag_test.go
parent18d831e92d67eafd1b0db8af9ffddbd04f7ae1f4 (diff)
Allow flag name normalization for compare
This allows the FlagSet to define equivalent word-separators. Given this, one can make - and _ equivalent, so --foo-bar and --foo_bar both work.
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go
index a33c601..10e6ebb 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -233,6 +233,55 @@ func TestFlagSetParse(t *testing.T) {
testParse(NewFlagSet("test", ContinueOnError), t)
}
+func testNormalizedNames(args []string, t *testing.T) {
+ f := NewFlagSet("normalized", ContinueOnError)
+ if f.Parsed() {
+ t.Error("f.Parse() = true before Parse")
+ }
+ f.SetWordSeparators([]string{"-", "_"})
+ withDashFlag := f.Bool("with-dash-flag", false, "bool value")
+ withUnderFlag := f.Bool("with_under_flag", false, "bool value")
+ withBothFlag := f.Bool("with-both_flag", false, "bool value")
+ if err := f.Parse(args); err != nil {
+ t.Fatal(err)
+ }
+ if !f.Parsed() {
+ t.Error("f.Parse() = false after Parse")
+ }
+ if *withDashFlag != true {
+ t.Error("withDashFlag flag should be true, is ", *withDashFlag)
+ }
+ if *withUnderFlag != true {
+ t.Error("withUnderFlag flag should be true, is ", *withUnderFlag)
+ }
+ if *withBothFlag != true {
+ t.Error("withBothFlag flag should be true, is ", *withBothFlag)
+ }
+}
+
+func TestNormalizedNames(t *testing.T) {
+ args := []string{
+ "--with-dash-flag",
+ "--with-under-flag",
+ "--with-both-flag",
+ }
+ testNormalizedNames(args, t)
+
+ args = []string{
+ "--with_dash_flag",
+ "--with_under_flag",
+ "--with_both_flag",
+ }
+ testNormalizedNames(args, t)
+
+ args = []string{
+ "--with-dash_flag",
+ "--with-under_flag",
+ "--with-both_flag",
+ }
+ testNormalizedNames(args, t)
+}
+
// Declare a user-defined flag type.
type flagVar []string