Skip to content

Commit 3682f60

Browse files
committed
--json-writer arg
1 parent 58c31ac commit 3682f60

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

src/jsontestrunner/main.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
#pragma warning(disable : 4996) // disable fopen deprecation warning
1616
#endif
1717

18+
struct Options
19+
{
20+
std::string path;
21+
Json::Features features;
22+
bool parseOnly;
23+
typedef std::string (*writeFuncType)(Json::Value const&);
24+
writeFuncType write;
25+
};
26+
1827
static std::string normalizeFloatingPointStr(double value) {
1928
char buffer[32];
2029
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
@@ -176,9 +185,10 @@ static std::string useStyledStreamWriter(
176185
static int rewriteValueTree(
177186
const std::string& rewritePath,
178187
const Json::Value& root,
188+
Options::writeFuncType write,
179189
std::string* rewrite)
180190
{
181-
*rewrite = useStyledWriter(root);
191+
*rewrite = write(root);
182192
FILE* fout = fopen(rewritePath.c_str(), "wt");
183193
if (!fout) {
184194
printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
@@ -213,21 +223,16 @@ static int printUsage(const char* argv[]) {
213223
return 3;
214224
}
215225

216-
struct Options
217-
{
218-
std::string path;
219-
Json::Features features;
220-
bool parseOnly;
221-
};
222226
static int parseCommandLine(
223227
int argc, const char* argv[], Options* opts)
224228
{
225229
opts->parseOnly = false;
230+
opts->write = &useStyledWriter;
226231
if (argc < 2) {
227232
return printUsage(argv);
228233
}
229234
int index = 1;
230-
if (std::string(argv[1]) == "--json-checker") {
235+
if (std::string(argv[index]) == "--json-checker") {
231236
opts->features = Json::Features::strictMode();
232237
opts->parseOnly = true;
233238
++index;
@@ -236,6 +241,18 @@ static int parseCommandLine(
236241
printConfig();
237242
return 3;
238243
}
244+
if (std::string(argv[index]) == "--json-writer") {
245+
++index;
246+
std::string const writerName(argv[index++]);
247+
if (writerName == "StyledWriter") {
248+
opts->write = &useStyledWriter;
249+
} else if (writerName == "StyledStreamWriter") {
250+
opts->write = &useStyledStreamWriter;
251+
} else {
252+
printf("Unknown '--json-writer %s'\n", writerName.c_str());
253+
return 4;
254+
}
255+
}
239256
if (index == argc || index + 1 < argc) {
240257
return printUsage(argv);
241258
}
@@ -271,7 +288,7 @@ static int runTest(Options const& opts)
271288
return exitCode;
272289
}
273290
std::string rewrite;
274-
exitCode = rewriteValueTree(rewritePath, root, &rewrite);
291+
exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
275292
if (exitCode) {
276293
return exitCode;
277294
}

0 commit comments

Comments
 (0)