[opt] Change the parameter of OptTable::PrintHelp from Name to Usage and don't append...
authorFangrui Song <maskray@google.com>
Wed, 10 Oct 2018 00:15:31 +0000 (00:15 +0000)
committerFangrui Song <maskray@google.com>
Wed, 10 Oct 2018 00:15:31 +0000 (00:15 +0000)
Summary:
Before, "[options] <inputs>" is unconditionally appended to the `Name` parameter. It is more flexible to change its semantic to `Usage` and let user customize the usage line.

% llvm-objcopy
...
USAGE: llvm-objcopy <input> [ <output> ] [options] <inputs>

With this patch:

% llvm-objcopy
...
USAGE: llvm-objcopy input [output]

Reviewers: rupprecht, alexshap, jhenderson

Reviewed By: rupprecht

Subscribers: jakehehrlich, mehdi_amini, steven_wu, dexonsmith, llvm-commits

Differential Revision: https://reviews.llvm.org/D51009

llvm-svn: 344097

llvm/include/llvm/Option/OptTable.h
llvm/lib/Option/OptTable.cpp
llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
llvm/test/tools/llvm-cvtres/help.test
llvm/test/tools/llvm-rc/helpmsg.test
llvm/tools/llvm-cvtres/llvm-cvtres.cpp
llvm/tools/llvm-mt/llvm-mt.cpp
llvm/tools/llvm-objcopy/llvm-objcopy.cpp
llvm/tools/llvm-rc/llvm-rc.cpp
llvm/unittests/Option/OptionParsingTest.cpp

index 743c477..fdb05d8 100644 (file)
@@ -217,8 +217,8 @@ public:
   /// Render the help text for an option table.
   ///
   /// \param OS - The stream to write the help text to.
-  /// \param Name - The name to use in the usage line.
-  /// \param Title - The title to use in the usage line.
+  /// \param Usage - USAGE: Usage
+  /// \param Title - OVERVIEW: Title
   /// \param FlagsToInclude - If non-zero, only include options with any
   ///                         of these flags set.
   /// \param FlagsToExclude - Exclude options with any of these flags set.
@@ -226,11 +226,11 @@ public:
   ///                         that don't have help texts. By default, we display
   ///                         only options that are not hidden and have help
   ///                         texts.
-  void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
+  void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
                  unsigned FlagsToInclude, unsigned FlagsToExclude,
                  bool ShowAllAliases) const;
 
-  void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
+  void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
                  bool ShowHidden = false, bool ShowAllAliases = false) const;
 };
 
index 022b9d5..312ff78 100644 (file)
@@ -521,19 +521,17 @@ static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
   return getOptionHelpGroup(Opts, GroupID);
 }
 
-void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
+void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
                          bool ShowHidden, bool ShowAllAliases) const {
-  PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/
+  PrintHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/
             (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
 }
 
-void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
+void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
                          unsigned FlagsToInclude, unsigned FlagsToExclude,
                          bool ShowAllAliases) const {
-  OS << "OVERVIEW: " << Title << "\n";
-  OS << '\n';
-  OS << "USAGE: " << Name << " [options] <inputs>\n";
-  OS << '\n';
+  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.
index f8de7ca..c5a28d4 100644 (file)
@@ -96,7 +96,8 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
   // Handle when no input or output is specified
   if (Args.hasArgNoClaim(OPT_INPUT) ||
       (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l))) {
-    Table.PrintHelp(outs(), ArgsArr[0], "dlltool", false);
+    Table.PrintHelp(outs(), "llvm-dlltool [options] file...", "llvm-dlltool",
+                    false);
     llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64\n";
     return 1;
   }
index d636dca..64f4fe4 100644 (file)
@@ -123,7 +123,7 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
 
   // Handle /help
   if (Args.hasArg(OPT_help)) {
-    Table.PrintHelp(outs(), ArgsArr[0], "LLVM Lib");
+    Table.PrintHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
     return 0;
   }
 
