Commit 361f0d8bf451afe3d6f6b47e3c0b3ecf0f9f0385

Authored by Jarryd Beck
1 parent def4848b

basic help printing

Showing 1 changed file with 41 additions and 1 deletions
src/cxxopts.cpp
@@ -24,6 +24,9 @@ THE SOFTWARE. @@ -24,6 +24,9 @@ THE SOFTWARE.
24 24
25 #include "cxxopts.hpp" 25 #include "cxxopts.hpp"
26 26
  27 +#define OPTION_LONGEST 30
  28 +#define OPTION_DESC_GAP 2
  29 +
27 namespace cxxopts 30 namespace cxxopts
28 { 31 {
29 32
@@ -67,6 +70,17 @@ namespace cxxopts @@ -67,6 +70,17 @@ namespace cxxopts
67 70
68 return result; 71 return result;
69 } 72 }
  73 +
  74 + std::string
  75 + format_description
  76 + (
  77 + const std::string& text,
  78 + int start,
  79 + int width
  80 + )
  81 + {
  82 + return text;
  83 + }
70 } 84 }
71 85
72 OptionAdder 86 OptionAdder
@@ -343,7 +357,33 @@ Options::help() const @@ -343,7 +357,33 @@ Options::help() const
343 auto s = format_option(o.s, o.l, o.has_arg); 357 auto s = format_option(o.s, o.l, o.has_arg);
344 longest = std::max(longest, s.size()); 358 longest = std::max(longest, s.size());
345 format.push_back(std::make_pair(s, std::string())); 359 format.push_back(std::make_pair(s, std::string()));
346 - result += s + "\n"; 360 + }
  361 +
  362 + longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST));
  363 +
  364 + //widest allowed description
  365 + int allowed = 78 - longest - OPTION_DESC_GAP;
  366 +
  367 + auto fiter = format.begin();
  368 + for (const auto& o : group->second)
  369 + {
  370 + auto d = format_description(o.desc, longest, allowed);
  371 +
  372 + result += fiter->first;
  373 + if (fiter->first.size() > longest)
  374 + {
  375 + result += "\n";
  376 + result += std::string(longest + OPTION_DESC_GAP, ' ');
  377 + }
  378 + else
  379 + {
  380 + result += std::string(longest + OPTION_DESC_GAP - fiter->first.size(),
  381 + ' ');
  382 + }
  383 + result += d;
  384 + result += "\n";
  385 +
  386 + ++fiter;
347 } 387 }
348 388
349 return result; 389 return result;