| 1 |
#include <pbcopper/cli2/internal/OptionTranslator.h> |
| 2 |
|
| 3 |
#include <pbcopper/cli2/OptionValue.h> |
| 4 |
#include <pbcopper/json/JSON.h> |
| 5 |
|
| 6 |
#include <sstream> |
| 7 |
#include <stdexcept> |
| 8 |
#include <string> |
| 9 |
#include <unordered_map> |
| 10 |
|
| 11 |
using Json = PacBio::JSON::Json; |
| 12 |
using Option = PacBio::CLI_v2::Option; |
| 13 |
using OptionData = PacBio::CLI_v2::internal::OptionData; |
| 14 |
using OptionValue = PacBio::CLI_v2::OptionValue; |
| 15 |
using OptionValueType = PacBio::CLI_v2::OptionValueType; |
| 16 |
|
| 17 |
namespace { |
| 18 |
|
| 19 |
struct MissingOptionFieldException : public std::runtime_error |
| 20 |
{ |
| 21 |
MissingOptionFieldException(const std::string& field, const std::string& optionText) |
| 22 |
: std::runtime_error("") |
| 23 |
{ |
| 24 |
std::ostringstream msg; |
| 25 |
msg << "[pbcopper] command line option ERROR: required field: \"" << field |
| 26 |
<< "\" is missing or empty in option definition:\n" |
| 27 |
<< optionText << '\n'; |
| 28 |
msg_ = msg.str(); |
| 29 |
} |
| 30 |
|
| 31 |
const char* what() const noexcept override { return msg_.c_str(); } |
| 32 |
|
| 33 |
std::string msg_; |
| 34 |
}; |
| 35 |
|
| 36 |
void Validate(const Option& option, const OptionData& result) |
| 37 |
{ |
| 38 |
// option data fields |
| 39 |
if (result.Names.empty()) { |
| 40 |
throw MissingOptionFieldException{"names", option.text}; |
| 41 |
} |
| 42 |
if (result.Description.empty()) { |
| 43 |
throw MissingOptionFieldException{"description", option.text}; |
| 44 |
} |
| 45 |
|
| 46 |
// ensure defaults match choices |
| 47 |
if (!result.Choices.empty() && result.DefaultValue) { |
| 48 |
|
| 49 |
const auto match = |
| 50 |
std::find(result.Choices.cbegin(), result.Choices.cend(), *result.DefaultValue); |
| 51 |
if (match == result.Choices.cend()) { |
| 52 |
std::ostringstream msg; |
| 53 |
msg << "[pbcopper] command line option ERROR: default value: \""; |
| 54 |
std::visit([&msg](const auto& v) { msg << v; }, *result.DefaultValue); |
| 55 |
msg << "\" is not among valid choices for option: " << result.Names.at(0); |
| 56 |
throw std::runtime_error{msg.str()}; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
} // namespace |
| 62 |
|
| 63 |
namespace PacBio { |
| 64 |
namespace CLI_v2 { |
| 65 |
namespace internal { |
| 66 |
|
| 67 |
OptionValue MakeOptionValue(const Json& value, const OptionValueType type) |
| 68 |
{ |
| 69 |
switch (type) { |
| 70 |
case OptionValueType::INT: { |
| 71 |
const int64_t i = value; |
| 72 |
return OptionValue{i}; |
| 73 |
} |
| 74 |
case OptionValueType::UINT: { |
| 75 |
const uint64_t u = value; |
| 76 |
return OptionValue{u}; |
| 77 |
} |
| 78 |
case OptionValueType::FLOAT: { |
| 79 |
const double d = value; |
| 80 |
return OptionValue{d}; |
| 81 |
} |
| 82 |
case OptionValueType::BOOL: { |
| 83 |
const bool b = value; |
| 84 |
return OptionValue{b}; |
| 85 |
} |
| 86 |
case OptionValueType::STRING: |
| 87 |
case OptionValueType::FILE: |
| 88 |
case OptionValueType::DIR: { |
| 89 |
std::string s = value; |
| 90 |
return OptionValue{std::move(s)}; |
| 91 |
} |
| 92 |
default: { |
| 93 |
assert(false); // unreachable |
| 94 |
throw std::invalid_argument{ |
| 95 |
"[pbcopper] command line option ERROR: unknown or unreachable option type"}; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
std::vector<std::string> OptionTranslator::OptionNames(const Option& option) |
| 101 |
{ |
| 102 |
std::vector<std::string> result; |
| 103 |
const Json root = Json::parse(option.text); |
| 104 |
for (const auto& name : root["names"]) { |
| 105 |
result.push_back(name); |
| 106 |
} |
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
OptionData OptionTranslator::Translate(const Option& option) |
| 111 |
{ |
| 112 |
|
| 113 |
OptionData result; |
| 114 |
|
| 115 |
try { |
| 116 |
const Json root = Json::parse(option.text); |
| 117 |
|
| 118 |
// |
| 119 |
// NOTE: Keep this first. Some other fields depend on it. |
| 120 |
// |
| 121 |
// type |
| 122 |
const auto type = root.find("type"); |
| 123 |
if (type != root.cend()) { |
| 124 |
result.Type = ValueType(*type); |
| 125 |
} |
| 126 |
|
| 127 |
// names |
| 128 |
const auto names = root.find("names"); |
| 129 |
if (names != root.cend()) { |
| 130 |
for (const auto& name : *names) { |
| 131 |
result.Names.push_back(name.get<std::string>()); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// names.hidden |
| 136 |
const auto hiddenNames = root.find("names.hidden"); |
| 137 |
if (hiddenNames != root.cend()) { |
| 138 |
for (const auto& hiddenName : *hiddenNames) { |
| 139 |
result.HiddenNames.push_back(hiddenName.get<std::string>()); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// description - from single string, or joining array of strings |
| 144 |
const auto description = root.find("description"); |
| 145 |
if (description != root.cend()) { |
| 146 |
if (description->is_array()) { |
| 147 |
for (const auto& line : *description) { |
| 148 |
result.Description += line.get<std::string>(); |
| 149 |
} |
| 150 |
} else { |
| 151 |
result.Description = description->get<std::string>(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// required |
| 156 |
const auto required = root.find("required"); |
| 157 |
if (required != root.cend()) { |
| 158 |
result.IsRequired = *required; |
| 159 |
} |
| 160 |
|
| 161 |
// hidden |
| 162 |
const auto hidden = root.find("hidden"); |
| 163 |
if (hidden != root.cend()) { |
| 164 |
result.IsHidden = *hidden; |
| 165 |
} |
| 166 |
|
| 167 |
// choices |
| 168 |
const auto choices = root.find("choices"); |
| 169 |
if (choices != root.cend()) { |
| 170 |
for (const auto& choice : *choices) { |
| 171 |
result.Choices.emplace_back(MakeOptionValue(choice, result.Type)); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// choices.hidden |
| 176 |
const auto hideChoices = root.find("choices.hidden"); |
| 177 |
if (hideChoices != root.cend()) { |
| 178 |
result.IsChoicesHidden = *hideChoices; |
| 179 |
} |
| 180 |
|
| 181 |
// default |
| 182 |
// - direct value supplied to Option ctor |
| 183 |
// - supplied in Option definition text |
| 184 |
// - ok if absent for bool switches, default is OFF |
| 185 |
// - ok if absent for string options, default is "" |
| 186 |
// |
| 187 |
const auto defaultValue = root.find("default"); |
| 188 |
if (option.defaultValue) { |
| 189 |
result.DefaultValue = option.defaultValue; |
| 190 |
} else if (defaultValue != root.cend()) { |
| 191 |
result.DefaultValue = MakeOptionValue(*defaultValue, result.Type); |
| 192 |
} else if (result.Type == OptionValueType::BOOL) { |
| 193 |
result.DefaultValue = false; |
| 194 |
} else if (IsStringLike(result.Type)) { |
| 195 |
result.DefaultValue = std::string{}; |
| 196 |
} |
| 197 |
|
| 198 |
// default.hidden |
| 199 |
const auto hideDefault = root.find("default.hidden"); |
| 200 |
if (hideDefault != root.cend()) { |
| 201 |
result.IsDefaultValueHidden = *hideDefault; |
| 202 |
} |
| 203 |
|
| 204 |
} catch (const std::exception& e) { |
| 205 |
std::ostringstream msg; |
| 206 |
msg << "[pbcopper] command line option ERROR: cannot parse option definition:\n" |
| 207 |
<< option.text << '\n' |
| 208 |
<< "because of JSON error: " << e.what() << '\n'; |
| 209 |
throw std::runtime_error{msg.str()}; |
| 210 |
} |
| 211 |
|
| 212 |
Validate(option, result); |
| 213 |
return result; |
| 214 |
} |
| 215 |
|
| 216 |
std::vector<OptionData> OptionTranslator::Translate(const std::vector<Option>& options) |
| 217 |
{ |
| 218 |
std::vector<OptionData> result; |
| 219 |
for (const auto& opt : options) { |
| 220 |
result.emplace_back(Translate(opt)); |
| 221 |
} |
| 222 |
return result; |
| 223 |
} |
| 224 |
|
| 225 |
} // namespace internal |
| 226 |
} // namespace CLI_v2 |
| 227 |
} // namespace PacBio |