-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathcli.cpp
67 lines (59 loc) · 1.5 KB
/
cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fstream>
#include "nlohmann/json-schema.hpp"
using namespace nlohmann;
using namespace nlohmann::json_schema;
class main_cli : public CLI::App
{
std::string version;
std::ifstream schema_input;
std::filesystem::path object_path;
// TODO: Export this as a built-in loader
void loader(const json_uri &uri, json &sch)
{
std::string filename = object_path.parent_path().append(uri.path());
std::ifstream lf(filename);
if (!lf.good())
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
try {
lf >> sch;
} catch (const std::exception &e) {
throw e;
}
}
public:
json schema;
json object;
json_validator validator;
main_cli()
: CLI::App{"Json schema validator", "json-validator"},
validator{
[this](const json_uri &u, json &s) { this->loader(u, s); },
default_string_format_check},
version{nlohmann::json_schema::version}
{
set_version_flag("--version", version);
add_option("schema", schema_input, "JSON schema of the object")
->check(CLI::ExistingFile);
add_option("object", object_path, "JSON object to validate")
->check(CLI::ExistingFile);
}
void validate()
{
validator.set_root_schema(schema);
validator.validate(object);
}
};
int main(int argc, char *argv[])
{
main_cli app{};
try {
app.parse(argc, argv);
} catch (const CLI::ParseError &e) {
return app.exit(e);
}
app.validate();
return 0;
}