aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/CommandLineTest.cpp
diff options
context:
space:
mode:
authorDon Hinton <donh@apple.com>2019-12-06 14:40:21 -0800
committerDon Hinton <donh@apple.com>2019-12-06 15:16:45 -0800
commit6555995a6d4545ff59dcf3388f9acfce3b6129a5 (patch)
treeced9063ae798be396c497a476da6927a7bd1a92c /llvm/unittests/Support/CommandLineTest.cpp
parent0a717d5b5d31fc2d5bc98ca695031fb09e65beb0 (diff)
downloadllvm-6555995a6d4545ff59dcf3388f9acfce3b6129a5.zip
llvm-6555995a6d4545ff59dcf3388f9acfce3b6129a5.tar.gz
llvm-6555995a6d4545ff59dcf3388f9acfce3b6129a5.tar.bz2
[CommandLine] Add callbacks to Options
Summary: Add a new cl::callback attribute to Option. This attribute specifies a callback function that is called when an option is seen, and can be used to set other options, as in option A implies option B. If the option is a `cl::list`, and `cl::CommaSeparated` is also specified, the callback will fire once for each value. This could be used to validate combinations or selectively set other options. Reviewers: beanz, thomasfinch, MaskRay, thopre, serge-sans-paille Reviewed By: beanz Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70620
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp66
1 files changed, 64 insertions, 2 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 694fec2..702aa52 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -71,7 +71,7 @@ public:
~StackOption() override { this->removeArgument(); }
template <class DT> StackOption<T> &operator=(const DT &V) {
- this->setValue(V);
+ Base::operator=(V);
return *this;
}
};
@@ -1722,4 +1722,66 @@ TEST(CommandLineTest, OptionErrorMessageSuggest) {
cl::ResetAllOptionOccurrences();
}
-} // anonymous namespace
+
+TEST(CommandLineTest, Callback) {
+ cl::ResetCommandLineParser();
+
+ StackOption<bool> OptA("a", cl::desc("option a"));
+ StackOption<bool> OptB(
+ "b", cl::desc("option b -- This option turns on option a"),
+ cl::callback([&](const bool &) { OptA = true; }));
+ StackOption<bool> OptC(
+ "c", cl::desc("option c -- This option turns on options a and b"),
+ cl::callback([&](const bool &) { OptB = true; }));
+ StackOption<std::string, cl::list<std::string>> List(
+ "list",
+ cl::desc("option list -- This option turns on options a, b, and c when "
+ "'foo' is included in list"),
+ cl::CommaSeparated,
+ cl::callback([&](const std::string &Str) {
+ if (Str == "foo")
+ OptC = true;
+ }));
+
+ const char *args1[] = {"prog", "-a"};
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args1));
+ EXPECT_TRUE(OptA);
+ EXPECT_FALSE(OptB);
+ EXPECT_FALSE(OptC);
+ EXPECT_TRUE(List.size() == 0);
+ cl::ResetAllOptionOccurrences();
+
+ const char *args2[] = {"prog", "-b"};
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args2));
+ EXPECT_TRUE(OptA);
+ EXPECT_TRUE(OptB);
+ EXPECT_FALSE(OptC);
+ EXPECT_TRUE(List.size() == 0);
+ cl::ResetAllOptionOccurrences();
+
+ const char *args3[] = {"prog", "-c"};
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args3));
+ EXPECT_TRUE(OptA);
+ EXPECT_TRUE(OptB);
+ EXPECT_TRUE(OptC);
+ EXPECT_TRUE(List.size() == 0);
+ cl::ResetAllOptionOccurrences();
+
+ const char *args4[] = {"prog", "--list=foo,bar"};
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args4));
+ EXPECT_TRUE(OptA);
+ EXPECT_TRUE(OptB);
+ EXPECT_TRUE(OptC);
+ EXPECT_TRUE(List.size() == 2);
+ cl::ResetAllOptionOccurrences();
+
+ const char *args5[] = {"prog", "--list=bar"};
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args5));
+ EXPECT_FALSE(OptA);
+ EXPECT_FALSE(OptB);
+ EXPECT_FALSE(OptC);
+ EXPECT_TRUE(List.size() == 1);
+
+ cl::ResetAllOptionOccurrences();
+}
+} // anonymous namespace