[clang] [clang-tools-extra] [lld] [llvm] [llvm] Add subcommand support for OptTable (PR #155026)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 26 11:22:41 PDT 2025
================
@@ -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;
+ }
----------------
PiJoules wrote:
```
return std::find(SubCommandIDs.begin(), SubCommandIDs.end(), ActiveSubCommandID) != SubCommandIDs.end();
```
might be cleaner
https://github.com/llvm/llvm-project/pull/155026
More information about the llvm-commits
mailing list