-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmain.cc
More file actions
267 lines (225 loc) · 10.1 KB
/
Copy pathmain.cc
File metadata and controls
267 lines (225 loc) · 10.1 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <sourcemeta/core/jsonschema.h>
#include <sourcemeta/core/options.h>
#include <cstdlib> // EXIT_SUCCESS
#include <filesystem> // std::filesystem
#include <print> // std::print, std::println
#include <string> // std::string
#include <string_view> // std::string_view
#include "command.h"
#include "configure.h"
#include "error.h"
#include "utils.h"
constexpr std::string_view USAGE_DETAILS{R"EOF(
Global Options:
--verbose, -v Enable verbose output
--debug, -g Enable even higher verbose output
--resolve, -r Import the given JSON Schema (or directory of schemas)
into the resolution context
--default-dialect, -d <uri> Specify the URI for the default dialect to be used
if the `$schema` keyword is not set
--json, -j Prefer JSON output if supported
--http, -h Allow network access to resolve remote schemas
Commands:
version / --version / -v
Print the current version of the JSON Schema CLI.
help / --help / -h
Print this command reference help.
validate <schema.json|.yaml> <instance.json|.jsonl|.yaml|directory...>
[--benchmark/-b] [--loop <iterations>] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--trace/-t] [--fast/-f]
[--template/-m <template.json>] [--entrypoint/-p <pointer|uri>]
[--path/-P <pointer>]
Validate one or more instances against the given schema.
By default, schemas are validated in exhaustive mode, which results in
better error messages, at the expense of speed. The --fast/-f option
makes the schema compiler optimise for speed, at the expense of error
messages. Looping in benchmark mode allows to collect the execution time
average and standard deviation over multiple runs.
You may additionally pass a pre-compiled schema template (see the
`compile` command). However, you still need to pass the original schema
for error reporting purposes. Make sure they match or you will get
non-sense results.
Use --path/-P to validate against a sub-schema by JSON Pointer.
metaschema [schemas-or-directories...] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--trace/-t]
Validate that a schema or a set of schemas are valid with respect
to their metaschemas.
compile <schema.json|.yaml> [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--fast/-f] [--minify/-m]
[--include/-n <name>] [--entrypoint/-p <pointer|uri>]
Compile the given schema into an internal optimised representation.
Use --include/-n to output as a C/C++ header file.
Use --entrypoint/-p to compile a subschema by JSON Pointer or URI.
test [schemas-or-directories...] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>]
Run a set of unit tests against a schema.
Pass --json/-j to output results in CTRF format (https://ctrf.io).
fmt [schemas-or-directories...] [--check/-c] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--keep-ordering/-k]
[--indentation/-n <spaces>]
Format the input schemas in-place or check they are formatted.
This command does not support YAML schemas yet.
lint [schemas-or-directories...] [--fix/-f] [--format/-m]
[--keep-ordering/-k] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--exclude/-x <rule-name>]
[--only/-o <rule-name>] [--list/-l] [--indentation/-n <spaces>]
[--rule/-a <rule-schema>]
Lint the input schemas and potentially fix the reported issues.
The --fix/-f option is not supported when passing YAML schemas.
Use --format/-m with --fix to format the output even when there
are no linting issues.
Use --keep-ordering/-k with --format to preserve key order.
Use --list/-l to print a summary of all enabled rules.
Use --rule/-a to add a custom lint rule defined as a JSON Schema.
bundle <schema.json|.yaml> [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--without-id/-w]
Perform JSON Schema Bundling on a schema to inline remote references,
printing the result to standard output.
inspect <schema.json|.yaml>
Statically inspect a schema to display schema locations and
references in a human-readable manner.
canonicalize <schema.json|.yaml>
Transform a JSON Schema into a canonical normalized form.
Canonicalization is a process that simplifies a schema into a
more verbose but semantically equivalent representation, making
it easier for static analysis.
codegen <schema.json|.yaml> --target/-t <target> [--name/-n <name>]
Generate code from a JSON Schema. Currently only supports
--target/-t set to typescript and JSON Schema 2020-12.
encode <document.json|.jsonl> <output.binpack>
Encode a JSON document or JSONL dataset using JSON BinPack.
decode <output.binpack> <output.json|.jsonl>
Decode a JSON document or JSONL dataset using JSON BinPack.
install [<uri> <path>] [--force/-f] [--frozen/-z]
Fetch and install external schema dependencies declared in
jsonschema.json. Pass a URI and a local file path to add a new
dependency before installing. Pass --force/-f to re-fetch all
dependencies regardless of lock state. Pass --frozen to strictly
verify dependencies against the lock file without modifying it,
intended for CI/CD environments.
For more documentation, visit https://github.com/sourcemeta/jsonschema
)EOF"};
auto jsonschema_main(const std::string &program, const std::string &command,
sourcemeta::core::Options &app, int argc, char *argv[])
-> int {
if (command == "fmt") {
app.flag("check", {"c"});
app.flag("keep-ordering", {"k"});
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.option("indentation", {"n"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::fmt(app);
return EXIT_SUCCESS;
} else if (command == "inspect") {
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::inspect(app);
return EXIT_SUCCESS;
} else if (command == "bundle") {
app.flag("without-id", {"w"});
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::bundle(app);
return EXIT_SUCCESS;
} else if (command == "lint") {
app.flag("fix", {"f"});
app.flag("format", {"m"});
app.flag("keep-ordering", {"k"});
app.flag("list", {"l"});
app.option("extension", {"e"});
app.option("exclude", {"x"});
app.option("only", {"o"});
app.option("ignore", {"i"});
app.option("indentation", {"n"});
app.option("rule", {"a"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::lint(app);
return EXIT_SUCCESS;
} else if (command == "validate") {
app.flag("benchmark", {"b"});
app.flag("trace", {"t"});
app.flag("fast", {"f"});
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.option("template", {"m"});
app.option("loop", {"l"});
app.option("entrypoint", {"p"});
app.option("path", {"P"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::validate(app);
return EXIT_SUCCESS;
} else if (command == "metaschema") {
app.flag("trace", {"t"});
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::metaschema(app);
return EXIT_SUCCESS;
} else if (command == "compile") {
app.flag("fast", {"f"});
app.flag("minify", {"m"});
app.option("include", {"n"});
app.option("entrypoint", {"p"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::compile(app);
return EXIT_SUCCESS;
} else if (command == "canonicalize") {
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::canonicalize(app);
return EXIT_SUCCESS;
} else if (command == "test") {
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::test(app);
return EXIT_SUCCESS;
} else if (command == "encode") {
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::encode(app);
return EXIT_SUCCESS;
} else if (command == "decode") {
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::decode(app);
return EXIT_SUCCESS;
} else if (command == "codegen") {
app.option("name", {"n"});
app.option("target", {"t"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::codegen(app);
return EXIT_SUCCESS;
} else if (command == "install") {
app.flag("force", {"f"});
app.flag("frozen", {"z"});
app.parse(argc, argv, {.skip = 1});
sourcemeta::jsonschema::install(app);
return EXIT_SUCCESS;
} else if (command == "help" || command == "--help" || command == "-h") {
std::println("JSON Schema CLI - v{}",
sourcemeta::jsonschema::PROJECT_VERSION);
std::println("Usage: {} <command> [arguments...]",
std::filesystem::path{program}.filename().string());
std::print("{}", USAGE_DETAILS);
return EXIT_SUCCESS;
} else if (command == "version" || command == "--version" ||
command == "-v") {
std::println("{}", sourcemeta::jsonschema::PROJECT_VERSION);
return EXIT_SUCCESS;
} else {
throw sourcemeta::jsonschema::UnknownCommandError{command};
}
}
auto main(int argc, char *argv[]) noexcept -> int {
sourcemeta::core::Options app;
app.flag("http", {"h"});
app.flag("verbose", {"v"});
app.flag("debug", {"g"});
app.flag("json", {"j"});
app.option("resolve", {"r"});
app.option("default-dialect", {"d"});
return sourcemeta::jsonschema::try_catch(app, [&app, argc, &argv]() {
const std::string program{argv[0]};
const std::string command{argc > 1 ? argv[1] : "help"};
return jsonschema_main(program, command, app, argc, argv);
});
}