From 9ff20ef857429619267e3f156a4f81ad9e1eb8c1 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 3 May 2020 16:36:22 +0200 Subject: Improve help printouts. --- getoptpp.hpp | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/getoptpp.hpp b/getoptpp.hpp index a363750..92f8e82 100644 --- a/getoptpp.hpp +++ b/getoptpp.hpp @@ -108,14 +108,8 @@ class Options { const std::vector arguments() const { return args; } void help() { - std::size_t width = 0; - for(auto const & opt : options) { - if(opt.name == nullptr) { - continue; // skip terminating option - } - width = std::max(width, std::strlen(opt.name) + (opt.has_arg != no_argument ? 0 : 3)); - } - width += 1; + std::size_t width = 26; + std::size_t column_width = 80; int index = 0; for(auto const & help_text : help_texts) { @@ -136,19 +130,43 @@ class Options { break; } + std::string option_str; + if(opt.val >= '!' && opt.val <= '~') + { + option_str = " -" + std::string(1, opt.val) + ", --" + opt.name + " " + args; + } + else + { + option_str = " --" + std::string(opt.name) + " " + args; + } + std::string padding; - std::size_t pad_size = width - (std::strlen(opt.name) + args.size()); - padding.append(pad_size, ' '); - if(opt.val >= 33 && opt.val <= 126) + if(option_str.size() < width) { - std::cout << " -" << static_cast(opt.val) << ", --" << opt.name << " " << args << padding; + padding.append(width - option_str.size(), ' '); } else { - std::cout << " --" << opt.name << " " << args << padding; + padding = "\n"; + padding.append(width, ' '); } - std::cout << help_text << std::endl; + std::cout << option_str << padding; + + auto i = width; + for(auto c : help_text) + { + if((c == '\n') || (i > column_width && (c == ' ' || c == '\t'))) + { + std::string padding(width, ' '); + std::cout << '\n' << padding; + i = width; + continue; + } + std::cout << c; + ++i; + } + std::cout << '\n'; ++index; } } -- cgit v1.2.3