Commit 2e4c78805db5c6ac393d2c60c745775162cb3133
1 parent
96b2e09b
format help description
Showing
2 changed files
with
48 additions
and
3 deletions
src/cxxopts.cpp
| ... | ... | @@ -79,7 +79,50 @@ namespace cxxopts |
| 79 | 79 | int width |
| 80 | 80 | ) |
| 81 | 81 | { |
| 82 | - return text; | |
| 82 | + std::string result; | |
| 83 | + | |
| 84 | + auto current = text.begin(); | |
| 85 | + auto startLine = current; | |
| 86 | + auto lastSpace = current; | |
| 87 | + | |
| 88 | + int size = 0; | |
| 89 | + | |
| 90 | + while (current != text.end()) | |
| 91 | + { | |
| 92 | + if (*current == ' ') | |
| 93 | + { | |
| 94 | + lastSpace = current; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if (size > width) | |
| 98 | + { | |
| 99 | + if (lastSpace == startLine) | |
| 100 | + { | |
| 101 | + result.append(startLine, current + 1); | |
| 102 | + result.append("\n"); | |
| 103 | + result.append(start, ' '); | |
| 104 | + } | |
| 105 | + else | |
| 106 | + { | |
| 107 | + result.append(startLine, current); | |
| 108 | + result.append("\n"); | |
| 109 | + result.append(start, ' '); | |
| 110 | + } | |
| 111 | + startLine = lastSpace + 1; | |
| 112 | + size = 0; | |
| 113 | + } | |
| 114 | + else | |
| 115 | + { | |
| 116 | + ++size; | |
| 117 | + } | |
| 118 | + | |
| 119 | + ++current; | |
| 120 | + } | |
| 121 | + | |
| 122 | + //append whatever is left | |
| 123 | + result.append(startLine, current); | |
| 124 | + | |
| 125 | + return result; | |
| 83 | 126 | } |
| 84 | 127 | } |
| 85 | 128 | |
| ... | ... | @@ -367,7 +410,7 @@ Options::help() const |
| 367 | 410 | auto fiter = format.begin(); |
| 368 | 411 | for (const auto& o : group->second) |
| 369 | 412 | { |
| 370 | - auto d = format_description(o.desc, longest, allowed); | |
| 413 | + auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed); | |
| 371 | 414 | |
| 372 | 415 | result += fiter->first; |
| 373 | 416 | if (fiter->first.size() > longest) | ... | ... |
src/example.cpp
| ... | ... | @@ -37,7 +37,9 @@ int main(int argc, char* argv[]) |
| 37 | 37 | ("a,apple", "an apple") |
| 38 | 38 | ("b,bob", "Bob") |
| 39 | 39 | ("f,file", "File", cxxopts::value<std::vector<std::string>>()) |
| 40 | - ("positional", "Positional arguments", cxxopts::value<std::string>()) | |
| 40 | + ("positional", | |
| 41 | + "Positional arguments: these are the arguments that are entered " | |
| 42 | + "without an option", cxxopts::value<std::string>()) | |
| 41 | 43 | ("help", "Print help") |
| 42 | 44 | ; |
| 43 | 45 | ... | ... |