@@ -537,7 +537,6 @@ InputArgList OptTable::internalParseArgs(
537537
538538 MissingArgIndex = MissingArgCount = 0 ;
539539 unsigned Index = 0 , End = ArgArr.size ();
540-
541540 while (Index < End) {
542541 // Ingore nullptrs, they are response file's EOL markers
543542 if (Args.getArgString (Index) == nullptr ) {
@@ -562,9 +561,9 @@ InputArgList OptTable::internalParseArgs(
562561 }
563562
564563 unsigned Prev = Index;
565- std::unique_ptr<Arg> A =
566- GroupedShortOptions ? parseOneArgGrouped (Args, Index)
567- : internalParseOneArg (Args, Index, ExcludeOption);
564+ std::unique_ptr<Arg> A = GroupedShortOptions
565+ ? parseOneArgGrouped (Args, Index)
566+ : internalParseOneArg (Args, Index, ExcludeOption);
568567 assert ((Index > Prev || GroupedShortOptions) &&
569568 " Parser failed to consume argument." );
570569
@@ -720,9 +719,9 @@ static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
720719void OptTable::printHelp (raw_ostream &OS, const char *Usage, const char *Title,
721720 bool ShowHidden, bool ShowAllAliases,
722721 Visibility VisibilityMask,
723- StringRef Subcommand ) const {
722+ StringRef SubCommand ) const {
724723 return internalPrintHelp (
725- OS, Usage, Title, Subcommand , ShowHidden, ShowAllAliases,
724+ OS, Usage, Title, SubCommand , ShowHidden, ShowAllAliases,
726725 [VisibilityMask](const Info &CandidateInfo) -> bool {
727726 return (CandidateInfo.Visibility & VisibilityMask) == 0 ;
728727 },
@@ -735,7 +734,7 @@ void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
735734 bool ShowHidden = !(FlagsToExclude & HelpHidden);
736735 FlagsToExclude &= ~HelpHidden;
737736 return internalPrintHelp (
738- OS, Usage, Title, /* Subcommand =*/ {}, ShowHidden, ShowAllAliases,
737+ OS, Usage, Title, /* SubCommand =*/ {}, ShowHidden, ShowAllAliases,
739738 [FlagsToInclude, FlagsToExclude](const Info &CandidateInfo) {
740739 if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
741740 return true ;
@@ -747,7 +746,7 @@ void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
747746}
748747
749748void OptTable::internalPrintHelp (
750- raw_ostream &OS, const char *Usage, const char *Title, StringRef Subcommand ,
749+ raw_ostream &OS, const char *Usage, const char *Title, StringRef SubCommand ,
751750 bool ShowHidden, bool ShowAllAliases,
752751 std::function<bool (const Info &)> ExcludeOption,
753752 Visibility VisibilityMask) const {
@@ -757,10 +756,10 @@ void OptTable::internalPrintHelp(
757756 // pairs.
758757 std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
759758
760- const SubCommand * ActiveSubCommand =
759+ auto ActiveSubCommand =
761760 std::find_if (SubCommands.begin (), SubCommands.end (),
762- [&](const auto &C) { return Subcommand == C.Name ; });
763- if (!Subcommand .empty ()) {
761+ [&](const auto &C) { return SubCommand == C.Name ; });
762+ if (!SubCommand .empty ()) {
764763 assert (ActiveSubCommand != nullptr && " Not a valid registered subcommand." );
765764 OS << ActiveSubCommand->HelpText << " \n\n " ;
766765 if (!StringRef (ActiveSubCommand->Usage ).empty ())
@@ -776,29 +775,24 @@ void OptTable::internalPrintHelp(
776775 }
777776
778777 auto DoesOptionBelongToSubcommand = [&](const Info &CandidateInfo) {
778+ // Retrieve the SubCommandIDs registered to the given current CandidateInfo Option.
779779 ArrayRef<unsigned > SubCommandIDs =
780780 CandidateInfo.getCommandIDs (SubCommandIDsTable);
781781
782- // Options with no subcommands are global.
783- if (SubCommandIDs. empty ()) {
784- // if Subcommand is not empty then don't print global options.
785- return Subcommand .empty ();
786- }
787-
788- // If we are not in a subcommand, don't show subcommand-specific options .
789- if (Subcommand .empty ())
782+ // If no registered subcommands, then only global options are to be printed .
783+ // If no valid SubCommand ( empty) in commandline then print the current global CandidateInfo Option.
784+ if (SubCommandIDs. empty ())
785+ return SubCommand .empty ();
786+
787+ // Handle CandidateInfo Option which has at least one registered SubCommand.
788+ // If no valid SubCommand (empty) in commandline, this CandidateInfo option should not be printed .
789+ if (SubCommand .empty ())
790790 return false ;
791791
792- // The subcommand ID is its index in the SubCommands table.
792+ // Find the ID of the valid subcommand passed in commandline ( its index in the SubCommands table which contains all subcommands) .
793793 unsigned ActiveSubCommandID = ActiveSubCommand - &SubCommands[0 ];
794-
795- // Check if the option belongs to the active subcommand.
796- for (unsigned ID : SubCommandIDs) {
797- if (ID == ActiveSubCommandID) {
798- return true ;
799- }
800- }
801- return false ;
794+ // Print if the ActiveSubCommandID is registered with the CandidateInfo Option.
795+ return std::find (SubCommandIDs.begin (), SubCommandIDs.end (), ActiveSubCommandID) != SubCommandIDs.end ();
802796 };
803797
804798 for (unsigned Id = 1 , e = getNumOptions () + 1 ; Id != e; ++Id) {
0 commit comments