summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2016-10-05 05:20:08 +0000
committerDean Michael Berris <dberris@google.com>2016-10-05 05:20:08 +0000
commit27358cff88f929cee961d96e03706083d12ae7c7 (patch)
tree0374253222ebe9e0f0a493f25477d75451459b1c
parent3fce2fdf4c660df108cf506ca7d308c795842243 (diff)
downloadbcm5719-llvm-27358cff88f929cee961d96e03706083d12ae7c7.tar.gz
bcm5719-llvm-27358cff88f929cee961d96e03706083d12ae7c7.zip
[Support][CommandLine] Add cl::getRegisteredSubcommands()
This should allow users of the library to get a range to iterate through all the subcommands that are registered to the global parser. This allows users to define subcommands in libraries that self-register to have dispatch done at a different stage (like main). It allows for writing code like the following: for (auto *S : cl::getRegisteredSubcommands()) { if (*S) { // Dispatch on S->getName(). } } This change also contains tests that show this usage pattern. Reviewers: zturner, dblaikie, echristo Subscribers: llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D24489 llvm-svn: 283296
-rw-r--r--llvm/include/llvm/Support/CommandLine.h22
-rw-r--r--llvm/lib/Support/CommandLine.cpp11
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp29
3 files changed, 62 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index f2e8b281716..c46a99cb6bd 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -1736,6 +1736,28 @@ void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
/// than just handing around a global list.
StringMap<Option *> &getRegisteredOptions(SubCommand &Sub = *TopLevelSubCommand);
+/// \brief Use this to get all registered SubCommands from the provided parser.
+///
+/// \return A range of all SubCommand pointers registered with the parser.
+///
+/// Typical usage:
+/// \code
+/// main(int argc, char* argv[]) {
+/// llvm::cl::ParseCommandLineOptions(argc, argv);
+/// for (auto* S : llvm::cl::getRegisteredSubcommands()) {
+/// if (*S) {
+/// std::cout << "Executing subcommand: " << S->getName() << std::endl;
+/// // Execute some function based on the name...
+/// }
+/// }
+/// }
+/// \endcode
+///
+/// This interface is useful for defining subcommands in libraries and
+/// the dispatch from a single point (like in the main function).
+iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
+getRegisteredSubcommands();
+
//===----------------------------------------------------------------------===//
// Standalone command line processing utilities.
//
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index c5ca2149de3..76bf45e8f51 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -309,6 +309,12 @@ public:
RegisteredSubCommands.erase(sub);
}
+ iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
+ getRegisteredSubcommands() {
+ return make_range(RegisteredSubCommands.begin(),
+ RegisteredSubCommands.end());
+ }
+
void reset() {
ActiveSubCommand = nullptr;
ProgramName.clear();
@@ -2107,6 +2113,11 @@ StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) {
return Sub.OptionsMap;
}
+iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
+cl::getRegisteredSubcommands() {
+ return GlobalParser->getRegisteredSubcommands();
+}
+
void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) {
for (auto &I : Sub.OptionsMap) {
if (I.second->Category != &Category &&
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index e227586ba3e..72c9e32a60d 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -476,4 +476,33 @@ TEST(CommandLineTest, RemoveFromAllSubCommands) {
EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
}
+TEST(CommandLineTest, GetRegisteredSubcommands) {
+ cl::ResetCommandLineParser();
+
+ StackSubCommand SC1("sc1", "First Subcommand");
+ StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
+ StackSubCommand SC2("sc2", "Second subcommand");
+ StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
+
+ const char *args0[] = {"prog", "sc1"};
+ const char *args1[] = {"prog", "sc2"};
+
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
+ EXPECT_FALSE(Opt1);
+ EXPECT_FALSE(Opt2);
+ for (auto *S : cl::getRegisteredSubcommands()) {
+ if (*S)
+ EXPECT_EQ("sc1", S->getName());
+ }
+
+ cl::ResetAllOptionOccurrences();
+ EXPECT_TRUE(cl::ParseCommandLineOptions(2, args1, StringRef(), true));
+ EXPECT_FALSE(Opt1);
+ EXPECT_FALSE(Opt2);
+ for (auto *S : cl::getRegisteredSubcommands()) {
+ if (*S)
+ EXPECT_EQ("sc2", S->getName());
+ }
+}
+
} // anonymous namespace
OpenPOWER on IntegriCloud