Skip to content

Commit 854bbdb

Browse files
committed
Implement merging
1 parent c371105 commit 854bbdb

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

include/openPMD/cli/convert-toml-json.hpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <iostream>
88
#include <sstream>
9+
#include <stdexcept>
910
#include <string>
1011

1112
namespace from_format_to_format
@@ -58,17 +59,11 @@ struct switch_::other_type<json::SupportedLanguages::TOML>
5859
template <typename FromFormatToFormat>
5960
class convert_json_toml
6061
{
61-
static void with_parsed_cmdline_args(std::string jsonOrToml)
62+
static void
63+
with_parsed_cmdline_args(openPMD::json::ParsedConfig parsed_config)
6264
{
6365
namespace json = openPMD::json;
64-
auto [config, originallySpecifiedAs] = json::parseOptions(
65-
jsonOrToml,
66-
/* considerFiles = */ true,
67-
/* convertLowercase = */ false);
68-
{
69-
// NOLINTNEXTLINE(bugprone-unused-local-non-trivial-variable)
70-
[[maybe_unused]] auto _ = std::move(jsonOrToml);
71-
}
66+
auto [config, originallySpecifiedAs] = std::move(parsed_config);
7267
switch (originallySpecifiedAs)
7368
{
7469
using SL = json::SupportedLanguages;
@@ -83,6 +78,30 @@ class convert_json_toml
8378
}
8479
}
8580

81+
static auto merge(char const **begin, char const **end)
82+
-> openPMD::json::ParsedConfig
83+
{
84+
namespace json = openPMD::json;
85+
if (begin == end)
86+
{
87+
throw std::runtime_error(
88+
"merge: need at least one JSON/TOML file.");
89+
}
90+
auto config = json::parseOptions(
91+
*begin,
92+
/* considerFiles = */ true,
93+
/* convertLowercase = */ false);
94+
for (++begin; begin != end; ++begin)
95+
{
96+
auto [next, _] = json::parseOptions(
97+
*begin,
98+
/* considerFiles = */ true,
99+
/* convertLowercase = */ false);
100+
json::merge_internal(config.config, next, /* do_prune = */ false);
101+
}
102+
return config;
103+
}
104+
86105
public:
87106
static void run_application(
88107
int argc, char const **argv, void (*print_help_message)(char const *))
@@ -101,19 +120,15 @@ class convert_json_toml
101120
jsonOrToml = readEverything.str();
102121
}
103122
break;
104-
case 2:
123+
default:
105124
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
106125
{
107126
print_help_message(argv[1]);
108127
exit(0);
109128
}
110-
jsonOrToml = argv[1];
129+
auto parsed_config = merge(argv + 1, argv + argc);
130+
with_parsed_cmdline_args(std::move(parsed_config));
111131
break;
112-
default:
113-
throw std::runtime_error(
114-
std::string("Usage: ") + argv[0] +
115-
" [file location or inline JSON/TOML]");
116132
}
117-
with_parsed_cmdline_args(std::move(jsonOrToml));
118133
}
119134
};

0 commit comments

Comments
 (0)