Commit 34b92f689482f17a2cb7304e42df19ec09c8a977

Authored by Henry Fredrick Schreiner
1 parent c5cab1fe

Format cleanup, using standard fn

examples/try1.cpp
... ... @@ -4,10 +4,10 @@
4 4 int main (int argc, char** argv) {
5 5  
6 6 CLI::App app("K3Pi goofit fitter");
7   - CLI::App* start = app.add_subcommand("start");
8   - CLI::App* stop = app.add_subcommand("stop");
  7 + app.add_flag("--random", "Some random flag");
  8 + CLI::App* start = app.add_subcommand("start", "A great subcommand");
  9 + CLI::App* stop = app.add_subcommand("stop", "Do you really want to stop?");
9 10  
10   - std::cout << app.help();
11 11 std::string file;
12 12 start->add_option("-f,--file", file, "File name");
13 13  
... ...
include/CLI.hpp
... ... @@ -174,7 +174,13 @@ namespace detail {
174 174 return "STRING";
175 175 }
176 176  
177   -
  177 + void format_help(std::stringstream &out, std::string name, std::string discription, size_t wid) {
  178 + name = " " + name;
  179 + out << std::setw(wid) << std::left << name;
  180 + if(name.length()>=wid)
  181 + out << std::endl << std::setw(wid) << "";
  182 + out << discription << std::endl;
  183 + }
178 184  
179 185 struct Combiner {
180 186 int num;
... ... @@ -567,7 +573,7 @@ public:
567 573 /// The first half of the help print, name plus default, etc
568 574 std::string help_name() const {
569 575 std::stringstream out;
570   - out << " " << get_name();
  576 + out << get_name();
571 577 if(expected() != 0) {
572 578 if(typeval != "")
573 579 out << " " << typeval;
... ... @@ -581,25 +587,6 @@ public:
581 587 return out.str();
582 588 }
583 589  
584   - /// The length of the name part of the help, for formatting
585   - int help_len() const {
586   - return help_name().length();
587   - }
588   -
589   - /// Make a help string, adjustable len.
590   - std::string help(int len = 0) const {
591   - std::stringstream out;
592   - if(help_len() > len) {
593   - out << help_name() << "\n";
594   - out << std::setw(len) << " ";
595   -
596   - } else {
597   - out << std::setw(len) << std::left << help_name();
598   - }
599   - out << discription;
600   - return out.str();
601   - }
602   -
603 590 /// Produce a flattened vector of results, vs. a vector of vectors.
604 591 std::vector<std::string> flatten_results() const {
605 592 std::vector<std::string> output;
... ... @@ -1326,7 +1313,7 @@ public:
1326 1313 throw OptionNotFound(name);
1327 1314 }
1328 1315  
1329   - std::string help() const {
  1316 + std::string help(size_t wid=30) const {
1330 1317 std::stringstream out;
1331 1318 if(name != "")
1332 1319 out << "Subcommand: " << name << " ";
... ... @@ -1354,17 +1341,14 @@ public:
1354 1341 pos=true;
1355 1342 }
1356 1343  
1357   -
1358 1344 out << std::endl << std::endl;
1359 1345  
1360 1346 // Positional discriptions
1361 1347 if(pos) {
1362 1348 out << "Positionals:" << std::endl;
1363 1349 for(const Option &opt : options)
1364   - if(opt.positional() && opt.has_discription()) {
1365   - out << std::setw(30) << std::left << " " + opt.get_pname();
1366   - out << opt.get_discription() << std::endl;
1367   - }
  1350 + if(opt.positional() && opt.has_discription())
  1351 + detail::format_help(out, opt.get_pname(), opt.get_discription(), wid);
1368 1352 out << std::endl;
1369 1353  
1370 1354 }
... ... @@ -1374,9 +1358,9 @@ public:
1374 1358 if(npos) {
1375 1359 out << "Options:" << std::endl;
1376 1360 for(const Option &opt : options) {
1377   - if(opt.nonpositional()) {
1378   - out << opt.help(30) << std::endl;
1379   - }
  1361 + if(opt.nonpositional())
  1362 + detail::format_help(out, opt.help_name(), opt.get_discription(), wid);
  1363 +
1380 1364 }
1381 1365 out << std::endl;
1382 1366 }
... ... @@ -1384,11 +1368,8 @@ public:
1384 1368 // Subcommands
1385 1369 if(subcommands.size()> 0) {
1386 1370 out << "Subcommands:" << std::endl;
1387   - int max = std::accumulate(std::begin(subcommands), std::end(subcommands), 0,
1388   - [](int i, const std::unique_ptr<App> &j){return std::max(i, (int) j->get_name().length()+3);});
1389   - for(const std::unique_ptr<App> &com : subcommands) {
1390   - out << std::setw(max) << std::left << com->get_name() << " " << com->prog_discription << std::endl;
1391   - }
  1371 + for(const std::unique_ptr<App> &com : subcommands)
  1372 + detail::format_help(out, com->get_name(), com->prog_discription, wid);
1392 1373 }
1393 1374 return out.str();
1394 1375 }
... ...