|
| 1 | +#include <fmt/format.h> |
| 2 | +#include <facade/util/cli_opts.hpp> |
| 3 | +#include <facade/util/visitor.hpp> |
| 4 | +#include <algorithm> |
| 5 | +#include <cassert> |
| 6 | +#include <filesystem> |
| 7 | +#include <iostream> |
| 8 | +#include <span> |
| 9 | +#include <variant> |
| 10 | + |
| 11 | +namespace facade { |
| 12 | +namespace { |
| 13 | +namespace fs = std::filesystem; |
| 14 | + |
| 15 | +CliOpts::Opt const* find_opt(CliOpts::Spec const& spec, std::variant<std::string_view, char> key) { |
| 16 | + for (auto const& opt : spec.options) { |
| 17 | + bool match{}; |
| 18 | + auto const visitor = Visitor{ |
| 19 | + [opt, &match](std::string_view const full) { |
| 20 | + if (opt.key.full == full) { match = true; } |
| 21 | + }, |
| 22 | + [opt, &match](char const single) { |
| 23 | + if (opt.key.single == single) { match = true; } |
| 24 | + }, |
| 25 | + }; |
| 26 | + std::visit(visitor, key); |
| 27 | + if (match) { return &opt; } |
| 28 | + } |
| 29 | + return {}; |
| 30 | +} |
| 31 | + |
| 32 | +struct OptParser { |
| 33 | + using Result = CliOpts::Result; |
| 34 | + |
| 35 | + CliOpts::Spec valid_spec{}; |
| 36 | + Ptr<CliOpts::Parser> out; |
| 37 | + std::string exe_name{}; |
| 38 | + |
| 39 | + OptParser(CliOpts::Spec spec, Ptr<CliOpts::Parser> out, char const* arg0) : out(out) { |
| 40 | + valid_spec.version = spec.version.empty() ? "(unknown)" : spec.version; |
| 41 | + std::erase_if(spec.options, [](CliOpts::Opt const& o) { return !o.key.valid(); }); |
| 42 | + valid_spec.options.reserve(spec.options.size() + 2); |
| 43 | + std::move(spec.options.begin(), spec.options.end(), std::back_inserter(valid_spec.options)); |
| 44 | + valid_spec.options.push_back(CliOpts::Opt{.key = {.full = "help"}, .help = "Show this help text"}); |
| 45 | + valid_spec.options.push_back(CliOpts::Opt{.key = {.full = "version"}, .help = "Display the version"}); |
| 46 | + exe_name = fs::path{arg0}.filename().stem().generic_string(); |
| 47 | + } |
| 48 | + |
| 49 | + std::size_t get_max_width() const { |
| 50 | + auto ret = std::size_t{}; |
| 51 | + for (auto const& opt : valid_spec.options) { |
| 52 | + auto width = opt.key.full.size(); |
| 53 | + if (!opt.value.empty()) { |
| 54 | + width += 1; // = |
| 55 | + if (opt.is_optional_value) { |
| 56 | + width += 2; // [] |
| 57 | + } |
| 58 | + width += opt.value.size(); |
| 59 | + } |
| 60 | + ret = std::max(ret, width); |
| 61 | + } |
| 62 | + return ret; |
| 63 | + } |
| 64 | + |
| 65 | + Result print_help() const { |
| 66 | + auto str = std::string{}; |
| 67 | + str.reserve(1024); |
| 68 | + fmt::format_to(std::back_inserter(str), "Usage: {} [OPTION]...\n\n", exe_name); |
| 69 | + auto const max_width = get_max_width(); |
| 70 | + for (auto const& opt : valid_spec.options) { |
| 71 | + fmt::format_to(std::back_inserter(str), " {}{}", (opt.key.single ? '-' : ' '), (opt.key.single ? opt.key.single : ' ')); |
| 72 | + if (!opt.key.full.empty()) { fmt::format_to(std::back_inserter(str), "{} --{}", (opt.key.single ? ',' : ' '), opt.key.full); } |
| 73 | + auto width = opt.key.full.size(); |
| 74 | + if (!opt.value.empty()) { |
| 75 | + fmt::format_to(std::back_inserter(str), "{}={}{}", (opt.is_optional_value ? "[" : ""), opt.value, (opt.is_optional_value ? "]" : "")); |
| 76 | + width += (opt.is_optional_value ? 3 : 1) + opt.value.size(); |
| 77 | + } |
| 78 | + assert(width <= max_width); |
| 79 | + auto const remain = max_width - width + 4; |
| 80 | + for (std::size_t i = 0; i < remain; ++i) { str += ' '; } |
| 81 | + fmt::format_to(std::back_inserter(str), "{}\n", opt.help); |
| 82 | + } |
| 83 | + std::cout << str << '\n'; |
| 84 | + return Result::eExitSuccess; |
| 85 | + } |
| 86 | + |
| 87 | + Result print_version() const { |
| 88 | + std::cout << fmt::format("{} version {}\n", exe_name, valid_spec.version); |
| 89 | + return Result::eExitSuccess; |
| 90 | + } |
| 91 | + |
| 92 | + CliOpts::Result opt(CliOpts::Key key, CliOpts::Value value) const { |
| 93 | + if (key.full == "help") { return print_help(); } |
| 94 | + if (key.full == "version") { return print_version(); } |
| 95 | + if (out) { out->opt(key, value); } |
| 96 | + return Result::eContinue; |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +struct ParseOpt { |
| 101 | + using Result = CliOpts::Result; |
| 102 | + |
| 103 | + OptParser const& out; |
| 104 | + |
| 105 | + std::string_view current{}; |
| 106 | + |
| 107 | + bool at_end() const { return current.empty(); } |
| 108 | + |
| 109 | + char advance() { |
| 110 | + if (at_end()) { return {}; } |
| 111 | + auto ret = current[0]; |
| 112 | + current = current.substr(1); |
| 113 | + return ret; |
| 114 | + } |
| 115 | + |
| 116 | + char peek() const { return current[0]; } |
| 117 | + |
| 118 | + Result unknown_option(std::string_view opt) const { |
| 119 | + std::cerr << fmt::format("unknown option: {}\n", opt); |
| 120 | + return Result::eExitFailure; |
| 121 | + } |
| 122 | + |
| 123 | + Result missing_value(CliOpts::Opt const& opt) const { |
| 124 | + auto str = opt.key.full; |
| 125 | + if (str.empty()) { str = {&opt.key.single, 1}; } |
| 126 | + std::cerr << fmt::format("missing required value for option: {}{}\n", (opt.key.full.empty() ? "-" : "--"), str); |
| 127 | + return Result::eExitFailure; |
| 128 | + } |
| 129 | + |
| 130 | + Result parse_word() { |
| 131 | + auto const it = current.find('='); |
| 132 | + CliOpts::Opt const* opt{}; |
| 133 | + auto value = CliOpts::Value{}; |
| 134 | + if (it != std::string_view::npos) { |
| 135 | + auto const key = current.substr(0, it); |
| 136 | + opt = find_opt(out.valid_spec, key); |
| 137 | + if (!opt) { return unknown_option(key); } |
| 138 | + value = current.substr(it + 1); |
| 139 | + } else { |
| 140 | + opt = find_opt(out.valid_spec, current); |
| 141 | + if (!opt) { return unknown_option(current); } |
| 142 | + } |
| 143 | + if (!opt->value.empty() && !opt->is_optional_value && value.empty()) { return missing_value(*opt); } |
| 144 | + return out.opt(opt->key, value); |
| 145 | + } |
| 146 | + |
| 147 | + Result parse_singles() { |
| 148 | + char prev{}; |
| 149 | + while (!at_end() && peek() != '=') { |
| 150 | + prev = advance(); |
| 151 | + auto const* opt = find_opt(out.valid_spec, prev); |
| 152 | + if (!opt) { return unknown_option({&prev, 1}); } |
| 153 | + return out.opt(opt->key, {}); |
| 154 | + } |
| 155 | + if (peek() == '=') { |
| 156 | + if (prev == '\0') { return unknown_option(""); } |
| 157 | + advance(); |
| 158 | + auto const* opt = find_opt(out.valid_spec, prev); |
| 159 | + if (!opt) { return unknown_option({&prev, 1}); } |
| 160 | + return out.opt(opt->key, current); |
| 161 | + } |
| 162 | + return Result::eContinue; |
| 163 | + } |
| 164 | + |
| 165 | + Result parse() { |
| 166 | + auto const c = advance(); |
| 167 | + if (c == '-') { |
| 168 | + advance(); |
| 169 | + return parse_word(); |
| 170 | + } |
| 171 | + return parse_singles(); |
| 172 | + } |
| 173 | + |
| 174 | + Result operator()(std::string_view opt_arg) { |
| 175 | + current = opt_arg; |
| 176 | + return parse(); |
| 177 | + } |
| 178 | +}; |
| 179 | + |
| 180 | +} // namespace |
| 181 | + |
| 182 | +auto CliOpts::parse(Spec spec, Ptr<Parser> out, int argc, char const* const* argv) -> Result { |
| 183 | + if (argc < 1) { return Result::eContinue; } |
| 184 | + auto opt_parser = OptParser{std::move(spec), out, argv[0]}; |
| 185 | + auto parse_opt = ParseOpt{opt_parser}; |
| 186 | + for (int i = 1; i < argc; ++i) { |
| 187 | + if (auto const result = parse_opt(argv[i]); result != Result::eContinue) { return result; } |
| 188 | + } |
| 189 | + return Result::eContinue; |
| 190 | +} |
| 191 | + |
| 192 | +auto CliOpts::parse(std::string_view version, int argc, char const* const* argv) -> Result { |
| 193 | + auto spec = Spec{.version = version}; |
| 194 | + return parse(std::move(spec), {}, argc, argv); |
| 195 | +} |
| 196 | +} // namespace facade |
0 commit comments