1package flags 2 3import ( 4 "fmt" 5 "testing" 6) 7 8func TestShort(t *testing.T) { 9 var opts = struct { 10 Value bool `short:"v"` 11 }{} 12 13 ret := assertParseSuccess(t, &opts, "-v") 14 15 assertStringArray(t, ret, []string{}) 16 17 if !opts.Value { 18 t.Errorf("Expected Value to be true") 19 } 20} 21 22func TestShortTooLong(t *testing.T) { 23 var opts = struct { 24 Value bool `short:"vv"` 25 }{} 26 27 assertParseFail(t, ErrShortNameTooLong, "short names can only be 1 character long, not `vv'", &opts) 28} 29 30func TestShortRequired(t *testing.T) { 31 var opts = struct { 32 Value bool `short:"v" required:"true"` 33 }{} 34 35 assertParseFail(t, ErrRequired, fmt.Sprintf("the required flag `%cv' was not specified", defaultShortOptDelimiter), &opts) 36} 37 38func TestShortRequiredFalsy1(t *testing.T) { 39 var opts = struct { 40 Value bool `short:"v" required:"false"` 41 }{} 42 43 assertParseSuccess(t, &opts) 44} 45 46func TestShortRequiredFalsy2(t *testing.T) { 47 var opts = struct { 48 Value bool `short:"v" required:"no"` 49 }{} 50 51 assertParseSuccess(t, &opts) 52} 53 54func TestShortMultiConcat(t *testing.T) { 55 var opts = struct { 56 V bool `short:"v"` 57 O bool `short:"o"` 58 F bool `short:"f"` 59 }{} 60 61 ret := assertParseSuccess(t, &opts, "-vo", "-f") 62 63 assertStringArray(t, ret, []string{}) 64 65 if !opts.V { 66 t.Errorf("Expected V to be true") 67 } 68 69 if !opts.O { 70 t.Errorf("Expected O to be true") 71 } 72 73 if !opts.F { 74 t.Errorf("Expected F to be true") 75 } 76} 77 78func TestShortMultiRequiredConcat(t *testing.T) { 79 var opts = struct { 80 V bool `short:"v" required:"true"` 81 O bool `short:"o" required:"true"` 82 F bool `short:"f" required:"true"` 83 }{} 84 85 ret := assertParseSuccess(t, &opts, "-vo", "-f") 86 87 assertStringArray(t, ret, []string{}) 88 89 if !opts.V { 90 t.Errorf("Expected V to be true") 91 } 92 93 if !opts.O { 94 t.Errorf("Expected O to be true") 95 } 96 97 if !opts.F { 98 t.Errorf("Expected F to be true") 99 } 100} 101 102func TestShortMultiSlice(t *testing.T) { 103 var opts = struct { 104 Values []bool `short:"v"` 105 }{} 106 107 ret := assertParseSuccess(t, &opts, "-v", "-v") 108 109 assertStringArray(t, ret, []string{}) 110 assertBoolArray(t, opts.Values, []bool{true, true}) 111} 112 113func TestShortMultiSliceConcat(t *testing.T) { 114 var opts = struct { 115 Values []bool `short:"v"` 116 }{} 117 118 ret := assertParseSuccess(t, &opts, "-vvv") 119 120 assertStringArray(t, ret, []string{}) 121 assertBoolArray(t, opts.Values, []bool{true, true, true}) 122} 123 124func TestShortWithEqualArg(t *testing.T) { 125 var opts = struct { 126 Value string `short:"v"` 127 }{} 128 129 ret := assertParseSuccess(t, &opts, "-v=value") 130 131 assertStringArray(t, ret, []string{}) 132 assertString(t, opts.Value, "value") 133} 134 135func TestShortWithArg(t *testing.T) { 136 var opts = struct { 137 Value string `short:"v"` 138 }{} 139 140 ret := assertParseSuccess(t, &opts, "-vvalue") 141 142 assertStringArray(t, ret, []string{}) 143 assertString(t, opts.Value, "value") 144} 145 146func TestShortArg(t *testing.T) { 147 var opts = struct { 148 Value string `short:"v"` 149 }{} 150 151 ret := assertParseSuccess(t, &opts, "-v", "value") 152 153 assertStringArray(t, ret, []string{}) 154 assertString(t, opts.Value, "value") 155} 156 157func TestShortMultiWithEqualArg(t *testing.T) { 158 var opts = struct { 159 F []bool `short:"f"` 160 Value string `short:"v"` 161 }{} 162 163 assertParseFail(t, ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%cv'", defaultShortOptDelimiter), &opts, "-ffv=value") 164} 165 166func TestShortMultiArg(t *testing.T) { 167 var opts = struct { 168 F []bool `short:"f"` 169 Value string `short:"v"` 170 }{} 171 172 ret := assertParseSuccess(t, &opts, "-ffv", "value") 173 174 assertStringArray(t, ret, []string{}) 175 assertBoolArray(t, opts.F, []bool{true, true}) 176 assertString(t, opts.Value, "value") 177} 178 179func TestShortMultiArgConcatFail(t *testing.T) { 180 var opts = struct { 181 F []bool `short:"f"` 182 Value string `short:"v"` 183 }{} 184 185 assertParseFail(t, ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%cv'", defaultShortOptDelimiter), &opts, "-ffvvalue") 186} 187 188func TestShortMultiArgConcat(t *testing.T) { 189 var opts = struct { 190 F []bool `short:"f"` 191 Value string `short:"v"` 192 }{} 193 194 ret := assertParseSuccess(t, &opts, "-vff") 195 196 assertStringArray(t, ret, []string{}) 197 assertString(t, opts.Value, "ff") 198} 199 200func TestShortOptional(t *testing.T) { 201 var opts = struct { 202 F []bool `short:"f"` 203 Value string `short:"v" optional:"yes" optional-value:"value"` 204 }{} 205 206 ret := assertParseSuccess(t, &opts, "-fv", "f") 207 208 assertStringArray(t, ret, []string{"f"}) 209 assertString(t, opts.Value, "value") 210} 211 212func TestShortOptionalFalsy1(t *testing.T) { 213 var opts = struct { 214 F []bool `short:"f"` 215 Value string `short:"v" optional:"false" optional-value:"value"` 216 }{} 217 218 ret := assertParseSuccess(t, &opts, "-fv", "f") 219 220 assertStringArray(t, ret, []string{}) 221 assertString(t, opts.Value, "f") 222} 223 224func TestShortOptionalFalsy2(t *testing.T) { 225 var opts = struct { 226 F []bool `short:"f"` 227 Value string `short:"v" optional:"no" optional-value:"value"` 228 }{} 229 230 ret := assertParseSuccess(t, &opts, "-fv", "f") 231 232 assertStringArray(t, ret, []string{}) 233 assertString(t, opts.Value, "f") 234} 235