llvm.org GIT mirror llvm / 9f34fd5
Option parser: class for consuming a joined arg in addition to all remaining args git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266394 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 3 years ago
6 changed file(s) with 73 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
4545 def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">;
4646 // An option which consumes all remaining arguments if there are any.
4747 def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">;
48 // An option which consumes an optional joined argument and any other remaining
49 // arguments.
50 def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">;
4851
4952 // Define the option flags.
5053
5050 JoinedClass,
5151 SeparateClass,
5252 RemainingArgsClass,
53 RemainingArgsJoinedClass,
5354 CommaJoinedClass,
5455 MultiArgClass,
5556 JoinedOrSeparateClass,
149150 case MultiArgClass:
150151 case JoinedOrSeparateClass:
151152 case RemainingArgsClass:
153 case RemainingArgsJoinedClass:
152154 return RenderSeparateStyle;
153155 }
154156 llvm_unreachable("Unexpected kind!");
314314 break;
315315
316316 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
317 case Option::RemainingArgsClass:
317 case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
318318 Name += ' ';
319319 // FALLTHROUGH
320320 case Option::JoinedClass: case Option::CommaJoinedClass:
5050 P(JoinedOrSeparateClass);
5151 P(JoinedAndSeparateClass);
5252 P(RemainingArgsClass);
53 P(RemainingArgsJoinedClass);
5354 #undef P
5455 }
5556
233234 A->getValues().push_back(Args.getArgString(Index++));
234235 return A;
235236 }
237 case RemainingArgsJoinedClass: {
238 Arg *A = new Arg(UnaliasedOption, Spelling, Index);
239 if (ArgSize != strlen(Args.getArgString(Index))) {
240 // An inexact match means there is a joined arg.
241 A->getValues().push_back(Args.getArgString(Index) + ArgSize);
242 }
243 Index++;
244 while (Index < Args.getNumInputArgStrings() &&
245 Args.getArgString(Index) != nullptr)
246 A->getValues().push_back(Args.getArgString(Index++));
247 return A;
248 }
249
236250 default:
237251 llvm_unreachable("Invalid option kind!");
238252 }
201201 EXPECT_EQ("foo", AL.getAllArgValues(OPT_Slurp)[2]);
202202 }
203203
204 TEST(Option, SlurpJoinedEmpty) {
205 TestOptTable T;
206 unsigned MAI, MAC;
207
208 const char *MyArgs[] = { "-A", "-slurpjoined" };
209 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
210 EXPECT_TRUE(AL.hasArg(OPT_A));
211 EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
212 EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 0U);
213 }
214
215 TEST(Option, SlurpJoinedOneJoined) {
216 TestOptTable T;
217 unsigned MAI, MAC;
218
219 const char *MyArgs[] = { "-A", "-slurpjoinedfoo" };
220 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
221 EXPECT_TRUE(AL.hasArg(OPT_A));
222 EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
223 EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 1U);
224 EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined)[0], "foo");
225 }
226
227 TEST(Option, SlurpJoinedAndSeparate) {
228 TestOptTable T;
229 unsigned MAI, MAC;
230
231 const char *MyArgs[] = { "-A", "-slurpjoinedfoo", "bar", "baz" };
232 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
233 EXPECT_TRUE(AL.hasArg(OPT_A));
234 EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
235 EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
236 EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
237 EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
238 EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
239 }
240
241 TEST(Option, SlurpJoinedButSeparate) {
242 TestOptTable T;
243 unsigned MAI, MAC;
244
245 const char *MyArgs[] = { "-A", "-slurpjoined", "foo", "bar", "baz" };
246 InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
247 EXPECT_TRUE(AL.hasArg(OPT_A));
248 EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
249 EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
250 EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
251 EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
252 EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
253 }
254
204255 TEST(Option, FlagAliasToJoined) {
205256 TestOptTable T;
206257 unsigned MAI, MAC;
2525 def K : Flag<["-"], "K">, Alias;
2626
2727 def Slurp : Option<["-"], "slurp", KIND_REMAINING_ARGS>;
28
29 def SlurpJoined : Option<["-"], "slurpjoined", KIND_REMAINING_ARGS_JOINED>;