Skip to content

Commit e1387dc

Browse files
option to add multiple shared memories
1 parent 4f012f9 commit e1387dc

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

src/main.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ auto main(int argc, char **argv) -> int {
4343

4444
options.add_options("shared memory")("shmname", "name of the shared memory to dump", cxxopts::value<std::string>());
4545
options.add_options("shared memory")("configfile", "config file", cxxopts::value<std::string>());
46-
options.parse_positional({"shmname", "configfile"});
46+
options.add_options("shared memory")("shmnames_configfiles",
47+
"additional shared memories and config files",
48+
cxxopts::value<std::vector<std::string>>());
49+
options.parse_positional({"shmname", "configfile", "shmnames_configfiles"});
4750

48-
options.positional_help("SHM_NAME CFG_FILE");
51+
options.positional_help("SHM_NAME CFG_FILE [SHM_NAME CFG_FILE ...]");
4952

5053
// parse arguments
5154
cxxopts::ParseResult opts;
@@ -142,6 +145,15 @@ auto main(int argc, char **argv) -> int {
142145
return EX_USAGE;
143146
}
144147

148+
std::vector<std::string> additional_shm_configs;
149+
if (opts.count("shmnames_configfiles")) {
150+
additional_shm_configs = opts["shmnames_configfiles"].as<std::vector<std::string>>();
151+
if (additional_shm_configs.size() % 2 != 0) {
152+
std::cerr << "no config file specified for shared memory \"" << additional_shm_configs.back() << "\"\n";
153+
return EX_USAGE;
154+
}
155+
}
156+
145157
static volatile bool terminate = false;
146158
auto sig_term_handler = [](int) { terminate = true; };
147159

@@ -162,16 +174,39 @@ auto main(int argc, char **argv) -> int {
162174
}
163175

164176
// create SHM_Format instance(s)
177+
std::unordered_set<std::string> shm_names;
178+
165179
std::vector<std::unique_ptr<SHM_Format>> shm_format;
166180
try {
167181
const std::string &shm_name = opts["shmname"].as<std::string>();
168182
const std::string &cfg_file_path = opts["configfile"].as<std::string>();
169183
shm_format.emplace_back(std::make_unique<SHM_Format>(shm_name, cfg_file_path));
184+
shm_names.emplace(shm_name);
170185
} catch (const std::exception &e) {
171186
std::cerr << e.what() << '\n';
172187
return EX_SOFTWARE;
173188
}
174189

190+
if (!additional_shm_configs.empty()) {
191+
// length is multiple of 2; checked above
192+
for (std::size_t i = 0; i < additional_shm_configs.size(); i += 2) {
193+
try {
194+
const std::string &shm_name = additional_shm_configs.at(i);
195+
const std::string &cfg_file_path = additional_shm_configs.at(i + 1);
196+
197+
if (shm_names.contains(shm_name)) {
198+
std::cerr << "duplicate shared memory: " << shm_name << '\n';
199+
return EX_USAGE;
200+
}
201+
202+
shm_format.emplace_back(std::make_unique<SHM_Format>(shm_name, cfg_file_path));
203+
} catch (const std::exception &e) {
204+
std::cerr << e.what() << '\n';
205+
return EX_SOFTWARE;
206+
}
207+
}
208+
}
209+
175210
// output data
176211
auto execute = [&]() {
177212
nlohmann::json result_json;

0 commit comments

Comments
 (0)