llvm.org GIT mirror llvm / 0eff58c
[CommandLine] Change help output to prefix long options with `--` instead of `-`. NFC . Part 3 of 5 Summary: By default, `parseCommandLineOptions()` will accept either a `-` or `--` prefix for long options -- options with names longer than a single character. While this change does not affect behavior, it will be helpful with a subsequent change that requires long options use the `--` prefix. Reviewers: rnk, thopre Reviewed By: thopre Subscribers: thopre, cfe-commits, hiraditya, llvm-commits Tags: #llvm, #clang Differential Revision: https://reviews.llvm.org/D61269 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359909 91177308-0d34-0410-b5e6-96231b3b80d8 Don Hinton 1 year, 7 months ago
4 changed file(s) with 81 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
8787
8888 //===----------------------------------------------------------------------===//
8989
90 static StringRef ArgPrefix = " -";
91 static StringRef ArgPrefixLong = " --";
92 static StringRef ArgHelpPrefix = " - ";
93
94 static size_t argPlusPrefixesSize(StringRef ArgName) {
95 size_t Len = ArgName.size();
96 if (Len == 1)
97 return Len + ArgPrefix.size() + ArgHelpPrefix.size();
98 return Len + ArgPrefixLong.size() + ArgHelpPrefix.size();
99 }
100
101 static StringRef argPrefix(StringRef ArgName) {
102 if (ArgName.size() == 1)
103 return ArgPrefix;
104 return ArgPrefixLong;
105 }
106
90107 namespace {
108
109 class PrintArg {
110 StringRef ArgName;
111 public:
112 PrintArg(StringRef ArgName) : ArgName(ArgName) {}
113 friend raw_ostream &operator<<(raw_ostream &OS, const PrintArg&);
114 };
115
116 raw_ostream &operator<<(raw_ostream &OS, const PrintArg& Arg) {
117 OS << argPrefix(Arg.ArgName) << Arg.ArgName;
118 return OS;
119 }
91120
92121 class CommandLineParser {
93122 public:
13381367 if (!Handler) {
13391368 if (SinkOpts.empty()) {
13401369 *Errs << ProgramName << ": Unknown command line argument '" << argv[i]
1341 << "'. Try: '" << argv[0] << " -help'\n";
1370 << "'. Try: '" << argv[0] << " --help'\n";
13421371
13431372 if (NearestHandler) {
13441373 // If we know a near match, report it as well.
1345 *Errs << ProgramName << ": Did you mean '-" << NearestHandlerString
1346 << "'?\n";
1374 *Errs << ProgramName << ": Did you mean '"
1375 << PrintArg(NearestHandlerString) << "'?\n";
13471376 }
13481377
13491378 ErrorParsing = true;
13771406 << ": Not enough positional command line arguments specified!\n"
13781407 << "Must specify at least " << NumPositionalRequired
13791408 << " positional argument" << (NumPositionalRequired > 1 ? "s" : "")
1380 << ": See: " << argv[0] << " -help\n";
1409 << ": See: " << argv[0] << " --help\n";
13811410
13821411 ErrorParsing = true;
13831412 } else if (!HasUnlimitedPositionals &&
13841413 PositionalVals.size() > PositionalOpts.size()) {
13851414 *Errs << ProgramName << ": Too many positional arguments specified!\n"
13861415 << "Can specify at most " << PositionalOpts.size()
1387 << " positional arguments: See: " << argv[0] << " -help\n";
1416 << " positional arguments: See: " << argv[0] << " --help\n";
13881417 ErrorParsing = true;
13891418
13901419 } else if (!ConsumeAfterOpt) {
14971526 if (ArgName.empty())
14981527 Errs << HelpStr; // Be nice for positional arguments
14991528 else
1500 Errs << GlobalParser->ProgramName << ": for the -" << ArgName;
1529 Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName);
15011530
15021531 Errs << " option: " << Message << "\n";
15031532 return true;
15351564 return O.ValueStr;
15361565 }
15371566
1538 static StringRef ArgPrefix = " -";
1539 static StringRef ArgHelpPrefix = " - ";
1540 static size_t ArgPrefixesSize = ArgPrefix.size() + ArgHelpPrefix.size();
1541
15421567 //===----------------------------------------------------------------------===//
15431568 // cl::alias class implementation
15441569 //
15451570
15461571 // Return the width of the option tag for printing...
1547 size_t alias::getOptionWidth() const { return ArgStr.size() + ArgPrefixesSize; }
1572 size_t alias::getOptionWidth() const {
1573 return argPlusPrefixesSize(ArgStr);
1574 }
15481575
15491576 void Option::printHelpStr(StringRef HelpStr, size_t Indent,
15501577 size_t FirstLineIndentedBy) {
15601587
15611588 // Print out the option for the alias.
15621589 void alias::printOptionInfo(size_t GlobalWidth) const {
1563 outs() << ArgPrefix << ArgStr;
1564 printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + ArgPrefixesSize);
1590 outs() << PrintArg(ArgStr);
1591 printHelpStr(HelpStr, GlobalWidth, argPlusPrefixesSize(ArgStr));
15651592 }
15661593
15671594 //===----------------------------------------------------------------------===//
15731600
15741601 // Return the width of the option tag for printing...
15751602 size_t basic_parser_impl::getOptionWidth(const Option &O) const {
1576 size_t Len = O.ArgStr.size();
1603 size_t Len = argPlusPrefixesSize(O.ArgStr);
15771604 auto ValName = getValueName();
15781605 if (!ValName.empty()) {
15791606 size_t FormattingLen = 3;
15821609 Len += getValueStr(O, ValName).size() + FormattingLen;
15831610 }
15841611
1585 return Len + ArgPrefixesSize;
1612 return Len;
15861613 }
15871614
15881615 // printOptionInfo - Print out information about this option. The
15901617 //
15911618 void basic_parser_impl::printOptionInfo(const Option &O,
15921619 size_t GlobalWidth) const {
1593 outs() << ArgPrefix << O.ArgStr;
1620 outs() << PrintArg(O.ArgStr);
15941621
15951622 auto ValName = getValueName();
15961623 if (!ValName.empty()) {
16061633
16071634 void basic_parser_impl::printOptionName(const Option &O,
16081635 size_t GlobalWidth) const {
1609 outs() << ArgPrefix << O.ArgStr;
1636 outs() << PrintArg(O.ArgStr);
16101637 outs().indent(GlobalWidth - O.ArgStr.size());
16111638 }
16121639
17381765 // Return the width of the option tag for printing...
17391766 size_t generic_parser_base::getOptionWidth(const Option &O) const {
17401767 if (O.hasArgStr()) {
1741 size_t Size = O.ArgStr.size() + ArgPrefixesSize + EqValue.size();
1768 size_t Size =
1769 argPlusPrefixesSize(O.ArgStr) + EqValue.size();
17421770 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
17431771 StringRef Name = getOption(i);
17441772 if (!shouldPrintOption(Name, getDescription(i), O))
17661794 if (O.getValueExpectedFlag() == ValueOptional) {
17671795 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
17681796 if (getOption(i).empty()) {
1769 outs() << ArgPrefix << O.ArgStr;
1797 outs() << PrintArg(O.ArgStr);
17701798 Option::printHelpStr(O.HelpStr, GlobalWidth,
1771 O.ArgStr.size() + ArgPrefixesSize);
1799 argPlusPrefixesSize(O.ArgStr));
17721800 break;
17731801 }
17741802 }
17751803 }
17761804
1777 outs() << ArgPrefix << O.ArgStr << EqValue;
1805 outs() << PrintArg(O.ArgStr) << EqValue;
17781806 Option::printHelpStr(O.HelpStr, GlobalWidth,
1779 O.ArgStr.size() + EqValue.size() + ArgPrefixesSize);
1807 EqValue.size() +
1808 argPlusPrefixesSize(O.ArgStr));
17801809 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
17811810 StringRef OptionName = getOption(i);
17821811 StringRef Description = getDescription(i);
17981827 if (!O.HelpStr.empty())
17991828 outs() << " " << O.HelpStr << '\n';
18001829 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
1801 auto Option = getOption(i);
1802 outs() << " -" << Option;
1830 StringRef Option = getOption(i);
1831 outs() << " " << PrintArg(Option);
18031832 Option::printHelpStr(getDescription(i), GlobalWidth, Option.size() + 8);
18041833 }
18051834 }
18131842 void generic_parser_base::printGenericOptionDiff(
18141843 const Option &O, const GenericOptionValue &Value,
18151844 const GenericOptionValue &Default, size_t GlobalWidth) const {
1816 outs() << " -" << O.ArgStr;
1845 outs() << " " << PrintArg(O.ArgStr);
18171846 outs().indent(GlobalWidth - O.ArgStr.size());
18181847
18191848 unsigned NumOpts = getNumOptions();
20332062 printSubCommands(Subs, MaxSubLen);
20342063 outs() << "\n";
20352064 outs() << " Type \"" << GlobalParser->ProgramName
2036 << " -help\" to get more help on a specific "
2065 << " --help\" to get more help on a specific "
20372066 "subcommand";
20382067 }
20392068
21102139 Category = SortedCategories.begin(),
21112140 E = SortedCategories.end();
21122141 Category != E; ++Category) {
2113 // Hide empty categories for -help, but show for -help-hidden.
2142 // Hide empty categories for --help, but show for --help-hidden.
21142143 const auto &CategoryOptions = CategorizedOptions[*Category];
21152144 bool IsEmptyCategory = CategoryOptions.empty();
21162145 if (!ShowHidden && IsEmptyCategory)
21262155 else
21272156 outs() << "\n";
21282157
2129 // When using -help-hidden explicitly state if the category has no
2158 // When using --help-hidden explicitly state if the category has no
21302159 // options associated with it.
21312160 if (IsEmptyCategory) {
21322161 outs() << " This option category has no options.\n";
21762205 static cl::OptionCategory GenericCategory("Generic Options");
21772206
21782207 // Define uncategorized help printers.
2179 // -help-list is hidden by default because if Option categories are being used
2180 // then -help behaves the same as -help-list.
2208 // --help-list is hidden by default because if Option categories are being used
2209 // then --help behaves the same as --help-list.
21812210 static cl::opt> HLOp(
21822211 "help-list",
2183 cl::desc("Display list of available options (-help-list-hidden for more)"),
2212 cl::desc("Display list of available options (--help-list-hidden for more)"),
21842213 cl::location(UncategorizedNormalPrinter), cl::Hidden, cl::ValueDisallowed,
21852214 cl::cat(GenericCategory), cl::sub(*AllSubCommands));
21862215
21942223 // behaviour at runtime depending on whether one or more Option categories have
21952224 // been declared.
21962225 static cl::opt>
2197 HOp("help", cl::desc("Display available options (-help-hidden for more)"),
2226 HOp("help", cl::desc("Display available options (--help-hidden for more)"),
21982227 cl::location(WrappedNormalPrinter), cl::ValueDisallowed,
21992228 cl::cat(GenericCategory), cl::sub(*AllSubCommands));
22002229
2201 static cl::alias HOpA("h", cl::desc("Alias for -help"), cl::aliasopt(HOp),
2230 static cl::alias HOpA("h", cl::desc("Alias for --help"), cl::aliasopt(HOp),
22022231 cl::DefaultOption);
22032232
22042233 static cl::opt>
22252254 // registered then it is useful to show the categorized help instead of
22262255 // uncategorized help.
22272256 if (GlobalParser->RegisteredOptionCategories.size() > 1) {
2228 // unhide -help-list option so user can have uncategorized output if they
2257 // unhide --help-list option so user can have uncategorized output if they
22292258 // want it.
22302259 HLOp.setHiddenFlag(NotHidden);
22312260
2525 ; RUN: not FileCheck -dump-input=foobar 2>&1 \
2626 ; RUN: | FileCheck %s -match-full-lines -check-prefix=BADVAL
2727
28 BADVAL: {{F|f}}ile{{C|c}}heck{{.*}}: for the -dump-input option: Cannot find option named 'foobar'!
28 BADVAL: {{F|f}}ile{{C|c}}heck{{.*}}: for the --dump-input option: Cannot find option named 'foobar'!
2929
3030 ;--------------------------------------------------
3131 ; Check -dump-input=help.
77
88 # CHECK-OBJDUMP: -h - Alias for --section-headers
99 # CHECK-READOBJ: -h - Alias for --file-headers
10 # CHECK-TBLGEN: -h - Alias for -help
11 # CHECK-OPT-RPT: -h - Alias for -help
10 # CHECK-TBLGEN: -h - Alias for --help
11 # CHECK-OPT-RPT: -h - Alias for --help
1212 # CHECK-DWARF: -h - Alias for -help
1313
1414 # llvm-dwarfdump declares `-h` option and prints special help in that case,
1515 # which is weird, but makes for a good test, i.e., shows the default `-h`
1616 # wasn't used.
17 # CHECK-DWARF-H-NOT: -help-list - Display list of available options (-help-list-hidden for more)
17 # CHECK-DWARF-H-NOT: --help-list - Display list of available options (--help-list-hidden for more)
10411041
10421042 StackOption TestOption(Opt, cl::desc(HelpText),
10431043 OptionAttributes...);
1044 printOptionInfo(TestOption, 25);
1044 printOptionInfo(TestOption, 26);
10451045 outs().flush();
10461046 }
10471047 auto Buffer = MemoryBuffer::getFile(File.FilePath);
10681068 cl::values(clEnumValN(OptionValue::Val, "v1", "desc1")));
10691069
10701070 // clang-format off
1071 EXPECT_EQ(Output, (" -" + Opt + "= - " + HelpText + "\n"
1072 " =v1 - desc1\n")
1071 EXPECT_EQ(Output, (" --" + Opt + "= - " + HelpText + "\n"
1072 " =v1 - desc1\n")
10731073 .str());
10741074 // clang-format on
10751075 }
10811081
10821082 // clang-format off
10831083 EXPECT_EQ(Output,
1084 (" -" + Opt + " - " + HelpText + "\n"
1085 " -" + Opt + "= - " + HelpText + "\n"
1086 " =v1 - desc1\n")
1084 (" --" + Opt + " - " + HelpText + "\n"
1085 " --" + Opt + "= - " + HelpText + "\n"
1086 " =v1 - desc1\n")
10871087 .str());
10881088 // clang-format on
10891089 }
10941094 clEnumValN(OptionValue::Val, "", "desc2")));
10951095
10961096 // clang-format off
1097 EXPECT_EQ(Output, (" -" + Opt + " - " + HelpText + "\n"
1098 " -" + Opt + "= - " + HelpText + "\n"
1099 " =v1 - desc1\n"
1100 " = - desc2\n")
1097 EXPECT_EQ(Output, (" --" + Opt + " - " + HelpText + "\n"
1098 " --" + Opt + "= - " + HelpText + "\n"
1099 " =v1 - desc1\n"
1100 " = - desc2\n")
11011101 .str());
11021102 // clang-format on
11031103 }
11081108 clEnumValN(OptionValue::Val, "", "")));
11091109
11101110 // clang-format off
1111 EXPECT_EQ(Output, (" -" + Opt + "= - " + HelpText + "\n"
1112 " =v1 - desc1\n"
1111 EXPECT_EQ(Output, (" --" + Opt + "= - " + HelpText + "\n"
1112 " =v1 - desc1\n"
11131113 " =\n")
11141114 .str());
11151115 // clang-format on
11211121
11221122 // clang-format off
11231123 EXPECT_EQ(Output,
1124 (" -" + Opt + "= - " + HelpText + "\n"
1124 (" --" + Opt + "= - " + HelpText + "\n"
11251125 " =v1\n").str());
11261126 // clang-format on
11271127 }
11461146
11471147 TEST_F(GetOptionWidthTest, GetOptionWidthArgNameLonger) {
11481148 StringRef ArgName("a-long-argument-name");
1149 size_t ExpectedStrSize = (" -" + ArgName + "= - ").str().size();
1149 size_t ExpectedStrSize = (" --" + ArgName + "= - ").str().size();
11501150 EXPECT_EQ(
11511151 runTest(ArgName, cl::values(clEnumValN(OptionValue::Val, "v", "help"))),
11521152 ExpectedStrSize);