index e807236..7df6264 100644 (file)
@@ -2,7 +2,7 @@
 ; RUN: FileCheck -input-file=%t %s -check-prefix=HELP_TEST
 
 ; HELP_TEST:     OVERVIEW: Resource Converter
-; HELP_TEST-DAG:  USAGE: cvtres [options] <inputs>
+; HELP_TEST-DAG:  USAGE: llvm-cvtres [options] file...
 ; HELP_TEST-DAG:  OPTIONS:
 ; HELP_TEST-NEXT:   /DEFINE:symbol 
 ; HELP_TEST-NEXT:   /FOLDDUPS:     
index e91d636..b9a55f6 100644 (file)
@@ -4,7 +4,7 @@
 ; RUN: FileCheck -input-file=%t1 %s
 
 ; CHECK:  OVERVIEW: Resource Converter
-; CHECK-DAG:  USAGE: rc [options] <inputs>
+; CHECK-DAG:  USAGE: rc [options] file...
 ; CHECK-DAG:  OPTIONS:
 ; CHECK-NEXT:    /?          Display this help and exit.
 ; CHECK-NEXT:    /C <value>  Set the codepage used for input strings.
index 5d8e6ab..53989c8 100644 (file)
@@ -103,7 +103,7 @@ int main(int Argc, const char **Argv) {
   opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
 
   if (InputArgs.hasArg(OPT_HELP)) {
-    T.PrintHelp(outs(), "cvtres", "Resource Converter", false);
+    T.PrintHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter", false);
     return 0;
   }
 
index 1339fd1..ea75c01 100644 (file)
@@ -115,7 +115,7 @@ int main(int Argc, const char **Argv) {
   }
 
   if (InputArgs.hasArg(OPT_help)) {
-    T.PrintHelp(outs(), "mt", "Manifest Tool", false);
+    T.PrintHelp(outs(), "llvm-mt [options] file...", "Manifest Tool", false);
     return 0;
   }
 
index 345a11d..3e494f9 100644 (file)
@@ -897,12 +897,12 @@ static DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
 
   if (InputArgs.size() == 0) {
-    T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
+    T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool");
     exit(1);
   }
 
   if (InputArgs.hasArg(OBJCOPY_help)) {
-    T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
+    T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool");
     exit(0);
   }
 
@@ -1039,13 +1039,14 @@ static DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) {
   llvm::opt::InputArgList InputArgs =
       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
 
+  static const char Usage[] = "llvm-strip [options] file...";
   if (InputArgs.size() == 0) {
-    T.PrintHelp(errs(), "llvm-strip", "strip tool");
+    T.PrintHelp(errs(), Usage, "strip tool");
     exit(1);
   }
 
   if (InputArgs.hasArg(STRIP_help)) {
-    T.PrintHelp(outs(), "llvm-strip", "strip tool");
+    T.PrintHelp(outs(), Usage, "strip tool");
     exit(0);
   }
 
index 0448f45..4511c5c 100644 (file)
@@ -90,7 +90,7 @@ int main(int Argc, const char **Argv) {
 
   // The tool prints nothing when invoked with no command-line arguments.
   if (InputArgs.hasArg(OPT_HELP)) {
-    T.PrintHelp(outs(), "rc", "Resource Converter", false);
+    T.PrintHelp(outs(), "rc [options] file...", "Resource Converter", false);
     return 0;
   }
 
index eef21ab..230049c 100644 (file)
@@ -97,6 +97,10 @@ TEST(Option, OptionParsing) {
   T.PrintHelp(RSO, "test", "title!");
   EXPECT_NE(std::string::npos, Help.find("-A"));
 
+  // Check usage line.
+  T.PrintHelp(RSO, "name [options] file...", "title!");
+  EXPECT_NE(std::string::npos, Help.find("USAGE: name [options] file...\n"));
+
   // Test aliases.
   auto Cs = AL.filtered(OPT_C);
   ASSERT_NE(Cs.begin(), Cs.end());