/// Minimal CLI: `cepis-lookup 01001000`. /// /// It exists for one concrete case — quickly confirming a local instance /// answers before pointing code at it: /// /// ```sh /// cepis-lookup --base-url http://localhost:8080 01001000 /// ``` /// /// A single CEP goes through the unit lookup, which carries `geo`; several go /// through the batch, which is one request instead of N and has no `geo`. /// /// It is not the library's main interface; for that, include ``. #include #include #include #include #include #include #include "cepis/cepis.hpp" namespace { cepis::Json to_json(const cepis::Address& address) { cepis::Json object = address.raw; if (!object.is_object()) { object = cepis::Json::object(); } return object; } void usage() { std::cerr << "usage: cepis-lookup [--base-url URL] [--timeout SECONDS] CEP [CEP...]\n"; } } // namespace int main(int argc, char** argv) { cepis::Config config; std::vector ceps; for (int i = 1; i < argc; ++i) { const std::string_view argument = argv[i]; if (argument == "--base-url" && i + 1 < argc) { config.base_url = argv[++i]; } else if (argument == "--timeout" && i + 1 < argc) { config.timeout = std::chrono::milliseconds(static_cast(std::stod(argv[++i]) * 1000)); } else if (argument == "-h" || argument == "--help") { usage(); return 0; } else if (!argument.empty() && argument.front() == '-') { usage(); return 2; } else { ceps.emplace_back(argument); } } if (ceps.empty()) { usage(); return 2; } try { const cepis::Client client(config); if (ceps.size() == 1) { std::cout << to_json(client.lookup(ceps.front())).dump() << '\n'; return 0; } cepis::Json output = cepis::Json::array(); for (const std::optional& address : client.lookup_many(ceps)) { output.push_back(address ? to_json(*address) : cepis::Json{}); } std::cout << output.dump() << '\n'; return 0; } catch (const cepis::Error& error) { std::cerr << "error: " << error.what() << '\n'; return 1; } }