Commit 34b92f689482f17a2cb7304e42df19ec09c8a977
1 parent
c5cab1fe
Format cleanup, using standard fn
Showing
2 changed files
with
19 additions
and
38 deletions
examples/try1.cpp
| @@ -4,10 +4,10 @@ | @@ -4,10 +4,10 @@ | ||
| 4 | int main (int argc, char** argv) { | 4 | int main (int argc, char** argv) { |
| 5 | 5 | ||
| 6 | CLI::App app("K3Pi goofit fitter"); | 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 | std::string file; | 11 | std::string file; |
| 12 | start->add_option("-f,--file", file, "File name"); | 12 | start->add_option("-f,--file", file, "File name"); |
| 13 | 13 |
include/CLI.hpp
| @@ -174,7 +174,13 @@ namespace detail { | @@ -174,7 +174,13 @@ namespace detail { | ||
| 174 | return "STRING"; | 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 | struct Combiner { | 185 | struct Combiner { |
| 180 | int num; | 186 | int num; |
| @@ -567,7 +573,7 @@ public: | @@ -567,7 +573,7 @@ public: | ||
| 567 | /// The first half of the help print, name plus default, etc | 573 | /// The first half of the help print, name plus default, etc |
| 568 | std::string help_name() const { | 574 | std::string help_name() const { |
| 569 | std::stringstream out; | 575 | std::stringstream out; |
| 570 | - out << " " << get_name(); | 576 | + out << get_name(); |
| 571 | if(expected() != 0) { | 577 | if(expected() != 0) { |
| 572 | if(typeval != "") | 578 | if(typeval != "") |
| 573 | out << " " << typeval; | 579 | out << " " << typeval; |
| @@ -581,25 +587,6 @@ public: | @@ -581,25 +587,6 @@ public: | ||
| 581 | return out.str(); | 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 | /// Produce a flattened vector of results, vs. a vector of vectors. | 590 | /// Produce a flattened vector of results, vs. a vector of vectors. |
| 604 | std::vector<std::string> flatten_results() const { | 591 | std::vector<std::string> flatten_results() const { |
| 605 | std::vector<std::string> output; | 592 | std::vector<std::string> output; |
| @@ -1326,7 +1313,7 @@ public: | @@ -1326,7 +1313,7 @@ public: | ||
| 1326 | throw OptionNotFound(name); | 1313 | throw OptionNotFound(name); |
| 1327 | } | 1314 | } |
| 1328 | 1315 | ||
| 1329 | - std::string help() const { | 1316 | + std::string help(size_t wid=30) const { |
| 1330 | std::stringstream out; | 1317 | std::stringstream out; |
| 1331 | if(name != "") | 1318 | if(name != "") |
| 1332 | out << "Subcommand: " << name << " "; | 1319 | out << "Subcommand: " << name << " "; |
| @@ -1354,17 +1341,14 @@ public: | @@ -1354,17 +1341,14 @@ public: | ||
| 1354 | pos=true; | 1341 | pos=true; |
| 1355 | } | 1342 | } |
| 1356 | 1343 | ||
| 1357 | - | ||
| 1358 | out << std::endl << std::endl; | 1344 | out << std::endl << std::endl; |
| 1359 | 1345 | ||
| 1360 | // Positional discriptions | 1346 | // Positional discriptions |
| 1361 | if(pos) { | 1347 | if(pos) { |
| 1362 | out << "Positionals:" << std::endl; | 1348 | out << "Positionals:" << std::endl; |
| 1363 | for(const Option &opt : options) | 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 | out << std::endl; | 1352 | out << std::endl; |
| 1369 | 1353 | ||
| 1370 | } | 1354 | } |
| @@ -1374,9 +1358,9 @@ public: | @@ -1374,9 +1358,9 @@ public: | ||
| 1374 | if(npos) { | 1358 | if(npos) { |
| 1375 | out << "Options:" << std::endl; | 1359 | out << "Options:" << std::endl; |
| 1376 | for(const Option &opt : options) { | 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 | out << std::endl; | 1365 | out << std::endl; |
| 1382 | } | 1366 | } |
| @@ -1384,11 +1368,8 @@ public: | @@ -1384,11 +1368,8 @@ public: | ||
| 1384 | // Subcommands | 1368 | // Subcommands |
| 1385 | if(subcommands.size()> 0) { | 1369 | if(subcommands.size()> 0) { |
| 1386 | out << "Subcommands:" << std::endl; | 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 | return out.str(); | 1374 | return out.str(); |
| 1394 | } | 1375 | } |