blob: 72c9e32a60dc9c7d97b49769fbd1461efaf3190b [file] [log] [blame]
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +00001//===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Reid Klecknera73c7782013-07-18 16:52:05 +000010#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000011#include "llvm/Config/config.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000012#include "llvm/Support/CommandLine.h"
Rafael Espindola454adf62015-06-13 12:49:52 +000013#include "llvm/Support/StringSaver.h"
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000014#include "gtest/gtest.h"
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000015#include <stdlib.h>
Chandler Carruth130cec22012-12-04 10:23:08 +000016#include <string>
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000017
18using namespace llvm;
19
20namespace {
21
22class TempEnvVar {
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023 public:
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000024 TempEnvVar(const char *name, const char *value)
25 : name(name) {
26 const char *old_value = getenv(name);
Craig Topper66f09ad2014-06-08 22:29:17 +000027 EXPECT_EQ(nullptr, old_value) << old_value;
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000028#if HAVE_SETENV
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000029 setenv(name, value, true);
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000030#else
31# define SKIP_ENVIRONMENT_TESTS
32#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000033 }
34
35 ~TempEnvVar() {
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000036#if HAVE_SETENV
37 // Assume setenv and unsetenv come together.
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000038 unsetenv(name);
Reid Kleckner542a4542015-02-26 21:08:21 +000039#else
40 (void)name; // Suppress -Wunused-private-field.
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000041#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000042 }
43
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000044 private:
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000045 const char *const name;
46};
47
Jordan Rosec25b0c72014-01-29 18:54:17 +000048template <typename T>
49class StackOption : public cl::opt<T> {
Jordan Rosed2ad22a2014-01-29 19:14:23 +000050 typedef cl::opt<T> Base;
Jordan Rosec25b0c72014-01-29 18:54:17 +000051public:
52 // One option...
53 template<class M0t>
54 explicit StackOption(const M0t &M0) : Base(M0) {}
55
56 // Two options...
57 template<class M0t, class M1t>
58 StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
59
60 // Three options...
61 template<class M0t, class M1t, class M2t>
62 StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
63
64 // Four options...
65 template<class M0t, class M1t, class M2t, class M3t>
66 StackOption(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
67 : Base(M0, M1, M2, M3) {}
68
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000069 ~StackOption() override { this->removeArgument(); }
Zachary Turner07670b32016-06-29 21:48:26 +000070
71 template <class DT> StackOption<T> &operator=(const DT &V) {
72 this->setValue(V);
73 return *this;
74 }
75};
76
77class StackSubCommand : public cl::SubCommand {
78public:
Mehdi Aminie11b7452016-10-01 03:43:20 +000079 StackSubCommand(StringRef Name,
80 StringRef Description = StringRef())
Zachary Turner07670b32016-06-29 21:48:26 +000081 : SubCommand(Name, Description) {}
82
83 StackSubCommand() : SubCommand() {}
84
85 ~StackSubCommand() { unregisterSubCommand(); }
Jordan Rosec25b0c72014-01-29 18:54:17 +000086};
87
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000088
Andrew Trick7cb710d2013-05-06 21:56:35 +000089cl::OptionCategory TestCategory("Test Options", "Description");
Andrew Trick7cb710d2013-05-06 21:56:35 +000090TEST(CommandLineTest, ModifyExisitingOption) {
Chris Bienemand1d94302015-01-28 19:00:25 +000091 StackOption<int> TestOption("test-option", cl::desc("old description"));
92
Andrew Trick7cb710d2013-05-06 21:56:35 +000093 const char Description[] = "New description";
94 const char ArgString[] = "new-test-option";
95 const char ValueString[] = "Integer";
96
Zachary Turner07670b32016-06-29 21:48:26 +000097 StringMap<cl::Option *> &Map =
98 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
Andrew Trick7cb710d2013-05-06 21:56:35 +000099
100 ASSERT_TRUE(Map.count("test-option") == 1) <<
101 "Could not find option in map.";
102
103 cl::Option *Retrieved = Map["test-option"];
104 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
105
106 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
107 "Incorrect default option category.";
108
109 Retrieved->setCategory(TestCategory);
110 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
111 "Failed to modify option's option category.";
112
113 Retrieved->setDescription(Description);
David Blaikieff43d692015-11-17 19:00:52 +0000114 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
115 << "Changing option description failed.";
Andrew Trick7cb710d2013-05-06 21:56:35 +0000116
117 Retrieved->setArgStr(ArgString);
David Blaikieff43d692015-11-17 19:00:52 +0000118 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
119 << "Failed to modify option's Argument string.";
Andrew Trick7cb710d2013-05-06 21:56:35 +0000120
121 Retrieved->setValueStr(ValueString);
David Blaikieff43d692015-11-17 19:00:52 +0000122 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
123 << "Failed to modify option's Value string.";
Andrew Trick7cb710d2013-05-06 21:56:35 +0000124
125 Retrieved->setHiddenFlag(cl::Hidden);
126 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
127 "Failed to modify option's hidden flag.";
128}
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000129#ifndef SKIP_ENVIRONMENT_TESTS
130
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +0000131const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
132
133cl::opt<std::string> EnvironmentTestOption("env-test-opt");
134TEST(CommandLineTest, ParseEnvironment) {
135 TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
136 EXPECT_EQ("", EnvironmentTestOption);
137 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
138 EXPECT_EQ("hello", EnvironmentTestOption);
139}
140
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000141// This test used to make valgrind complain
142// ("Conditional jump or move depends on uninitialised value(s)")
Andrew Trick7cb710d2013-05-06 21:56:35 +0000143//
144// Warning: Do not run any tests after this one that try to gain access to
145// registered command line options because this will likely result in a
146// SEGFAULT. This can occur because the cl::opt in the test below is declared
147// on the stack which will be destroyed after the test completes but the
148// command line system will still hold a pointer to a deallocated cl::Option.
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000149TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
150 // Put cl::opt on stack to check for proper initialization of fields.
Jordan Rosec25b0c72014-01-29 18:54:17 +0000151 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000152 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
153 EXPECT_EQ("", EnvironmentTestOptionLocal);
154 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
155 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
156}
157
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000158#endif // SKIP_ENVIRONMENT_TESTS
159
Andrew Trick0537a982013-05-06 21:56:23 +0000160TEST(CommandLineTest, UseOptionCategory) {
Jordan Rosec25b0c72014-01-29 18:54:17 +0000161 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
Andrew Trick0537a982013-05-06 21:56:23 +0000162
Andrew Trick7cb710d2013-05-06 21:56:35 +0000163 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
Andrew Trick0537a982013-05-06 21:56:23 +0000164 "Category.";
165}
166
Rafael Espindola454adf62015-06-13 12:49:52 +0000167typedef void ParserFunction(StringRef Source, StringSaver &Saver,
Reid Klecknere3f146d2014-08-22 19:29:17 +0000168 SmallVectorImpl<const char *> &NewArgv,
169 bool MarkEOLs);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000170
Mehdi Aminie11b7452016-10-01 03:43:20 +0000171void testCommandLineTokenizer(ParserFunction *parse, StringRef Input,
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000172 const char *const Output[], size_t OutputSize) {
173 SmallVector<const char *, 0> Actual;
Rafael Espindola454adf62015-06-13 12:49:52 +0000174 BumpPtrAllocator A;
Rafael Espindolab82455d2015-08-13 01:07:02 +0000175 StringSaver Saver(A);
Reid Klecknere3f146d2014-08-22 19:29:17 +0000176 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000177 EXPECT_EQ(OutputSize, Actual.size());
178 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
179 if (I < OutputSize)
180 EXPECT_STREQ(Output[I], Actual[I]);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000181 }
182}
183
Reid Klecknera73c7782013-07-18 16:52:05 +0000184TEST(CommandLineTest, TokenizeGNUCommandLine) {
Mehdi Aminie11b7452016-10-01 03:43:20 +0000185 const char Input[] =
Nico Weberfa7f4892016-04-26 13:53:56 +0000186 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
187 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
188 const char *const Output[] = {
189 "foo bar", "foo bar", "foo bar", "foo\\bar",
190 "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000191 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
192 array_lengthof(Output));
193}
194
195TEST(CommandLineTest, TokenizeWindowsCommandLine) {
Mehdi Aminie11b7452016-10-01 03:43:20 +0000196 const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000197 "\"st \\\"u\" \\v";
198 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
199 "lmn", "o", "pqr", "st \"u", "\\v" };
200 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
201 array_lengthof(Output));
Reid Klecknera73c7782013-07-18 16:52:05 +0000202}
203
Jordan Rosec25b0c72014-01-29 18:54:17 +0000204TEST(CommandLineTest, AliasesWithArguments) {
205 static const size_t ARGC = 3;
206 const char *const Inputs[][ARGC] = {
207 { "-tool", "-actual=x", "-extra" },
208 { "-tool", "-actual", "x" },
209 { "-tool", "-alias=x", "-extra" },
210 { "-tool", "-alias", "x" }
211 };
212
213 for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
214 StackOption<std::string> Actual("actual");
215 StackOption<bool> Extra("extra");
216 StackOption<std::string> Input(cl::Positional);
217
218 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
219
220 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
221 EXPECT_EQ("x", Actual);
222 EXPECT_EQ(0, Input.getNumOccurrences());
223
224 Alias.removeArgument();
225 }
226}
227
Justin Bogner759645e2014-07-14 20:53:57 +0000228void testAliasRequired(int argc, const char *const *argv) {
229 StackOption<std::string> Option("option", cl::Required);
230 cl::alias Alias("o", llvm::cl::aliasopt(Option));
231
232 cl::ParseCommandLineOptions(argc, argv);
233 EXPECT_EQ("x", Option);
234 EXPECT_EQ(1, Option.getNumOccurrences());
235
236 Alias.removeArgument();
237}
238
239TEST(CommandLineTest, AliasRequired) {
240 const char *opts1[] = { "-tool", "-option=x" };
241 const char *opts2[] = { "-tool", "-o", "x" };
242 testAliasRequired(array_lengthof(opts1), opts1);
243 testAliasRequired(array_lengthof(opts2), opts2);
244}
245
Chris Bieneman9e13af72015-01-21 22:45:52 +0000246TEST(CommandLineTest, HideUnrelatedOptions) {
Chris Bieneman68169362015-01-27 22:21:06 +0000247 StackOption<int> TestOption1("hide-option-1");
248 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
Chris Bieneman9e13af72015-01-21 22:45:52 +0000249
250 cl::HideUnrelatedOptions(TestCategory);
251
252 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
253 << "Failed to hide extra option.";
254 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
255 << "Hid extra option that should be visable.";
Chris Bieneman831fc5e2015-01-26 16:56:00 +0000256
Zachary Turner07670b32016-06-29 21:48:26 +0000257 StringMap<cl::Option *> &Map =
258 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
Chris Bieneman68169362015-01-27 22:21:06 +0000259 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
260 << "Hid default option that should be visable.";
261}
262
263cl::OptionCategory TestCategory2("Test Options set 2", "Description");
264
265TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
266 StackOption<int> TestOption1("multi-hide-option-1");
267 StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
268 StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
269
270 const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
271 &TestCategory2};
272
273 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
274
275 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
276 << "Failed to hide extra option.";
277 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
278 << "Hid extra option that should be visable.";
279 ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
280 << "Hid extra option that should be visable.";
281
Zachary Turner07670b32016-06-29 21:48:26 +0000282 StringMap<cl::Option *> &Map =
283 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
Chris Bieneman831fc5e2015-01-26 16:56:00 +0000284 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
285 << "Hid default option that should be visable.";
Chris Bieneman9e13af72015-01-21 22:45:52 +0000286}
Justin Bogner759645e2014-07-14 20:53:57 +0000287
Zachary Turner07670b32016-06-29 21:48:26 +0000288TEST(CommandLineTest, SetValueInSubcategories) {
289 cl::ResetCommandLineParser();
290
291 StackSubCommand SC1("sc1", "First subcommand");
292 StackSubCommand SC2("sc2", "Second subcommand");
293
294 StackOption<bool> TopLevelOpt("top-level", cl::init(false));
295 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
296 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
297
298 EXPECT_FALSE(TopLevelOpt);
299 EXPECT_FALSE(SC1Opt);
300 EXPECT_FALSE(SC2Opt);
301 const char *args[] = {"prog", "-top-level"};
Mehdi Aminie11b7452016-10-01 03:43:20 +0000302 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000303 EXPECT_TRUE(TopLevelOpt);
304 EXPECT_FALSE(SC1Opt);
305 EXPECT_FALSE(SC2Opt);
306
307 TopLevelOpt = false;
308
309 cl::ResetAllOptionOccurrences();
310 EXPECT_FALSE(TopLevelOpt);
311 EXPECT_FALSE(SC1Opt);
312 EXPECT_FALSE(SC2Opt);
313 const char *args2[] = {"prog", "sc1", "-sc1"};
Mehdi Aminie11b7452016-10-01 03:43:20 +0000314 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000315 EXPECT_FALSE(TopLevelOpt);
316 EXPECT_TRUE(SC1Opt);
317 EXPECT_FALSE(SC2Opt);
318
319 SC1Opt = false;
320
321 cl::ResetAllOptionOccurrences();
322 EXPECT_FALSE(TopLevelOpt);
323 EXPECT_FALSE(SC1Opt);
324 EXPECT_FALSE(SC2Opt);
325 const char *args3[] = {"prog", "sc2", "-sc2"};
Mehdi Aminie11b7452016-10-01 03:43:20 +0000326 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000327 EXPECT_FALSE(TopLevelOpt);
328 EXPECT_FALSE(SC1Opt);
329 EXPECT_TRUE(SC2Opt);
330}
331
332TEST(CommandLineTest, LookupFailsInWrongSubCommand) {
333 cl::ResetCommandLineParser();
334
335 StackSubCommand SC1("sc1", "First subcommand");
336 StackSubCommand SC2("sc2", "Second subcommand");
337
338 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
339 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
340
341 const char *args[] = {"prog", "sc1", "-sc2"};
Mehdi Aminie11b7452016-10-01 03:43:20 +0000342 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000343}
344
345TEST(CommandLineTest, AddToAllSubCommands) {
346 cl::ResetCommandLineParser();
347
348 StackSubCommand SC1("sc1", "First subcommand");
349 StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands),
350 cl::init(false));
351 StackSubCommand SC2("sc2", "Second subcommand");
352
353 const char *args[] = {"prog", "-everywhere"};
354 const char *args2[] = {"prog", "sc1", "-everywhere"};
355 const char *args3[] = {"prog", "sc2", "-everywhere"};
356
357 EXPECT_FALSE(AllOpt);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000358 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000359 EXPECT_TRUE(AllOpt);
360
361 AllOpt = false;
362
363 cl::ResetAllOptionOccurrences();
364 EXPECT_FALSE(AllOpt);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000365 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000366 EXPECT_TRUE(AllOpt);
367
368 AllOpt = false;
369
370 cl::ResetAllOptionOccurrences();
371 EXPECT_FALSE(AllOpt);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000372 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000373 EXPECT_TRUE(AllOpt);
374}
375
376TEST(CommandLineTest, ReparseCommandLineOptions) {
377 cl::ResetCommandLineParser();
378
379 StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand),
380 cl::init(false));
381
382 const char *args[] = {"prog", "-top-level"};
383
384 EXPECT_FALSE(TopLevelOpt);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000385 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000386 EXPECT_TRUE(TopLevelOpt);
387
388 TopLevelOpt = false;
389
390 cl::ResetAllOptionOccurrences();
391 EXPECT_FALSE(TopLevelOpt);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000392 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000393 EXPECT_TRUE(TopLevelOpt);
394}
395
396TEST(CommandLineTest, RemoveFromRegularSubCommand) {
397 cl::ResetCommandLineParser();
398
399 StackSubCommand SC("sc", "Subcommand");
400 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false));
401 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false));
402
403 const char *args[] = {"prog", "sc", "-remove-option"};
404
405 EXPECT_FALSE(RemoveOption);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000406 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000407 EXPECT_TRUE(RemoveOption);
408
409 RemoveOption.removeArgument();
410
411 cl::ResetAllOptionOccurrences();
Mehdi Aminie11b7452016-10-01 03:43:20 +0000412 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000413}
414
415TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
416 cl::ResetCommandLineParser();
417
418 StackOption<bool> TopLevelRemove(
419 "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
420 StackOption<bool> TopLevelKeep(
421 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
422
423 const char *args[] = {"prog", "-top-level-remove"};
424
425 EXPECT_FALSE(TopLevelRemove);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000426 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000427 EXPECT_TRUE(TopLevelRemove);
428
429 TopLevelRemove.removeArgument();
430
431 cl::ResetAllOptionOccurrences();
Mehdi Aminie11b7452016-10-01 03:43:20 +0000432 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000433}
434
435TEST(CommandLineTest, RemoveFromAllSubCommands) {
436 cl::ResetCommandLineParser();
437
438 StackSubCommand SC1("sc1", "First Subcommand");
439 StackSubCommand SC2("sc2", "Second Subcommand");
440 StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands),
441 cl::init(false));
442 StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands),
443 cl::init(false));
444
445 const char *args0[] = {"prog", "-remove-option"};
446 const char *args1[] = {"prog", "sc1", "-remove-option"};
447 const char *args2[] = {"prog", "sc2", "-remove-option"};
448
449 // It should work for all subcommands including the top-level.
450 EXPECT_FALSE(RemoveOption);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000451 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000452 EXPECT_TRUE(RemoveOption);
453
454 RemoveOption = false;
455
456 cl::ResetAllOptionOccurrences();
457 EXPECT_FALSE(RemoveOption);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000458 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000459 EXPECT_TRUE(RemoveOption);
460
461 RemoveOption = false;
462
463 cl::ResetAllOptionOccurrences();
464 EXPECT_FALSE(RemoveOption);
Mehdi Aminie11b7452016-10-01 03:43:20 +0000465 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000466 EXPECT_TRUE(RemoveOption);
467
468 RemoveOption.removeArgument();
469
470 // It should not work for any subcommands including the top-level.
471 cl::ResetAllOptionOccurrences();
Mehdi Aminie11b7452016-10-01 03:43:20 +0000472 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000473 cl::ResetAllOptionOccurrences();
Mehdi Aminie11b7452016-10-01 03:43:20 +0000474 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000475 cl::ResetAllOptionOccurrences();
Mehdi Aminie11b7452016-10-01 03:43:20 +0000476 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
Zachary Turner07670b32016-06-29 21:48:26 +0000477}
478
Dean Michael Berris27358cf2016-10-05 05:20:08 +0000479TEST(CommandLineTest, GetRegisteredSubcommands) {
480 cl::ResetCommandLineParser();
481
482 StackSubCommand SC1("sc1", "First Subcommand");
483 StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
484 StackSubCommand SC2("sc2", "Second subcommand");
485 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
486
487 const char *args0[] = {"prog", "sc1"};
488 const char *args1[] = {"prog", "sc2"};
489
490 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
491 EXPECT_FALSE(Opt1);
492 EXPECT_FALSE(Opt2);
493 for (auto *S : cl::getRegisteredSubcommands()) {
494 if (*S)
495 EXPECT_EQ("sc1", S->getName());
496 }
497
498 cl::ResetAllOptionOccurrences();
499 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args1, StringRef(), true));
500 EXPECT_FALSE(Opt1);
501 EXPECT_FALSE(Opt2);
502 for (auto *S : cl::getRegisteredSubcommands()) {
503 if (*S)
504 EXPECT_EQ("sc2", S->getName());
505 }
506}
507
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000508} // anonymous namespace