|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 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, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <vector> |
|
20 #include <iostream> |
|
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 in { |
|
30 namespace cli { |
|
31 |
|
32 class CLIParser { |
|
33 private: |
|
34 |
|
35 relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) { |
|
36 if (i < arguments.size()) return arguments[i++]; |
|
37 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); |
|
38 } |
|
39 |
|
40 void readNextRecord(const std::vector<relpipe::writer::string_t>& arguments, int& i, RelationConfiguration& currentRelation) { |
|
41 for (const auto& a : currentRelation.attributes) currentRelation.values.emplace_back(readNext(arguments, i)); |
|
42 } |
|
43 |
|
44 void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
45 if (currentRelation.relation.size()) { |
|
46 c.relationConfigurations.push_back(currentRelation); |
|
47 currentRelation = RelationConfiguration(); |
|
48 } |
|
49 } |
|
50 |
|
51 /** |
|
52 * TODO: use a common method |
|
53 */ |
|
54 bool parseBoolean(const relpipe::writer::string_t& value) { |
|
55 if (value == L"true") return true; |
|
56 else if (value == L"false") return false; |
|
57 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
58 } |
|
59 |
|
60 /** |
|
61 * TODO: use a common method |
|
62 */ |
|
63 relpipe::writer::TypeId parseTypeId(const relpipe::writer::string_t& value) { |
|
64 using t = relpipe::writer::TypeId; |
|
65 if (value == L"string") return t::STRING; |
|
66 else if (value == L"integer") return t::INTEGER; |
|
67 else if (value == L"boolean") return t::BOOLEAN; |
|
68 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
69 } |
|
70 |
|
71 public: |
|
72 |
|
73 static const relpipe::writer::string_t OPTION_RELATION; |
|
74 static const relpipe::writer::string_t OPTION_ATTRIBUTE; |
|
75 static const relpipe::writer::string_t OPTION_RECORD; |
|
76 static const relpipe::writer::string_t OPTION_RECORDS; |
|
77 static const relpipe::writer::string_t OPTION_RECORDS_STDIN; |
|
78 |
|
79 Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) { |
|
80 Configuration c; |
|
81 RelationConfiguration currentRelation; |
|
82 |
|
83 for (int i = 0; i < arguments.size();) { |
|
84 relpipe::writer::string_t option = readNext(arguments, i); |
|
85 |
|
86 if (option == OPTION_RELATION) { |
|
87 addRelation(c, currentRelation); // previous relation |
|
88 currentRelation.relation = readNext(arguments, i); |
|
89 } else if (option == OPTION_ATTRIBUTE) { |
|
90 AttributeRecipe attribute; |
|
91 attribute.name = readNext(arguments, i); |
|
92 attribute.type = parseTypeId(readNext(arguments, i)); |
|
93 currentRelation.attributes.push_back(attribute); |
|
94 } else if (option == OPTION_RECORD) { |
|
95 readNextRecord(arguments, i, currentRelation); |
|
96 } else if (option == OPTION_RECORDS) { |
|
97 while (i < arguments.size()) readNextRecord(arguments, i, currentRelation); |
|
98 } else if (option == OPTION_RECORDS_STDIN) { |
|
99 for (RelationConfiguration r : c.relationConfigurations) if (r.valueStream) throw relpipe::cli::RelpipeCLIException(L"Only one relation can read data from STDIN.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
100 currentRelation.valueStream = &std::cin; |
|
101 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
102 } |
|
103 addRelation(c, currentRelation); // last relation |
|
104 |
|
105 return c; |
|
106 } |
|
107 |
|
108 virtual ~CLIParser() { |
|
109 } |
|
110 }; |
|
111 |
|
112 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation"; |
|
113 const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute"; |
|
114 const relpipe::writer::string_t CLIParser::OPTION_RECORD = L"--record"; |
|
115 const relpipe::writer::string_t CLIParser::OPTION_RECORDS = L"--records"; |
|
116 const relpipe::writer::string_t CLIParser::OPTION_RECORDS_STDIN = L"--records-from-stdin"; |
|
117 |
|
118 } |
|
119 } |
|
120 } |