|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "common.h" |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | +#include <set> |
| 9 | +#include <map> |
| 10 | +#include <algorithm> |
| 11 | +#include <memory> |
| 12 | +#include <cassert> |
| 13 | + |
| 14 | +class CMDOptions |
| 15 | +{ |
| 16 | +private: |
| 17 | + std::string usageMessage; |
| 18 | + std::map<std::string, std::string> options; |
| 19 | + |
| 20 | + std::map<std::string, std::string> allowedOptions; |
| 21 | + std::vector<std::string> allowedOptionsOrder; |
| 22 | + std::map<std::string, std::string> defaultValues; |
| 23 | + std::map<std::string, std::vector<std::string> > allowedValues; |
| 24 | + |
| 25 | + CMDOptions(const CMDOptions&); |
| 26 | + CMDOptions& operator = (const CMDOptions&); |
| 27 | + CMDOptions() {} |
| 28 | + |
| 29 | +public: |
| 30 | + static std::unique_ptr<CMDOptions> Create() |
| 31 | + { |
| 32 | + return std::unique_ptr<CMDOptions>(new CMDOptions()); |
| 33 | + } |
| 34 | + |
| 35 | + void Parse(int argc, char **argv) |
| 36 | + { |
| 37 | + for (int i = 1; i < argc; i++) |
| 38 | + { |
| 39 | + std::string s(argv[i]); |
| 40 | + if (s == "/?" || s == "-?" || s == "--help" || s == "-help") |
| 41 | + { |
| 42 | + Usage(argv[0]); |
| 43 | + throw 0; |
| 44 | + } |
| 45 | + |
| 46 | + SetOption(s); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void SetUsageMessage(const std::string& msg) |
| 51 | + { |
| 52 | + usageMessage = msg; |
| 53 | + } |
| 54 | + |
| 55 | + void SetOption(const std::string& s) |
| 56 | + { |
| 57 | + size_t equalIndex = s.find('='); |
| 58 | + std::string name = s.substr(0, equalIndex); |
| 59 | + if (!allowedOptions.count(name)) |
| 60 | + { |
| 61 | + if (equalIndex == std::string::npos && allowedOptions.count("")) |
| 62 | + { |
| 63 | + options[""] = name; |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + UnrecognizedOption(name); |
| 68 | + } |
| 69 | + |
| 70 | + std::string value = (equalIndex == std::string::npos ? "" : s.substr(equalIndex + 1)); |
| 71 | + |
| 72 | + if (!options.count(name) || (defaultValues.count(name) && options[name] == defaultValues[name])) |
| 73 | + options[name] = value; |
| 74 | + |
| 75 | + if (!allowedValues[name].empty() && !count(allowedValues[name].begin(), allowedValues[name].end(), value)) |
| 76 | + InvalidOption(name); |
| 77 | + } |
| 78 | + |
| 79 | + void AddAllowedOption(const std::string& optionName, const std::string& defaultValue, const std::string& description) |
| 80 | + { |
| 81 | + AddAllowedOption(optionName, description); |
| 82 | + options[optionName] = defaultValue; |
| 83 | + defaultValues[optionName] = defaultValue; |
| 84 | + } |
| 85 | + |
| 86 | + void AddAllowedOption(const std::string& optionName, const std::string& description) |
| 87 | + { |
| 88 | + assert(!allowedOptions.count(optionName)); |
| 89 | + allowedOptions[optionName] = description; |
| 90 | + allowedOptionsOrder.push_back(optionName); |
| 91 | + } |
| 92 | + |
| 93 | + void AddAllowedValue(const std::string& optionName, const std::string& value) |
| 94 | + { |
| 95 | + assert(allowedOptions.count(optionName)); |
| 96 | + allowedValues[optionName].push_back(value); |
| 97 | + } |
| 98 | + |
| 99 | + std::string getOption(const std::string& optionName) const |
| 100 | + { |
| 101 | + if (!options.count(optionName)) { |
| 102 | + if (allowedOptions.count(optionName)) UnspecifiedOption(optionName); |
| 103 | + UnrecognizedOption(optionName); |
| 104 | + } |
| 105 | + |
| 106 | + assert(options.count(optionName)); |
| 107 | + return (*options.find(optionName)).second; |
| 108 | + } |
| 109 | + |
| 110 | + void setOption(const std::string& optionName, const std::string& value) |
| 111 | + { |
| 112 | + options[optionName] = value; |
| 113 | + } |
| 114 | + |
| 115 | + std::string getString(const std::string& optionName) const { |
| 116 | + return getOption(optionName); |
| 117 | + } |
| 118 | + |
| 119 | + int getInt(const std::string& optionName) const { |
| 120 | + return to_int(getOption(optionName)); |
| 121 | + } |
| 122 | + |
| 123 | + bool getBool(const std::string& optionName) const { |
| 124 | + return getOption(optionName) != "false"; |
| 125 | + } |
| 126 | + |
| 127 | + void setBool(const std::string& optionName, bool value) { |
| 128 | + if (!options.count(optionName)) { |
| 129 | + UnrecognizedOption(optionName); |
| 130 | + } |
| 131 | + |
| 132 | + assert(options.count(optionName)); |
| 133 | + setOption(optionName, value ? "true" : "false"); |
| 134 | + } |
| 135 | + |
| 136 | + bool hasOption(const std::string& optionName) const |
| 137 | + { |
| 138 | + if (!allowedOptions.count(optionName)) { |
| 139 | + UnrecognizedOption(optionName); |
| 140 | + } |
| 141 | + |
| 142 | + return options.count(optionName) > 0; |
| 143 | + } |
| 144 | + |
| 145 | + void UnspecifiedOption(const std::string& optionName) const |
| 146 | + { |
| 147 | + std::cout << "required option \"" << optionName << "\" is not specified\n"; |
| 148 | + throw 1; |
| 149 | + } |
| 150 | + |
| 151 | + void UnrecognizedOption(const std::string& optionName) const |
| 152 | + { |
| 153 | + std::cout << "unrecognized option \"" << optionName << "\"\n"; |
| 154 | + throw 1; |
| 155 | + } |
| 156 | + |
| 157 | + void InvalidOption(const std::string& optionName) const |
| 158 | + { |
| 159 | + std::cout << "value \"" << getOption(optionName) << "\" is invalid for option \"" << optionName << "\"\n"; |
| 160 | + throw 1; |
| 161 | + } |
| 162 | + |
| 163 | + void Usage(const std::string& program) const |
| 164 | + { |
| 165 | + if (usageMessage != "") |
| 166 | + std::cout << usageMessage << "\n"; |
| 167 | + else |
| 168 | + std::cout << "Usage: " << program << " [options]" << "\n"; |
| 169 | + |
| 170 | + std::cout << "Allowed options:"; |
| 171 | + for (auto opt : allowedOptionsOrder) |
| 172 | + { |
| 173 | + std::string name = allowedOptions.find(opt)->first; |
| 174 | + if (name.length() == 0) continue; |
| 175 | + |
| 176 | + std::cout << "\n"; |
| 177 | + std::cout << " " << name; |
| 178 | + if (allowedValues.count(name)) |
| 179 | + { |
| 180 | + auto av = allowedValues.find(name)->second; |
| 181 | + if (!av.empty()) |
| 182 | + { |
| 183 | + std::cout << "="; |
| 184 | + bool first = true; |
| 185 | + for (std::string s: av) |
| 186 | + if (first) |
| 187 | + { std::cout << "[" << s; first = false; } |
| 188 | + else |
| 189 | + std::cout << "|" << s; |
| 190 | + std::cout << "]"; |
| 191 | + } |
| 192 | + } |
| 193 | + std::cout << "\n"; |
| 194 | + |
| 195 | + std::cout << " " << allowedOptions.find(opt)->second << "\n"; |
| 196 | + } |
| 197 | + } |
| 198 | +}; |
0 commit comments