poafloc

poafloc - Parser Of Arguments For Lines Of Commands
git clone git://git.dimitrijedobrota.com/poafloc.git
Log | Files | Refs | README | LICENSE

commit 84921ee22706704d4d37b23fe3341a0218d099d0
parent fc5fd6d67d7b63502daeaad8e19df559d782a209
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat,  8 Jun 2024 20:48:44 +0200

Streamline --help generation

Diffstat:
Margs.hpp | 37+++++++++++++++++--------------------
1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/args.hpp b/args.hpp @@ -309,48 +309,45 @@ class Parser { argp->doc ? argp->doc : ""); for (const auto &entry : help_entries) { - std::size_t count = 0; bool prev = false; - std::cout << " "; + std::string message = " "; for (const char c : entry.opt_short()) { if (!prev) prev = true; - else - std::cout << ", ", count += 2; + else message += ", "; - std::string message = std::format("-{}", c); + message += std::format("-{}", c); if (entry.arg() && entry.opt_long().empty()) { - if (entry.opt()) + if (entry.opt()) { message += std::format("[{}]", entry.arg()); - else + } else { message += std::format(" {}", entry.arg()); + } } - - std::cout << message; - count += size(message); } - if (!prev) std::cout << " ", count += 4; + if (!prev) message += " "; for (const auto l : entry.opt_long()) { if (!prev) prev = true; - else - std::cout << ", ", count += 2; + else message += ", "; - std::string message = std::format("--{}", l); + message += std::format("--{}", l); if (entry.arg()) { - if (entry.opt()) + if (entry.opt()) { message += std::format("[={}]", entry.arg()); - else + } else { message += std::format("={}", entry.arg()); + } } - - std::cout << message; - count += size(message); } static const std::size_t limit = 30; - if (count < limit) std::cout << std::string(limit - count, ' '); + if (size(message) < limit) { + message += std::string(limit - size(message), ' '); + } + + std::cout << message; if (entry.message()) { std::istringstream iss(entry.message());