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