Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CommandLine] Show '[subcommand]' in the help for less than 3 subcommands #74557

Merged
merged 1 commit into from
Dec 8, 2023

Conversation

igorkudrin
Copy link
Collaborator

When a tool defines only one or two subcommands, the [subcommand] part is not displayed in the USAGE help line. Note that a similar issue with printing the list of the subcommands has been fixed in https://reviews.llvm.org/D25463.

@llvmbot
Copy link
Collaborator

llvmbot commented Dec 6, 2023

@llvm/pr-subscribers-llvm-support

Author: Igor Kudrin (igorkudrin)

Changes

When a tool defines only one or two subcommands, the [subcommand] part is not displayed in the USAGE help line. Note that a similar issue with printing the list of the subcommands has been fixed in https://reviews.llvm.org/D25463.


Full diff: https://github.com/llvm/llvm-project/pull/74557.diff

1 Files Affected:

  • (modified) llvm/lib/Support/CommandLine.cpp (+1-1)
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index a7e0cae8b855d..31f79972125da 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -2372,7 +2372,7 @@ class HelpPrinter {
 
     if (Sub == &SubCommand::getTopLevel()) {
       outs() << "USAGE: " << GlobalParser->ProgramName;
-      if (Subs.size() > 2)
+      if (!Subs.empty())
         outs() << " [subcommand]";
       outs() << " [options]";
     } else {

@igorkudrin
Copy link
Collaborator Author

I can't find an easy way to test this change. cl::PrintHelpMessage() uses llvm::outs() unconditionally; changing it to take an output stream as an argument would cause a cascade of changes in all the methods it calls directly and indirectly, including, for example, Option::printOptionInfo() with its overrides, and many others. The patch doesn't seem worth such a massive change.

PS The buildbot failures do not seem relevant, as the patch could not cause crashes.

@dwblaikie
Copy link
Collaborator

I can't find an easy way to test this change. cl::PrintHelpMessage() uses llvm::outs() unconditionally; changing it to take an output stream as an argument would cause a cascade of changes in all the methods it calls directly and indirectly, including, for example, Option::printOptionInfo() with its overrides, and many others. The patch doesn't seem worth such a massive change.

I guess more unit-testability would be nice to have sooner or later as this library gets wider adoption. Whether or not you want to sign up for that now is a more open question, probably.

How were similar changes tested? Perhaps tested with a concrete usage like clang, etc? Maybe one of these users has a case that would demonstrate the change/fix?

@igorkudrin
Copy link
Collaborator Author

Hmm, I was under the impression that gtest runs unit tests in parallel, so it is not possible to intercept stdout. But in fact, gtest is single-threaded, and moreover, there are other tests in the test suite that already intercept the stream. So I added tests for the change to the patch.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Strange that the original patch (79f3333) used > 2 for "at least 1". I wonder if some refactoring later caused the issue?

@igorkudrin
Copy link
Collaborator Author

LGTM. Strange that the original patch (79f3333) used > 2 for "at least 1". I wonder if some refactoring later caused the issue?

Thanks. I guess that it was made to match the condition (Sub == &*TopLevelSubCommand && Subs.size() > 2) a few lines later. And (just guessing again) the original idea about Subs.size() > 2 might have been based on the assumption that two special subcommands, TopLevel and All, would also be added to the list. And all this proves once again the importance of testing.

…ands

When a tool defines only one or two subcommands, the `[subcommand]` part
is not displayed in the `USAGE` help line. Note that a similar issue
with printing the list of the subcommands has been fixed in
https://reviews.llvm.org/D25463.
@igorkudrin igorkudrin changed the title [Support] Show '[subcommand]' in the help for less than 3 subcommands [CommandLine] Show '[subcommand]' in the help for less than 3 subcommands Dec 8, 2023
@igorkudrin igorkudrin merged commit 9d66d26 into llvm:main Dec 8, 2023
4 checks passed
@igorkudrin igorkudrin deleted the cl-help-subcommand branch December 13, 2023 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants