poafloc

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

commit d5dd6c25d71bb1a777ff99b2a6903b880eb713a0
parent f43244f45b0a1d518bafd73e85555c25bacb02a0
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat,  8 Jun 2024 21:10:01 +0200

Streamline --usage using existing help_entries

* More concise code without redundancies
* Exclude hidden
* Better sorting

Diffstat:
Margs.hpp | 53+++++++++++++++++++++++------------------------------
1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/args.hpp b/args.hpp @@ -393,51 +393,44 @@ class Parser { std::string message = std::format("Usage: {}", name); message += " [-"; - for (int i = 0; true; i++) { - const auto &opt = argp->options[i]; - if (!opt.name && !opt.key) break; - if (!std::isprint(opt.key)) continue; - if (opt.arg) continue; - - message += (char)opt.key; + for (const auto &entry : help_entries) { + if (entry.arg()) continue; + for (const char c : entry.opt_short()) { + message += c; + } } - message += "?]"; + message += "]"; std::cout << message; count = size(message); - for (int i = 0; true; i++) { - const auto &opt = argp->options[i]; - if (!opt.name && !opt.key) break; - if (!std::isprint(opt.key)) continue; - if (!opt.arg) continue; - - if (opt.options & Option::ARG_OPTIONAL) { - print(std::format(" [-{}[{}]]", (char)opt.key, opt.arg)); - } else { - print(std::format(" [-{} {}]", (char)opt.key, opt.arg)); + for (const auto &entry : help_entries) { + if (!entry.arg()) continue; + for (const char c : entry.opt_short()) { + if (entry.opt()) { + print(std::format(" [-{}[{}]]", c, entry.arg())); + } else { + print(std::format(" [-{} {}]", c, entry.arg())); + } } } - for (int i = 0; true; i++) { - const auto &opt = argp->options[i]; - if (!opt.name && !opt.key) break; - if (!opt.name) continue; + for (const auto &entry : help_entries) { + for (const char *name : entry.opt_long()) { + if (!entry.arg()) { + print(std::format(" [--{}]", name)); + continue; + } - if (!opt.arg) print(std::format(" [--{}]", opt.name)); - else { - if (opt.options & Option::ARG_OPTIONAL) { - print(std::format(" [--{}[={}]]", opt.name, opt.arg)); + if (entry.opt()) { + print(std::format(" [--{}[={}]]", name, entry.arg())); } else { - print(std::format(" [--{}={}]", opt.name, opt.arg)); + print(std::format(" [--{}={}]", name, entry.arg())); } } } - print(" [--help]"); - print(" [--usage] "); if (argp->doc) print(argp->doc); - std::cout << std::endl; exit(0);