================
@@ -742,28 +747,76 @@ void OptTable::printHelp(raw_ostream &OS, const char 
*Usage, const char *Title,
 }
 
 void OptTable::internalPrintHelp(
-    raw_ostream &OS, const char *Usage, const char *Title, bool ShowHidden,
-    bool ShowAllAliases, std::function<bool(const Info &)> ExcludeOption,
+    raw_ostream &OS, const char *Usage, const char *Title, StringRef 
Subcommand,
+    bool ShowHidden, bool ShowAllAliases,
+    std::function<bool(const Info &)> ExcludeOption,
     Visibility VisibilityMask) const {
   OS << "OVERVIEW: " << Title << "\n\n";
-  OS << "USAGE: " << Usage << "\n\n";
 
   // Render help text into a map of group-name to a list of (option, help)
   // pairs.
   std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
 
+  const SubCommand *ActiveSubCommand =
+      std::find_if(SubCommands.begin(), SubCommands.end(),
+                   [&](const auto &C) { return Subcommand == C.Name; });
+  if (!Subcommand.empty()) {
+    assert(ActiveSubCommand != nullptr && "Not a valid registered 
subcommand.");
+    OS << ActiveSubCommand->HelpText << "\n\n";
+    if (!StringRef(ActiveSubCommand->Usage).empty())
+      OS << "USAGE: " << ActiveSubCommand->Usage << "\n\n";
+  } else {
+    OS << "USAGE: " << Usage << "\n\n";
+    if (SubCommands.size() > 1) {
+      OS << "SUBCOMMANDS:\n\n";
+      for (const auto &C : SubCommands)
+        OS << C.Name << " - " << C.HelpText << "\n";
+      OS << "\n";
+    }
+  }
+
+  auto DoesOptionBelongToSubcommand = [&](const Info &CandidateInfo) {
+    ArrayRef<unsigned> SubCommandIDs =
+        CandidateInfo.getCommandIDs(SubCommandIDsTable);
+
+    // Options with no subcommands are global.
+    if (SubCommandIDs.empty()) {
+      // if Subcommand is not empty then don't print global options.
+      return Subcommand.empty();
+    }
+
+    // If we are not in a subcommand, don't show subcommand-specific options.
+    if (Subcommand.empty())
+      return false;
+
+    // The subcommand ID is its index in the SubCommands table.
+    unsigned ActiveSubCommandID = ActiveSubCommand - &SubCommands[0];
+
+    // Check if the option belongs to the active subcommand.
+    for (unsigned ID : SubCommandIDs) {
+      if (ID == ActiveSubCommandID) {
+        return true;
+      }
+    }
+    return false;
+  };
+
   for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
     // FIXME: Split out option groups.
     if (getOptionKind(Id) == Option::GroupClass)
       continue;
 
     const Info &CandidateInfo = getInfo(Id);
+
----------------
PiJoules wrote:

remove newline

https://github.com/llvm/llvm-project/pull/155026
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to