exit(0);
}
void usage(const char *name) const {
static const std::size_t limit = 60;
static std::size_t count = 0;
static const auto print = [](const std::string &message) {
if (count + size(message) > limit) {
std::cout << "\n ";
count = 6;
}
std::cout << message;
count += size(message);
};
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;
}
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 (int i = 0; true; i++) {
const auto &opt = argp->options[i];
if (!opt.name && !opt.key) break;
if (!opt.name) continue;
if (!opt.arg) print(std::format(" [--{}]", opt.name));
else {
if (opt.options & Option::ARG_OPTIONAL) {
print(std::format(" [--{}[={}]]", opt.name, opt.arg));
} else {
print(std::format(" [--{}={}]", opt.name, opt.arg));
}
}
}
print(" [--help]");
print(" [--usage] ");
if (argp->doc) print(argp->doc);
std::cout << std::endl;
exit(0);
}
const argp_t *argp;
std::unordered_map<int, const option_t *> options;