|
13 | 13 | */
|
14 | 14 |
|
15 | 15 | #include "parser/Parser.h"
|
| 16 | +#include "../tests/cxxopts.hpp" |
| 17 | +#include "absl/strings/string_view.h" |
16 | 18 | #include <iostream>
|
17 | 19 |
|
18 | 20 | class MyParserListener : public sfz::Parser::Listener {
|
@@ -49,18 +51,57 @@ class MyParserListener : public sfz::Parser::Listener {
|
49 | 51 |
|
50 | 52 | int main(int argc, char *argv[])
|
51 | 53 | {
|
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")) { |
53 | 80 | std::cerr << "Please indicate the SFZ file path.\n";
|
54 | 81 | return 1;
|
55 | 82 | }
|
56 | 83 |
|
57 |
| - const fs::path sfzFilePath { argv[1] }; |
| 84 | + const fs::path sfzFilePath { result["input"].as<std::string>() }; |
58 | 85 |
|
59 | 86 | sfz::Parser parser;
|
60 | 87 | MyParserListener listener(parser);
|
61 |
| - |
62 | 88 | 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); |
64 | 105 |
|
65 | 106 | if (parser.getErrorCount() > 0)
|
66 | 107 | return 1;
|
|
0 commit comments