0
|
1 |
/**
|
|
2 |
* Relational pipes
|
|
3 |
* Copyright © 2021 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/common/type/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 x11 {
|
|
31 |
|
|
32 |
class CLIParser {
|
|
33 |
private:
|
|
34 |
|
|
35 |
relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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::common::type::StringX>& 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::common::type::StringX& 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::common::type::StringX& 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::common::type::StringX OPTION_RELATION;
|
|
74 |
static const relpipe::common::type::StringX OPTION_WRITE_HEADER;
|
|
75 |
static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
|
|
76 |
static const relpipe::common::type::StringX OPTION_RECORD;
|
|
77 |
static const relpipe::common::type::StringX OPTION_RECORDS;
|
|
78 |
static const relpipe::common::type::StringX OPTION_RECORDS_STDIN;
|
|
79 |
|
|
80 |
Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
|
|
81 |
Configuration c;
|
|
82 |
RelationConfiguration currentRelation;
|
|
83 |
|
|
84 |
for (int i = 0; i < arguments.size();) {
|
|
85 |
relpipe::common::type::StringX option = readNext(arguments, i);
|
|
86 |
|
|
87 |
if (option == OPTION_RELATION) {
|
|
88 |
addRelation(c, currentRelation); // previous relation
|
|
89 |
currentRelation.relation = readNext(arguments, i);
|
|
90 |
} else if (option == OPTION_WRITE_HEADER) {
|
|
91 |
currentRelation.writeHeader = parseBoolean(readNext(arguments, i));
|
|
92 |
} else if (option == OPTION_ATTRIBUTE) {
|
|
93 |
AttributeRecipe attribute;
|
|
94 |
attribute.name = readNext(arguments, i);
|
|
95 |
attribute.type = parseTypeId(readNext(arguments, i));
|
|
96 |
currentRelation.attributes.push_back(attribute);
|
|
97 |
} else if (option == OPTION_RECORD) {
|
|
98 |
readNextRecord(arguments, i, currentRelation);
|
|
99 |
} else if (option == OPTION_RECORDS) {
|
|
100 |
while (i < arguments.size()) readNextRecord(arguments, i, currentRelation);
|
|
101 |
} else if (option == OPTION_RECORDS_STDIN) {
|
|
102 |
if (parseBoolean(readNext(arguments, i))) {
|
|
103 |
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);
|
|
104 |
currentRelation.valueStream = &std::cin;
|
|
105 |
}
|
|
106 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
107 |
}
|
|
108 |
addRelation(c, currentRelation); // last relation
|
|
109 |
|
|
110 |
if (c.relationConfigurations.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one relation. Use the " + OPTION_RELATION + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
111 |
for (auto& rc : c.relationConfigurations) if (rc.attributes.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one attribute. Use the " + OPTION_ATTRIBUTE + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
112 |
|
|
113 |
return c;
|
|
114 |
}
|
|
115 |
|
|
116 |
virtual ~CLIParser() {
|
|
117 |
}
|
|
118 |
};
|
|
119 |
|
|
120 |
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
|
|
121 |
const relpipe::common::type::StringX CLIParser::OPTION_WRITE_HEADER = L"--write-header";
|
|
122 |
const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
|
|
123 |
const relpipe::common::type::StringX CLIParser::OPTION_RECORD = L"--record";
|
|
124 |
const relpipe::common::type::StringX CLIParser::OPTION_RECORDS = L"--records";
|
|
125 |
const relpipe::common::type::StringX CLIParser::OPTION_RECORDS_STDIN = L"--records-on-stdin";
|
|
126 |
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|