summaryrefslogtreecommitdiff
path: root/lib/Support/CommandLine.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-05-10 17:15:51 +0000
committerAlexander Kornienko <alexfh@google.com>2013-05-10 17:15:51 +0000
commit2e24e19cacacd4eea346b914124ffc3acadbd72e (patch)
tree5d2ffeea5338af801b9c96c9755ae7753d0a16c6 /lib/Support/CommandLine.cpp
parent3a0d00eef992daafecc4d96fa716f851d8c9059a (diff)
downloadllvm-2e24e19cacacd4eea346b914124ffc3acadbd72e.tar.gz
llvm-2e24e19cacacd4eea346b914124ffc3acadbd72e.tar.bz2
llvm-2e24e19cacacd4eea346b914124ffc3acadbd72e.tar.xz
Better output for long help strings for command-line options.
Summary: This patch allows using \n inside long help strings for command-line options, so that all lines are equally indented. This is not a perfect solution, as we don't (and probably don't want to) know about terminal width, but it allows to format long help strings somehow readable without manually padding them with spaces. A motivating example is -help output from clang-format (source code in tools/clang-format/ClangFormat.cpp, see cl options offset, length, style, and dump-config). Reviewers: atrick, alexfh Reviewed By: alexfh CC: llvm-commits, rafael Differential Revision: http://llvm-reviews.chandlerc.com/D779 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181608 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/CommandLine.cpp')
-rw-r--r--lib/Support/CommandLine.cpp24
1 files changed, 16 insertions, 8 deletions
diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp
index 18d3db527b..f09705e69a 100644
--- a/lib/Support/CommandLine.cpp
+++ b/lib/Support/CommandLine.cpp
@@ -913,11 +913,20 @@ size_t alias::getOptionWidth() const {
return std::strlen(ArgStr)+6;
}
+static void printHelpStr(StringRef HelpStr, size_t Indent,
+ size_t FirstLineIndentedBy) {
+ std::pair<StringRef, StringRef> Split = HelpStr.split('\n');
+ outs().indent(Indent - FirstLineIndentedBy) << " - " << Split.first << "\n";
+ while (!Split.second.empty()) {
+ Split = Split.second.split('\n');
+ outs().indent(Indent) << Split.first << "\n";
+ }
+}
+
// Print out the option for the alias.
void alias::printOptionInfo(size_t GlobalWidth) const {
- size_t L = std::strlen(ArgStr);
outs() << " -" << ArgStr;
- outs().indent(GlobalWidth-L-6) << " - " << HelpStr << "\n";
+ printHelpStr(HelpStr, GlobalWidth, std::strlen(ArgStr) + 6);
}
//===----------------------------------------------------------------------===//
@@ -946,7 +955,7 @@ void basic_parser_impl::printOptionInfo(const Option &O,
if (const char *ValName = getValueName())
outs() << "=<" << getValueStr(O, ValName) << '>';
- outs().indent(GlobalWidth-getOptionWidth(O)) << " - " << O.HelpStr << '\n';
+ printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
}
void basic_parser_impl::printOptionName(const Option &O,
@@ -1087,9 +1096,8 @@ size_t generic_parser_base::getOptionWidth(const Option &O) const {
void generic_parser_base::printOptionInfo(const Option &O,
size_t GlobalWidth) const {
if (O.hasArgStr()) {
- size_t L = std::strlen(O.ArgStr);
outs() << " -" << O.ArgStr;
- outs().indent(GlobalWidth-L-6) << " - " << O.HelpStr << '\n';
+ printHelpStr(O.HelpStr, GlobalWidth, std::strlen(O.ArgStr) + 6);
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
size_t NumSpaces = GlobalWidth-strlen(getOption(i))-8;
@@ -1100,9 +1108,9 @@ void generic_parser_base::printOptionInfo(const Option &O,
if (O.HelpStr[0])
outs() << " " << O.HelpStr << '\n';
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
- size_t L = std::strlen(getOption(i));
- outs() << " -" << getOption(i);
- outs().indent(GlobalWidth-L-8) << " - " << getDescription(i) << '\n';
+ const char *Option = getOption(i);
+ outs() << " -" << Option;
+ printHelpStr(getDescription(i), GlobalWidth, std::strlen(Option) + 8);
}
}
}