Skip to content

Commit 19fc5d6

Browse files
committed
Add option handling, and external definitions
1 parent bb9d6cf commit 19fc5d6

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

devtools/Preprocessor.cpp

+45-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414

1515
#include "parser/Parser.h"
16+
#include "../tests/cxxopts.hpp"
17+
#include "absl/strings/string_view.h"
1618
#include <iostream>
1719

1820
class MyParserListener : public sfz::Parser::Listener {
@@ -49,18 +51,57 @@ class MyParserListener : public sfz::Parser::Listener {
4951

5052
int main(int argc, char *argv[])
5153
{
52-
if (argc != 2) {
54+
cxxopts::Options options("sfizz_preprocess", "Preprocess SFZ files");
55+
56+
options.positional_help("<sfz-file>");
57+
58+
options.add_options()
59+
("D,define", "Add external definition", cxxopts::value<std::vector<std::string>>())
60+
("i,input", "Input SFZ file", cxxopts::value<std::string>())
61+
("h,help", "Print usage");
62+
63+
options.parse_positional({"input"});
64+
65+
std::unique_ptr<cxxopts::ParseResult> resultPtr;
66+
try {
67+
resultPtr = absl::make_unique<cxxopts::ParseResult>(options.parse(argc, argv));
68+
} catch (cxxopts::OptionException& ex) {
69+
std::cerr << ex.what() << "\n";
70+
return 1;
71+
}
72+
cxxopts::ParseResult& result = *resultPtr;
73+
74+
if (result.count("help")) {
75+
std::cerr << options.help() << "\n";
76+
return 0;
77+
}
78+
79+
if (!result.count("input")) {
5380
std::cerr << "Please indicate the SFZ file path.\n";
5481
return 1;
5582
}
5683

57-
const fs::path sfzFilePath { argv[1] };
84+
const fs::path sfzFilePath { result["input"].as<std::string>() };
5885

5986
sfz::Parser parser;
6087
MyParserListener listener(parser);
61-
6288
parser.setListener(&listener);
63-
parser.parseFile(argv[1]);
89+
90+
if (result.count("define")) {
91+
auto& definitions = result["define"].as<std::vector<std::string>>();
92+
for (absl::string_view definition : definitions) {
93+
size_t pos = definition.find('=');
94+
if (pos == definition.npos) {
95+
std::cerr << "The definition is malformed, should be key=value.\n";
96+
return 1;
97+
}
98+
absl::string_view key = definition.substr(0, pos);
99+
absl::string_view val = definition.substr(pos + 1);
100+
parser.addExternalDefinition(key, val);
101+
}
102+
}
103+
104+
parser.parseFile(sfzFilePath);
64105

65106
if (parser.getErrorCount() > 0)
66107
return 1;

0 commit comments

Comments
 (0)