| 1 |
#include <pbcopper/cli2/OptionValue.h> |
| 2 |
|
| 3 |
#include <boost/algorithm/string.hpp> |
| 4 |
|
| 5 |
#include <ostream> |
| 6 |
#include <sstream> |
| 7 |
#include <stdexcept> |
| 8 |
#include <unordered_map> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
namespace PacBio { |
| 12 |
namespace CLI_v2 { |
| 13 |
namespace { |
| 14 |
|
| 15 |
class BoolConverter |
| 16 |
{ |
| 17 |
public: |
| 18 |
bool operator()(bool x) const noexcept { return x; } |
| 19 |
|
| 20 |
// anything else always throws |
| 21 |
template <typename T> |
| 22 |
bool operator()(const T&) const |
| 23 |
{ |
| 24 |
throw std::runtime_error{ |
| 25 |
"[pbcopper] command line option ERROR: cannot convert value to bool"}; |
| 26 |
return 0; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
class DoubleConverter |
| 31 |
{ |
| 32 |
public: |
| 33 |
double operator()(double x) const noexcept { return x; } |
| 34 |
|
| 35 |
// anything else always throws |
| 36 |
template <typename T> |
| 37 |
double operator()(const T&) const |
| 38 |
{ |
| 39 |
throw std::runtime_error{ |
| 40 |
"[pbcopper] command line option ERROR: cannot convert value to double"}; |
| 41 |
return 0; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
class Int64Converter |
| 46 |
{ |
| 47 |
public: |
| 48 |
int64_t operator()(int8_t x) const noexcept { return x; } |
| 49 |
int64_t operator()(int16_t x) const noexcept { return x; } |
| 50 |
int64_t operator()(int32_t x) const noexcept { return x; } |
| 51 |
int64_t operator()(int64_t x) const noexcept { return x; } |
| 52 |
|
| 53 |
// anything else always throws |
| 54 |
template <typename T> |
| 55 |
int64_t operator()(const T&) const |
| 56 |
{ |
| 57 |
throw std::runtime_error{ |
| 58 |
"[pbcopper] command line option ERROR: cannot convert value to integer"}; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
class UInt64Converter |
| 64 |
{ |
| 65 |
public: |
| 66 |
uint64_t operator()(uint8_t x) const noexcept { return x; } |
| 67 |
uint64_t operator()(uint16_t x) const noexcept { return x; } |
| 68 |
uint64_t operator()(uint32_t x) const noexcept { return x; } |
| 69 |
uint64_t operator()(uint64_t x) const noexcept { return x; } |
| 70 |
|
| 71 |
// anything else always throws |
| 72 |
template <typename T> |
| 73 |
uint64_t operator()(const T&) const |
| 74 |
{ |
| 75 |
throw std::runtime_error{ |
| 76 |
"[pbcopper] command line option ERROR: cannot convert value to unsigned integer"}; |
| 77 |
return 0; |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
class StringConverter |
| 82 |
{ |
| 83 |
public: |
| 84 |
std::string operator()(std::string x) const { return x; } |
| 85 |
|
| 86 |
// anything else always throws |
| 87 |
template <typename T> |
| 88 |
std::string operator()(const T&) const |
| 89 |
{ |
| 90 |
throw std::runtime_error{ |
| 91 |
"[pbcopper] command line option ERROR: cannot convert value to string"}; |
| 92 |
return 0; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
// clang-format off |
| 97 |
const std::unordered_map<std::string, OptionValueType> stringToType{ |
| 98 |
{"int", OptionValueType::INT}, {"integer", OptionValueType::INT}, |
| 99 |
{"uint", OptionValueType::UINT}, {"unsigned", OptionValueType::UINT}, |
| 100 |
{"unsigned int", OptionValueType::UINT}, {"unsigned integer", OptionValueType::UINT}, |
| 101 |
{"string", OptionValueType::STRING}, {"file", OptionValueType::FILE}, |
| 102 |
{"dir", OptionValueType::DIR}, {"float", OptionValueType::FLOAT}, |
| 103 |
{"double", OptionValueType::FLOAT}, {"bool", OptionValueType::BOOL}, |
| 104 |
{"boolean", OptionValueType::BOOL} |
| 105 |
}; |
| 106 |
|
| 107 |
const std::vector<std::string> typeToString{ |
| 108 |
// index follows our std::variant |
| 109 |
"int8_t", "int16_t", "int32_t", "int64_t", |
| 110 |
"uint8_t", "uint16_t", "uint32_t", "uint64_t", |
| 111 |
"double", "bool", "string" |
| 112 |
}; |
| 113 |
// clang-format on |
| 114 |
|
| 115 |
} // namespace |
| 116 |
|
| 117 |
OptionValueType ValueType(std::string typeString) |
| 118 |
{ |
| 119 |
boost::to_lower(typeString); |
| 120 |
const auto found = stringToType.find(typeString); |
| 121 |
if (found != stringToType.cend()) { |
| 122 |
return found->second; |
| 123 |
} else { |
| 124 |
std::ostringstream msg; |
| 125 |
msg << "[pbcopper] command line option ERROR: could not determine value type for \"" |
| 126 |
<< typeString << "\"\n"; |
| 127 |
throw std::runtime_error{msg.str()}; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
bool OptionValueToBool(const OptionValue& value) { return std::visit(BoolConverter{}, value); } |
| 132 |
|
| 133 |
double OptionValueToDouble(const OptionValue& value) |
| 134 |
{ |
| 135 |
return std::visit(DoubleConverter{}, value); |
| 136 |
} |
| 137 |
|
| 138 |
int64_t OptionValueToInt(const OptionValue& value) { return std::visit(Int64Converter{}, value); } |
| 139 |
|
| 140 |
std::string OptionValueToString(const OptionValue& value) |
| 141 |
{ |
| 142 |
return std::visit(StringConverter{}, value); |
| 143 |
} |
| 144 |
|
| 145 |
uint64_t OptionValueToUInt(const OptionValue& value) |
| 146 |
{ |
| 147 |
return std::visit(UInt64Converter{}, value); |
| 148 |
} |
| 149 |
|
| 150 |
std::ostream& operator<<(std::ostream& out, const OptionValue& value) |
| 151 |
{ |
| 152 |
const auto typeIndex = value.index(); |
| 153 |
switch (typeIndex) { |
| 154 |
// int8_t, int16_t, int32_t, int64_t |
| 155 |
case 0: |
| 156 |
case 1: |
| 157 |
case 2: |
| 158 |
case 3: { |
| 159 |
out << OptionValueToInt(value); |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
// uint8_t, uint16_t, uint32_t, uint64_t |
| 164 |
case 4: |
| 165 |
case 5: |
| 166 |
case 6: |
| 167 |
case 7: { |
| 168 |
out << OptionValueToUInt(value); |
| 169 |
break; |
| 170 |
} |
| 171 |
|
| 172 |
// double |
| 173 |
case 8: { |
| 174 |
out << OptionValueToDouble(value); |
| 175 |
break; |
| 176 |
} |
| 177 |
|
| 178 |
// bool |
| 179 |
case 9: { |
| 180 |
const bool ok = OptionValueToBool(value); |
| 181 |
out << (ok ? "true" : "false"); |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
// string |
| 186 |
case 10: { |
| 187 |
const std::string s = OptionValueToString(value); |
| 188 |
out << s; |
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
default: { |
| 193 |
std::ostringstream msg; |
| 194 |
msg << "[pbcopper] command line option ERROR: cannot print value for option of type: \"" |
| 195 |
<< typeToString.at(typeIndex) << "\"\n"; |
| 196 |
throw std::runtime_error{msg.str()}; |
| 197 |
} |
| 198 |
} |
| 199 |
return out; |
| 200 |
} |
| 201 |
|
| 202 |
} // namespace CLI_v2 |
| 203 |
} // namespace PacBio |