[PATCH] D24489: [Support][CommandLine] Add cl::getRegisteredSubcommands()

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 22:29:09 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL283296: [Support][CommandLine] Add cl::getRegisteredSubcommands() (authored by dberris).

Changed prior to commit:
  https://reviews.llvm.org/D24489?vs=71106&id=73592#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D24489

Files:
  llvm/trunk/include/llvm/Support/CommandLine.h
  llvm/trunk/lib/Support/CommandLine.cpp
  llvm/trunk/unittests/Support/CommandLineTest.cpp


Index: llvm/trunk/lib/Support/CommandLine.cpp
===================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp
+++ llvm/trunk/lib/Support/CommandLine.cpp
@@ -309,6 +309,12 @@
     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 @@
   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 &&
Index: llvm/trunk/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/CommandLineTest.cpp
+++ llvm/trunk/unittests/Support/CommandLineTest.cpp
@@ -476,4 +476,33 @@
   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
Index: llvm/trunk/include/llvm/Support/CommandLine.h
===================================================================
--- llvm/trunk/include/llvm/Support/CommandLine.h
+++ llvm/trunk/include/llvm/Support/CommandLine.h
@@ -1736,6 +1736,28 @@
 /// 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.
 //


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24489.73592.patch
Type: text/x-patch
Size: 3491 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161005/42c465d7/attachment.bin>


More information about the llvm-commits mailing list