| 1 |
#include <pbcopper/cli2/internal/PositionalArgumentTranslator.h> |
| 2 |
|
| 3 |
#include <pbcopper/cli2/PositionalArgument.h> |
| 4 |
#include <pbcopper/cli2/internal/PositionalArgumentData.h> |
| 5 |
#include <pbcopper/json/JSON.h> |
| 6 |
|
| 7 |
#include <sstream> |
| 8 |
|
| 9 |
using Json = PacBio::JSON::Json; |
| 10 |
using PositionalArgument = PacBio::CLI_v2::PositionalArgument; |
| 11 |
using PositionalArgumentData = PacBio::CLI_v2::internal::PositionalArgumentData; |
| 12 |
|
| 13 |
namespace { |
| 14 |
|
| 15 |
struct MissingPosArgFieldException : public std::runtime_error |
| 16 |
{ |
| 17 |
MissingPosArgFieldException(const std::string& field, const std::string& posArgText) |
| 18 |
: std::runtime_error("") |
| 19 |
{ |
| 20 |
std::ostringstream msg; |
| 21 |
msg << "[pbcopper] command line argument ERROR: required field: \"" << field |
| 22 |
<< "\" is missing or empty in positional argument definition:\n" |
| 23 |
<< posArgText << '\n'; |
| 24 |
msg_ = msg.str(); |
| 25 |
} |
| 26 |
|
| 27 |
const char* what() const noexcept override { return msg_.c_str(); } |
| 28 |
|
| 29 |
std::string msg_; |
| 30 |
}; |
| 31 |
|
| 32 |
void Validate(const PositionalArgument& posArg, const PositionalArgumentData& result) |
| 33 |
{ |
| 34 |
// required argument data fields |
| 35 |
if (result.Name.empty()) { |
| 36 |
throw MissingPosArgFieldException("name", posArg.text); |
| 37 |
} |
| 38 |
if (result.Description.empty()) { |
| 39 |
throw MissingPosArgFieldException("description", posArg.text); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
} // namespace |
| 44 |
|
| 45 |
namespace PacBio { |
| 46 |
namespace CLI_v2 { |
| 47 |
namespace internal { |
| 48 |
|
| 49 |
std::string PositionalArgumentTranslator::PositionalArgName(const PositionalArgument& posArg) |
| 50 |
{ |
| 51 |
const Json root = Json::parse(posArg.text); |
| 52 |
std::string result = root["name"]; |
| 53 |
return result; |
| 54 |
} |
| 55 |
|
| 56 |
PositionalArgumentData PositionalArgumentTranslator::Translate(const PositionalArgument& posArg) |
| 57 |
{ |
| 58 |
PositionalArgumentData result; |
| 59 |
|
| 60 |
try { |
| 61 |
const Json root = Json::parse(posArg.text); |
| 62 |
|
| 63 |
// name |
| 64 |
auto name = root.find("name"); |
| 65 |
if (name != root.cend()) { |
| 66 |
result.Name = name->get<std::string>(); |
| 67 |
} |
| 68 |
|
| 69 |
// description |
| 70 |
const auto description = root.find("description"); |
| 71 |
if (description != root.cend()) { |
| 72 |
if (description->is_array()) { |
| 73 |
for (const auto& line : *description) { |
| 74 |
result.Description += line.get<std::string>(); |
| 75 |
} |
| 76 |
} else { |
| 77 |
result.Description = description->get<std::string>(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// type |
| 82 |
const auto type = root.find("type"); |
| 83 |
if (type != root.cend()) { |
| 84 |
result.Type = ValueType(type->get<std::string>()); |
| 85 |
} |
| 86 |
|
| 87 |
// required |
| 88 |
const auto required = root.find("required"); |
| 89 |
if (required != root.cend()) { |
| 90 |
result.Required = *required; |
| 91 |
} |
| 92 |
|
| 93 |
} catch (const std::exception& e) { |
| 94 |
std::ostringstream msg; |
| 95 |
msg << "[pbcopper] command line argument ERROR: cannot parse definition:\n" |
| 96 |
<< posArg.text << '\n' |
| 97 |
<< "with JSON error: " << e.what() << '\n'; |
| 98 |
throw std::runtime_error{msg.str()}; |
| 99 |
} |
| 100 |
|
| 101 |
Validate(posArg, result); |
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
std::vector<PositionalArgumentData> PositionalArgumentTranslator::Translate( |
| 106 |
const std::vector<PositionalArgument>& posArgs) |
| 107 |
{ |
| 108 |
std::vector<PositionalArgumentData> result; |
| 109 |
for (const auto& posArg : posArgs) { |
| 110 |
result.emplace_back(Translate(posArg)); |
| 111 |
} |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
} // namespace internal |
| 116 |
} // namespace CLI_v2 |
| 117 |
} // namespace PacBio |