|
| 1 | +#include <iomanip> |
| 2 | +#include <sstream> |
| 3 | +#include <stdexcept> |
| 4 | + |
| 5 | +#include "input_parser.hpp" |
| 6 | + |
| 7 | +#include "absl/strings/match.h" |
| 8 | + |
| 9 | +InputParser::InputParser(std::string program_name, std::string description) |
| 10 | + : m_PROGRAM_NAME(std::move(program_name)) |
| 11 | + , m_DESCRIPTION(std::move(description)) {} |
| 12 | + |
| 13 | +auto InputParser::add_option(const Option& opt) -> void { |
| 14 | + if (!opt.short_name.empty() && is_option_registered(opt.short_name)) { |
| 15 | + throw std::invalid_argument("Duplicate short option: " + opt.short_name); |
| 16 | + } |
| 17 | + |
| 18 | + if (!opt.long_name.empty() && is_option_registered(opt.long_name)) { |
| 19 | + throw std::invalid_argument("Duplicate long option: " + opt.long_name); |
| 20 | + } |
| 21 | + |
| 22 | + const size_t IDX = m_OPTIONS.size(); |
| 23 | + m_OPTIONS.push_back(opt); |
| 24 | + |
| 25 | + if (!opt.short_name.empty()) { |
| 26 | + m_SHORT_MAP[opt.short_name] = IDX; |
| 27 | + } |
| 28 | + if (!opt.long_name.empty()) { |
| 29 | + m_LONG_MAP[opt.long_name] = IDX; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +auto InputParser::parse(int argc, char** argv) -> bool { |
| 34 | + reset_state(); |
| 35 | + |
| 36 | + for (int i = 1; i < argc;) { |
| 37 | + const std::string TOKEN = argv[i]; |
| 38 | + bool advance_index = true; |
| 39 | + |
| 40 | + if (is_equals_syntax_option(TOKEN)) { |
| 41 | + handle_equals_syntax(TOKEN); |
| 42 | + } else if (is_regular_option(TOKEN)) { |
| 43 | + advance_index = handle_regular_option(TOKEN, i, argc, argv); |
| 44 | + } else { |
| 45 | + handle_positional_arg(TOKEN); |
| 46 | + } |
| 47 | + |
| 48 | + if (advance_index) { |
| 49 | + ++i; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return m_ERRORS.empty(); |
| 54 | +} |
| 55 | + |
| 56 | +auto InputParser::has_option(const std::string& name) const -> bool { |
| 57 | + const auto IDX = get_option_index(name); |
| 58 | + return IDX && m_PARSED_VALUES.find(*IDX) != m_PARSED_VALUES.end(); |
| 59 | +} |
| 60 | + |
| 61 | +auto InputParser::get_argument(const std::string& name) const -> std::optional<std::string> { |
| 62 | + const auto IDX = get_option_index(name); |
| 63 | + if (!IDX) { |
| 64 | + return std::nullopt; |
| 65 | + } |
| 66 | + |
| 67 | + const auto ITERATOR = m_PARSED_VALUES.find(*IDX); |
| 68 | + if (ITERATOR == m_PARSED_VALUES.end()) { |
| 69 | + return std::nullopt; |
| 70 | + } |
| 71 | + |
| 72 | + return ITERATOR->second; |
| 73 | +} |
| 74 | + |
| 75 | +auto InputParser::get_positional_args() const -> const std::vector<std::string>& { |
| 76 | + return m_POSITIONAL_ARGS; |
| 77 | +} |
| 78 | + |
| 79 | +auto InputParser::get_errors() const -> const std::vector<std::string>& { |
| 80 | + return m_ERRORS; |
| 81 | +} |
| 82 | + |
| 83 | +auto InputParser::generate_help() const -> std::string { |
| 84 | + std::ostringstream oss; |
| 85 | + oss << "Usage: " << m_PROGRAM_NAME << " [options]\n\n"; |
| 86 | + oss << m_DESCRIPTION << "\n\n"; |
| 87 | + oss << "Options:\n"; |
| 88 | + |
| 89 | + constexpr size_t NAME_WIDTH = 30; |
| 90 | + |
| 91 | + for (const auto& opt : m_OPTIONS) { |
| 92 | + std::string name_display; |
| 93 | + if (!opt.short_name.empty() && !opt.long_name.empty()) { |
| 94 | + name_display = opt.short_name + ", " + opt.long_name; |
| 95 | + } else { |
| 96 | + name_display = !opt.short_name.empty() ? opt.short_name : opt.long_name; |
| 97 | + } |
| 98 | + |
| 99 | + if (opt.requires_argument) { |
| 100 | + name_display += " " + opt.arg_placeholder; |
| 101 | + } |
| 102 | + |
| 103 | + oss << " " << std::left << std::setw(NAME_WIDTH) << name_display << " " << opt.description << "\n"; |
| 104 | + } |
| 105 | + |
| 106 | + return oss.str(); |
| 107 | +} |
| 108 | + |
| 109 | +auto InputParser::get_option_index(const std::string& name) const -> std::optional<size_t> { |
| 110 | + // Check exact matches first |
| 111 | + if (auto iter = m_SHORT_MAP.find(name); iter != m_SHORT_MAP.end()) { |
| 112 | + return iter->second; |
| 113 | + } |
| 114 | + if (auto iter = m_LONG_MAP.find(name); iter != m_LONG_MAP.end()) { |
| 115 | + return iter->second; |
| 116 | + } |
| 117 | + |
| 118 | + // Handle automatic conversion between short and long forms |
| 119 | + if (name.size() > 2 && name.substr(0, 2) == "--") { |
| 120 | + const std::string SHORT_FORM = "-" + name.substr(2); |
| 121 | + if (auto iter = m_SHORT_MAP.find(SHORT_FORM); iter != m_SHORT_MAP.end()) { |
| 122 | + return iter->second; |
| 123 | + } |
| 124 | + } else if (name.size() == 2 && name[0] == '-') { |
| 125 | + const std::string LONGFORM = "--" + name.substr(1); |
| 126 | + if (auto iter = m_LONG_MAP.find(LONGFORM); iter != m_LONG_MAP.end()) { |
| 127 | + return iter->second; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return std::nullopt; |
| 132 | +} |
| 133 | + |
| 134 | +auto InputParser::is_option_registered(const std::string& name) const -> bool { |
| 135 | + return m_SHORT_MAP.find(name) != m_SHORT_MAP.end() || m_LONG_MAP.find(name) != m_LONG_MAP.end(); |
| 136 | +} |
| 137 | + |
| 138 | +auto InputParser::reset_state() -> void { |
| 139 | + m_PARSED_VALUES.clear(); |
| 140 | + m_POSITIONAL_ARGS.clear(); |
| 141 | + m_ERRORS.clear(); |
| 142 | +} |
| 143 | + |
| 144 | +auto InputParser::is_equals_syntax_option(const std::string& token) const -> bool { |
| 145 | + return token.size() >= 2 && token.substr(0, 2) == "--" && absl::StrContains(token, '='); |
| 146 | +} |
| 147 | + |
| 148 | +auto InputParser::is_regular_option(const std::string& token) const -> bool { |
| 149 | + return !token.empty() && token[0] == '-'; |
| 150 | +} |
| 151 | + |
| 152 | +auto InputParser::handle_equals_syntax(const std::string& token) -> void { |
| 153 | + const size_t POS = token.find('='); |
| 154 | + const std::string KEY = token.substr(0, POS); |
| 155 | + const std::string VALUE = token.substr(POS + 1); |
| 156 | + |
| 157 | + if (const auto ITER = m_LONG_MAP.find(KEY); ITER != m_LONG_MAP.end()) { |
| 158 | + const auto& option = m_OPTIONS[ITER->second]; |
| 159 | + if (option.requires_argument) { |
| 160 | + m_PARSED_VALUES[ITER->second] = VALUE; |
| 161 | + } else { |
| 162 | + m_ERRORS.push_back("Option " + KEY + " doesn't accept arguments"); |
| 163 | + } |
| 164 | + } else { |
| 165 | + m_ERRORS.push_back("Unknown option: " + KEY); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +auto InputParser::handle_regular_option(const std::string& token, int& index, int argc, char** argv) -> bool { |
| 170 | + std::optional<size_t> idx; |
| 171 | + bool advance_index = true; |
| 172 | + |
| 173 | + if (token.size() >= 2 && token.substr(0, 2) == "--") { |
| 174 | + if (auto iter = m_LONG_MAP.find(token); iter != m_LONG_MAP.end()) { |
| 175 | + idx = iter->second; |
| 176 | + } |
| 177 | + } else { |
| 178 | + if (auto iter = m_SHORT_MAP.find(token); iter != m_SHORT_MAP.end()) { |
| 179 | + idx = iter->second; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (!idx) { |
| 184 | + m_ERRORS.push_back("Unknown option: " + token); |
| 185 | + return advance_index; |
| 186 | + } |
| 187 | + |
| 188 | + const auto& option = m_OPTIONS[*idx]; |
| 189 | + if (option.requires_argument) { |
| 190 | + if (index + 1 >= argc) { |
| 191 | + m_ERRORS.push_back("Missing argument for: " + token); |
| 192 | + } else { |
| 193 | + m_PARSED_VALUES[*idx] = argv[++index]; |
| 194 | + advance_index = false; // Already advanced index |
| 195 | + } |
| 196 | + } else { |
| 197 | + m_PARSED_VALUES[*idx] = ""; |
| 198 | + } |
| 199 | + |
| 200 | + return advance_index; |
| 201 | +} |
| 202 | + |
| 203 | +auto InputParser::handle_positional_arg(const std::string& token) -> void { |
| 204 | + m_POSITIONAL_ARGS.push_back(token); |
| 205 | +} |
0 commit comments