Index: llvm/include/llvm/Support/CommandLine.h =================================================================== --- llvm/include/llvm/Support/CommandLine.h +++ llvm/include/llvm/Support/CommandLine.h @@ -1968,24 +1968,28 @@ SmallVectorImpl &Argv, bool MarkEOLs = false, bool RelativeNames = false); -/// Mark all options not part of this category as cl::ReallyHidden. +/// Mark all options not part of this category with the given cl::OptionHidden /// -/// \param Category the category of options to keep displaying +/// \param Category the category of options to keep displaying. +/// \param Opt the flag to apply to all other categories. /// /// Some tools (like clang-format) like to be able to hide all options that are /// not specific to the tool. This function allows a tool to specify a single /// option category to display in the -help output. void HideUnrelatedOptions(cl::OptionCategory &Category, + cl::OptionHidden Opt = cl::ReallyHidden, SubCommand &Sub = *TopLevelSubCommand); -/// Mark all options not part of the categories as cl::ReallyHidden. +/// Mark all options not part of the categories with the given cl::OptionHidden /// /// \param Categories the categories of options to keep displaying. +/// \param Opt the flag to apply to all other categories. /// /// Some tools (like clang-format) like to be able to hide all options that are /// not specific to the tool. This function allows a tool to specify a single /// option category to display in the -help output. void HideUnrelatedOptions(ArrayRef Categories, + OptionHidden Opt = cl::ReallyHidden, SubCommand &Sub = *TopLevelSubCommand); /// Reset all command line options to a state that looks as if they have Index: llvm/lib/Support/CommandLine.cpp =================================================================== --- llvm/lib/Support/CommandLine.cpp +++ llvm/lib/Support/CommandLine.cpp @@ -2422,22 +2422,23 @@ return GlobalParser->getRegisteredSubcommands(); } -void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) { +void cl::HideUnrelatedOptions(cl::OptionCategory &Category, OptionHidden Opt, + SubCommand &Sub) { for (auto &I : Sub.OptionsMap) { for (auto &Cat : I.second->Categories) { if (Cat != &Category && Cat != &GenericCategory) - I.second->setHiddenFlag(cl::ReallyHidden); + I.second->setHiddenFlag(Opt); } } } void cl::HideUnrelatedOptions(ArrayRef Categories, - SubCommand &Sub) { + OptionHidden Opt, SubCommand &Sub) { for (auto &I : Sub.OptionsMap) { for (auto &Cat : I.second->Categories) { if (find(Categories, Cat) == Categories.end() && Cat != &GenericCategory) - I.second->setHiddenFlag(cl::ReallyHidden); + I.second->setHiddenFlag(Opt); } } } Index: llvm/unittests/Support/CommandLineTest.cpp =================================================================== --- llvm/unittests/Support/CommandLineTest.cpp +++ llvm/unittests/Support/CommandLineTest.cpp @@ -378,17 +378,34 @@ StackOption TestOption1("hide-option-1"); StackOption TestOption2("hide-option-2", cl::cat(TestCategory)); + cl::HideUnrelatedOptions(TestCategory, cl::Hidden); + + ASSERT_EQ(cl::Hidden, TestOption1.getOptionHiddenFlag()) + << "Failed to hide extra option."; + ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) + << "Hid extra option that should be visible."; + + StringMap &Map = + cl::getRegisteredOptions(*cl::TopLevelSubCommand); + ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) + << "Hid default option that should be visible."; +} + +TEST(CommandLineTest, ReallyHideUnrelatedOptions) { + StackOption TestOption1("hide-option-1"); + StackOption TestOption2("hide-option-2", cl::cat(TestCategory)); + cl::HideUnrelatedOptions(TestCategory); ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag()) << "Failed to hide extra option."; ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) - << "Hid extra option that should be visable."; + << "Hid extra option that should be visible."; StringMap &Map = cl::getRegisteredOptions(*cl::TopLevelSubCommand); ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) - << "Hid default option that should be visable."; + << "Hid default option that should be visible."; } cl::OptionCategory TestCategory2("Test Options set 2", "Description"); @@ -401,19 +418,42 @@ const cl::OptionCategory *VisibleCategories[] = {&TestCategory, &TestCategory2}; + cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories), cl::Hidden); + + ASSERT_EQ(cl::Hidden, TestOption1.getOptionHiddenFlag()) + << "Failed to hide extra option."; + ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) + << "Hid extra option that should be visible."; + ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag()) + << "Hid extra option that should be visible."; + + StringMap &Map = + cl::getRegisteredOptions(*cl::TopLevelSubCommand); + ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) + << "Hid default option that should be visible."; +} + +TEST(CommandLineTest, ReallyHideUnrelatedOptionsMulti) { + StackOption TestOption1("multi-hide-option-1"); + StackOption TestOption2("multi-hide-option-2", cl::cat(TestCategory)); + StackOption TestOption3("multi-hide-option-3", cl::cat(TestCategory2)); + + const cl::OptionCategory *VisibleCategories[] = {&TestCategory, + &TestCategory2}; + cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories)); ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag()) << "Failed to hide extra option."; ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) - << "Hid extra option that should be visable."; + << "Hid extra option that should be visible."; ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag()) - << "Hid extra option that should be visable."; + << "Hid extra option that should be visible."; StringMap &Map = cl::getRegisteredOptions(*cl::TopLevelSubCommand); ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) - << "Hid default option that should be visable."; + << "Hid default option that should be visible."; } TEST(CommandLineTest, SetValueInSubcategories) {