|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <vector> |
|
21 |
|
22 #include <relpipe/writer/typedefs.h> |
|
23 #include <relpipe/cli/CLI.h> |
|
24 #include <relpipe/cli/RelpipeCLIException.h> |
|
25 |
|
26 #include "Configuration.h" |
|
27 |
|
28 namespace relpipe { |
|
29 namespace tr { |
|
30 namespace guile { |
|
31 |
|
32 class CLIParser { |
|
33 private: |
|
34 |
|
35 // TODO: move common methods/classes to relpipe-lib-cli |
|
36 |
|
37 string_t readNext(std::vector<string_t> arguments, int& i) { |
|
38 if (i < arguments.size()) return arguments[i++]; |
|
39 else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
40 } |
|
41 |
|
42 void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
43 if (currentRelation.relation.size()) { |
|
44 c.relationConfigurations.push_back(currentRelation); |
|
45 currentRelation = RelationConfiguration(); |
|
46 } |
|
47 } |
|
48 |
|
49 bool parseBoolean(const string_t& value, const string_t& optionName) { |
|
50 if (value == L"true") return true; |
|
51 else if (value == L"false") return false; |
|
52 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value of option: " + optionName + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
53 } |
|
54 |
|
55 public: |
|
56 |
|
57 static const string_t OPTION_RELATION; |
|
58 static const string_t OPTION_BEFORE_RECORDS; |
|
59 static const string_t OPTION_AFTER_RECORDS; |
|
60 static const string_t OPTION_FOR_EACH; |
|
61 static const string_t OPTION_WHERE; |
|
62 static const string_t OPTION_DROP; |
|
63 static const string_t OPTION_DEFINE; |
|
64 |
|
65 Configuration parse(const std::vector<string_t>& arguments) { |
|
66 Configuration c; |
|
67 RelationConfiguration currentRelation; |
|
68 |
|
69 { |
|
70 for (int i = 0; i < arguments.size();) { |
|
71 string_t option = readNext(arguments, i); |
|
72 |
|
73 if (option == OPTION_BEFORE_RECORDS) currentRelation.guileBeforeRecords = readNext(arguments, i); |
|
74 else if (option == OPTION_AFTER_RECORDS) currentRelation.guileAfterRecords = readNext(arguments, i); |
|
75 else if (option == OPTION_FOR_EACH) currentRelation.guileForEach = readNext(arguments, i); |
|
76 else if (option == OPTION_WHERE) currentRelation.guileWhere = readNext(arguments, i); |
|
77 else if (option == OPTION_DROP) currentRelation.drop = parseBoolean(readNext(arguments, i), option); |
|
78 else if (option == OPTION_RELATION) { |
|
79 addRelation(c, currentRelation); // previous relation |
|
80 currentRelation.relation = readNext(arguments, i); |
|
81 } else if (option == OPTION_DEFINE) { |
|
82 DefinitionRecipe definition; |
|
83 definition.name = readNext(arguments, i); |
|
84 definition.type = readNext(arguments, i); |
|
85 definition.value = readNext(arguments, i); |
|
86 currentRelation.definitions.push_back(definition); |
|
87 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
88 |
|
89 } |
|
90 |
|
91 addRelation(c, currentRelation); // last relation |
|
92 } |
|
93 |
|
94 return c; |
|
95 } |
|
96 |
|
97 virtual ~CLIParser() { |
|
98 } |
|
99 }; |
|
100 |
|
101 const string_t CLIParser::OPTION_RELATION = L"--relation"; |
|
102 const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records"; |
|
103 const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records"; |
|
104 const string_t CLIParser::OPTION_FOR_EACH = L"--for-each"; |
|
105 const string_t CLIParser::OPTION_WHERE = L"--where"; |
|
106 const string_t CLIParser::OPTION_DROP = L"--drop"; |
|
107 const string_t CLIParser::OPTION_DEFINE = L"--define"; |
|
108 |
|
109 } |
|
110 } |
|
111 } |