1package flags 2 3import ( 4 "testing" 5) 6 7func TestGroupInline(t *testing.T) { 8 var opts = struct { 9 Value bool `short:"v"` 10 11 Group struct { 12 G bool `short:"g"` 13 } `group:"Grouped Options"` 14 }{} 15 16 p, ret := assertParserSuccess(t, &opts, "-v", "-g") 17 18 assertStringArray(t, ret, []string{}) 19 20 if !opts.Value { 21 t.Errorf("Expected Value to be true") 22 } 23 24 if !opts.Group.G { 25 t.Errorf("Expected Group.G to be true") 26 } 27 28 if p.Command.Group.Find("Grouped Options") == nil { 29 t.Errorf("Expected to find group `Grouped Options'") 30 } 31} 32 33func TestGroupAdd(t *testing.T) { 34 var opts = struct { 35 Value bool `short:"v"` 36 }{} 37 38 var grp = struct { 39 G bool `short:"g"` 40 }{} 41 42 p := NewParser(&opts, Default) 43 g, err := p.AddGroup("Grouped Options", "", &grp) 44 45 if err != nil { 46 t.Fatalf("Unexpected error: %v", err) 47 return 48 } 49 50 ret, err := p.ParseArgs([]string{"-v", "-g", "rest"}) 51 52 if err != nil { 53 t.Fatalf("Unexpected error: %v", err) 54 return 55 } 56 57 assertStringArray(t, ret, []string{"rest"}) 58 59 if !opts.Value { 60 t.Errorf("Expected Value to be true") 61 } 62 63 if !grp.G { 64 t.Errorf("Expected Group.G to be true") 65 } 66 67 if p.Command.Group.Find("Grouped Options") != g { 68 t.Errorf("Expected to find group `Grouped Options'") 69 } 70 71 if p.Groups()[1] != g { 72 t.Errorf("Expected group %#v, but got %#v", g, p.Groups()[0]) 73 } 74 75 if g.Options()[0].ShortName != 'g' { 76 t.Errorf("Expected short name `g' but got %v", g.Options()[0].ShortName) 77 } 78} 79 80func TestGroupNestedInline(t *testing.T) { 81 var opts = struct { 82 Value bool `short:"v"` 83 84 Group struct { 85 G bool `short:"g"` 86 87 Nested struct { 88 N string `long:"n"` 89 } `group:"Nested Options"` 90 } `group:"Grouped Options"` 91 }{} 92 93 p, ret := assertParserSuccess(t, &opts, "-v", "-g", "--n", "n", "rest") 94 95 assertStringArray(t, ret, []string{"rest"}) 96 97 if !opts.Value { 98 t.Errorf("Expected Value to be true") 99 } 100 101 if !opts.Group.G { 102 t.Errorf("Expected Group.G to be true") 103 } 104 105 assertString(t, opts.Group.Nested.N, "n") 106 107 if p.Command.Group.Find("Grouped Options") == nil { 108 t.Errorf("Expected to find group `Grouped Options'") 109 } 110 111 if p.Command.Group.Find("Nested Options") == nil { 112 t.Errorf("Expected to find group `Nested Options'") 113 } 114} 115 116func TestGroupNestedInlineNamespace(t *testing.T) { 117 var opts = struct { 118 Opt string `long:"opt"` 119 120 Group struct { 121 Opt string `long:"opt"` 122 Group struct { 123 Opt string `long:"opt"` 124 } `group:"Subsubgroup" namespace:"sap"` 125 } `group:"Subgroup" namespace:"sip"` 126 }{} 127 128 p, ret := assertParserSuccess(t, &opts, "--opt", "a", "--sip.opt", "b", "--sip.sap.opt", "c", "rest") 129 130 assertStringArray(t, ret, []string{"rest"}) 131 132 assertString(t, opts.Opt, "a") 133 assertString(t, opts.Group.Opt, "b") 134 assertString(t, opts.Group.Group.Opt, "c") 135 136 for _, name := range []string{"Subgroup", "Subsubgroup"} { 137 if p.Command.Group.Find(name) == nil { 138 t.Errorf("Expected to find group '%s'", name) 139 } 140 } 141} 142 143func TestDuplicateShortFlags(t *testing.T) { 144 var opts struct { 145 Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"` 146 Variables []string `short:"v" long:"variable" description:"Set a variable value."` 147 } 148 149 args := []string{ 150 "--verbose", 151 "-v", "123", 152 "-v", "456", 153 } 154 155 _, err := ParseArgs(&opts, args) 156 157 if err == nil { 158 t.Errorf("Expected an error with type ErrDuplicatedFlag") 159 } else { 160 err2 := err.(*Error) 161 if err2.Type != ErrDuplicatedFlag { 162 t.Errorf("Expected an error with type ErrDuplicatedFlag") 163 } 164 } 165} 166 167func TestDuplicateLongFlags(t *testing.T) { 168 var opts struct { 169 Test1 []bool `short:"a" long:"testing" description:"Test 1"` 170 Test2 []string `short:"b" long:"testing" description:"Test 2."` 171 } 172 173 args := []string{ 174 "--testing", 175 } 176 177 _, err := ParseArgs(&opts, args) 178 179 if err == nil { 180 t.Errorf("Expected an error with type ErrDuplicatedFlag") 181 } else { 182 err2 := err.(*Error) 183 if err2.Type != ErrDuplicatedFlag { 184 t.Errorf("Expected an error with type ErrDuplicatedFlag") 185 } 186 } 187} 188 189func TestFindOptionByLongFlag(t *testing.T) { 190 var opts struct { 191 Testing bool `long:"testing" description:"Testing"` 192 } 193 194 p := NewParser(&opts, Default) 195 opt := p.FindOptionByLongName("testing") 196 197 if opt == nil { 198 t.Errorf("Expected option, but found none") 199 } 200 201 assertString(t, opt.LongName, "testing") 202} 203 204func TestFindOptionByShortFlag(t *testing.T) { 205 var opts struct { 206 Testing bool `short:"t" description:"Testing"` 207 } 208 209 p := NewParser(&opts, Default) 210 opt := p.FindOptionByShortName('t') 211 212 if opt == nil { 213 t.Errorf("Expected option, but found none") 214 } 215 216 if opt.ShortName != 't' { 217 t.Errorf("Expected 't', but got %v", opt.ShortName) 218 } 219} 220 221func TestFindOptionByLongFlagInSubGroup(t *testing.T) { 222 var opts struct { 223 Group struct { 224 Testing bool `long:"testing" description:"Testing"` 225 } `group:"sub-group"` 226 } 227 228 p := NewParser(&opts, Default) 229 opt := p.FindOptionByLongName("testing") 230 231 if opt == nil { 232 t.Errorf("Expected option, but found none") 233 } 234 235 assertString(t, opt.LongName, "testing") 236} 237 238func TestFindOptionByShortFlagInSubGroup(t *testing.T) { 239 var opts struct { 240 Group struct { 241 Testing bool `short:"t" description:"Testing"` 242 } `group:"sub-group"` 243 } 244 245 p := NewParser(&opts, Default) 246 opt := p.FindOptionByShortName('t') 247 248 if opt == nil { 249 t.Errorf("Expected option, but found none") 250 } 251 252 if opt.ShortName != 't' { 253 t.Errorf("Expected 't', but got %v", opt.ShortName) 254 } 255} 256