From 2e4c78805db5c6ac393d2c60c745775162cb3133 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Mon, 13 Oct 2014 14:30:56 +1100 Subject: [PATCH] format help description --- src/cxxopts.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++-- src/example.cpp | 4 +++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index 7e443a7..c1b4b21 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -79,7 +79,50 @@ namespace cxxopts int width ) { - return text; + std::string result; + + auto current = text.begin(); + auto startLine = current; + auto lastSpace = current; + + int size = 0; + + while (current != text.end()) + { + if (*current == ' ') + { + lastSpace = current; + } + + if (size > width) + { + if (lastSpace == startLine) + { + result.append(startLine, current + 1); + result.append("\n"); + result.append(start, ' '); + } + else + { + result.append(startLine, current); + result.append("\n"); + result.append(start, ' '); + } + startLine = lastSpace + 1; + size = 0; + } + else + { + ++size; + } + + ++current; + } + + //append whatever is left + result.append(startLine, current); + + return result; } } @@ -367,7 +410,7 @@ Options::help() const auto fiter = format.begin(); for (const auto& o : group->second) { - auto d = format_description(o.desc, longest, allowed); + auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed); result += fiter->first; if (fiter->first.size() > longest) diff --git a/src/example.cpp b/src/example.cpp index 2317221..37c2f30 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -37,7 +37,9 @@ int main(int argc, char* argv[]) ("a,apple", "an apple") ("b,bob", "Bob") ("f,file", "File", cxxopts::value>()) - ("positional", "Positional arguments", cxxopts::value()) + ("positional", + "Positional arguments: these are the arguments that are entered " + "without an option", cxxopts::value()) ("help", "Print help") ; -- libgit2 0.21.4