@@ -1132,8 +1132,13 @@ TEST(CommandLineTest, GroupingWithValue) {
1132
1132
cl::ResetCommandLineParser ();
1133
1133
1134
1134
StackOption<bool > OptF (" f" , cl::Grouping, cl::desc (" Some flag" ));
1135
+ StackOption<bool > OptB (" b" , cl::Grouping, cl::desc (" Another flag" ));
1136
+ StackOption<bool > OptD (" d" , cl::Grouping, cl::ValueDisallowed,
1137
+ cl::desc (" ValueDisallowed option" ));
1135
1138
StackOption<std::string> OptV (" v" , cl::Grouping,
1136
- cl::desc (" Grouping option with a value" ));
1139
+ cl::desc (" ValueRequired option" ));
1140
+ StackOption<std::string> OptO (" o" , cl::Grouping, cl::ValueOptional,
1141
+ cl::desc (" ValueOptional option" ));
1137
1142
1138
1143
// Should be possible to use an option which requires a value
1139
1144
// at the end of a group.
@@ -1142,12 +1147,178 @@ TEST(CommandLineTest, GroupingWithValue) {
1142
1147
cl::ParseCommandLineOptions (3 , args1, StringRef (), &llvm::nulls ()));
1143
1148
EXPECT_TRUE (OptF);
1144
1149
EXPECT_STREQ (" val1" , OptV.c_str ());
1150
+ OptV.clear ();
1145
1151
cl::ResetAllOptionOccurrences ();
1146
1152
1147
1153
// Should not crash if it is accidentally used elsewhere in the group.
1148
1154
const char *args2[] = {" prog" , " -vf" , " val2" };
1149
1155
EXPECT_FALSE (
1150
1156
cl::ParseCommandLineOptions (3 , args2, StringRef (), &llvm::nulls ()));
1157
+ OptV.clear ();
1158
+ cl::ResetAllOptionOccurrences ();
1159
+
1160
+ // Should allow the "opt=value" form at the end of the group
1161
+ const char *args3[] = {" prog" , " -fv=val3" };
1162
+ EXPECT_TRUE (
1163
+ cl::ParseCommandLineOptions (2 , args3, StringRef (), &llvm::nulls ()));
1164
+ EXPECT_TRUE (OptF);
1165
+ EXPECT_STREQ (" val3" , OptV.c_str ());
1166
+ OptV.clear ();
1167
+ cl::ResetAllOptionOccurrences ();
1168
+
1169
+ // Should allow assigning a value for a ValueOptional option
1170
+ // at the end of the group
1171
+ const char *args4[] = {" prog" , " -fo=val4" };
1172
+ EXPECT_TRUE (
1173
+ cl::ParseCommandLineOptions (2 , args4, StringRef (), &llvm::nulls ()));
1174
+ EXPECT_TRUE (OptF);
1175
+ EXPECT_STREQ (" val4" , OptO.c_str ());
1176
+ OptO.clear ();
1177
+ cl::ResetAllOptionOccurrences ();
1178
+
1179
+ // Should assign an empty value if a ValueOptional option is used elsewhere
1180
+ // in the group.
1181
+ const char *args5[] = {" prog" , " -fob" };
1182
+ EXPECT_TRUE (
1183
+ cl::ParseCommandLineOptions (2 , args5, StringRef (), &llvm::nulls ()));
1184
+ EXPECT_TRUE (OptF);
1185
+ EXPECT_EQ (1 , OptO.getNumOccurrences ());
1186
+ EXPECT_EQ (1 , OptB.getNumOccurrences ());
1187
+ EXPECT_TRUE (OptO.empty ());
1188
+ cl::ResetAllOptionOccurrences ();
1189
+
1190
+ // Should not allow an assignment for a ValueDisallowed option.
1191
+ const char *args6[] = {" prog" , " -fd=false" };
1192
+ EXPECT_FALSE (
1193
+ cl::ParseCommandLineOptions (2 , args6, StringRef (), &llvm::nulls ()));
1194
+ }
1195
+
1196
+ TEST (CommandLineTest, GroupingAndPrefix) {
1197
+ cl::ResetCommandLineParser ();
1198
+
1199
+ StackOption<bool > OptF (" f" , cl::Grouping, cl::desc (" Some flag" ));
1200
+ StackOption<bool > OptB (" b" , cl::Grouping, cl::desc (" Another flag" ));
1201
+ StackOption<std::string> OptP (" p" , cl::Prefix, cl::Grouping,
1202
+ cl::desc (" Prefix and Grouping" ));
1203
+ StackOption<std::string> OptA (" a" , cl::AlwaysPrefix, cl::Grouping,
1204
+ cl::desc (" AlwaysPrefix and Grouping" ));
1205
+
1206
+ // Should be possible to use a cl::Prefix option without grouping.
1207
+ const char *args1[] = {" prog" , " -pval1" };
1208
+ EXPECT_TRUE (
1209
+ cl::ParseCommandLineOptions (2 , args1, StringRef (), &llvm::nulls ()));
1210
+ EXPECT_STREQ (" val1" , OptP.c_str ());
1211
+ OptP.clear ();
1212
+ cl::ResetAllOptionOccurrences ();
1213
+
1214
+ // Should be possible to pass a value in a separate argument.
1215
+ const char *args2[] = {" prog" , " -p" , " val2" };
1216
+ EXPECT_TRUE (
1217
+ cl::ParseCommandLineOptions (3 , args2, StringRef (), &llvm::nulls ()));
1218
+ EXPECT_STREQ (" val2" , OptP.c_str ());
1219
+ OptP.clear ();
1220
+ cl::ResetAllOptionOccurrences ();
1221
+
1222
+ // The "-opt=value" form should work, too.
1223
+ const char *args3[] = {" prog" , " -p=val3" };
1224
+ EXPECT_TRUE (
1225
+ cl::ParseCommandLineOptions (2 , args3, StringRef (), &llvm::nulls ()));
1226
+ EXPECT_STREQ (" val3" , OptP.c_str ());
1227
+ OptP.clear ();
1228
+ cl::ResetAllOptionOccurrences ();
1229
+
1230
+ // All three previous cases should work the same way if an option with both
1231
+ // cl::Prefix and cl::Grouping modifiers is used at the end of a group.
1232
+ const char *args4[] = {" prog" , " -fpval4" };
1233
+ EXPECT_TRUE (
1234
+ cl::ParseCommandLineOptions (2 , args4, StringRef (), &llvm::nulls ()));
1235
+ EXPECT_TRUE (OptF);
1236
+ EXPECT_STREQ (" val4" , OptP.c_str ());
1237
+ OptP.clear ();
1238
+ cl::ResetAllOptionOccurrences ();
1239
+
1240
+ const char *args5[] = {" prog" , " -fp" , " val5" };
1241
+ EXPECT_TRUE (
1242
+ cl::ParseCommandLineOptions (3 , args5, StringRef (), &llvm::nulls ()));
1243
+ EXPECT_TRUE (OptF);
1244
+ EXPECT_STREQ (" val5" , OptP.c_str ());
1245
+ OptP.clear ();
1246
+ cl::ResetAllOptionOccurrences ();
1247
+
1248
+ const char *args6[] = {" prog" , " -fp=val6" };
1249
+ EXPECT_TRUE (
1250
+ cl::ParseCommandLineOptions (2 , args6, StringRef (), &llvm::nulls ()));
1251
+ EXPECT_TRUE (OptF);
1252
+ EXPECT_STREQ (" val6" , OptP.c_str ());
1253
+ OptP.clear ();
1254
+ cl::ResetAllOptionOccurrences ();
1255
+
1256
+ // Should assign a value even if the part after a cl::Prefix option is equal
1257
+ // to the name of another option.
1258
+ const char *args7[] = {" prog" , " -fpb" };
1259
+ EXPECT_TRUE (
1260
+ cl::ParseCommandLineOptions (2 , args7, StringRef (), &llvm::nulls ()));
1261
+ EXPECT_TRUE (OptF);
1262
+ EXPECT_STREQ (" b" , OptP.c_str ());
1263
+ EXPECT_FALSE (OptB);
1264
+ OptP.clear ();
1265
+ cl::ResetAllOptionOccurrences ();
1266
+
1267
+ // Should be possible to use a cl::AlwaysPrefix option without grouping.
1268
+ const char *args8[] = {" prog" , " -aval8" };
1269
+ EXPECT_TRUE (
1270
+ cl::ParseCommandLineOptions (2 , args8, StringRef (), &llvm::nulls ()));
1271
+ EXPECT_STREQ (" val8" , OptA.c_str ());
1272
+ OptA.clear ();
1273
+ cl::ResetAllOptionOccurrences ();
1274
+
1275
+ // Should not be possible to pass a value in a separate argument.
1276
+ const char *args9[] = {" prog" , " -a" , " val9" };
1277
+ EXPECT_FALSE (
1278
+ cl::ParseCommandLineOptions (3 , args9, StringRef (), &llvm::nulls ()));
1279
+ cl::ResetAllOptionOccurrences ();
1280
+
1281
+ // With the "-opt=value" form, the "=" symbol should be preserved.
1282
+ const char *args10[] = {" prog" , " -a=val10" };
1283
+ EXPECT_TRUE (
1284
+ cl::ParseCommandLineOptions (2 , args10, StringRef (), &llvm::nulls ()));
1285
+ EXPECT_STREQ (" =val10" , OptA.c_str ());
1286
+ OptA.clear ();
1287
+ cl::ResetAllOptionOccurrences ();
1288
+
1289
+ // All three previous cases should work the same way if an option with both
1290
+ // cl::AlwaysPrefix and cl::Grouping modifiers is used at the end of a group.
1291
+ const char *args11[] = {" prog" , " -faval11" };
1292
+ EXPECT_TRUE (
1293
+ cl::ParseCommandLineOptions (2 , args11, StringRef (), &llvm::nulls ()));
1294
+ EXPECT_TRUE (OptF);
1295
+ EXPECT_STREQ (" val11" , OptA.c_str ());
1296
+ OptA.clear ();
1297
+ cl::ResetAllOptionOccurrences ();
1298
+
1299
+ const char *args12[] = {" prog" , " -fa" , " val12" };
1300
+ EXPECT_FALSE (
1301
+ cl::ParseCommandLineOptions (3 , args12, StringRef (), &llvm::nulls ()));
1302
+ cl::ResetAllOptionOccurrences ();
1303
+
1304
+ const char *args13[] = {" prog" , " -fa=val13" };
1305
+ EXPECT_TRUE (
1306
+ cl::ParseCommandLineOptions (2 , args13, StringRef (), &llvm::nulls ()));
1307
+ EXPECT_TRUE (OptF);
1308
+ EXPECT_STREQ (" =val13" , OptA.c_str ());
1309
+ OptA.clear ();
1310
+ cl::ResetAllOptionOccurrences ();
1311
+
1312
+ // Should assign a value even if the part after a cl::AlwaysPrefix option
1313
+ // is equal to the name of another option.
1314
+ const char *args14[] = {" prog" , " -fab" };
1315
+ EXPECT_TRUE (
1316
+ cl::ParseCommandLineOptions (2 , args14, StringRef (), &llvm::nulls ()));
1317
+ EXPECT_TRUE (OptF);
1318
+ EXPECT_STREQ (" b" , OptA.c_str ());
1319
+ EXPECT_FALSE (OptB);
1320
+ OptA.clear ();
1321
+ cl::ResetAllOptionOccurrences ();
1151
1322
}
1152
1323
1153
1324
} // anonymous namespace
0 commit comments