|
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 |
|
21 #include <relpipe/common/type/typedefs.h> |
|
22 #include <relpipe/cli/CLI.h> |
|
23 #include <relpipe/cli/RelpipeCLIException.h> |
|
24 |
|
25 #include "Configuration.h" |
|
26 |
|
27 namespace relpipe { |
|
28 namespace tr { |
|
29 namespace cut { |
|
30 |
|
31 class CLIParser { |
|
32 private: |
|
33 |
|
34 relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) { |
|
35 if (i < arguments.size()) return arguments[i++]; |
|
36 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); |
|
37 } |
|
38 |
|
39 /** |
|
40 * TODO: use a common method |
|
41 */ |
|
42 bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
43 if (value == L"true") return true; |
|
44 else if (value == L"false") return false; |
|
45 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); |
|
46 } |
|
47 |
|
48 RelationConfiguration::ENTITY parseEntity(const relpipe::common::type::StringX& value) { |
|
49 if (value == L"relation") return RelationConfiguration::ENTITY::RELATION; |
|
50 else if (value == L"attribute") return RelationConfiguration::ENTITY::ATTRIBUTE; |
|
51 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation“, „attribute“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
52 } |
|
53 |
|
54 void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
55 if (currentRelation.relation.size()) { |
|
56 using E = RelationConfiguration::ENTITY; |
|
57 currentRelation.relationPattern = currentRelation.caseSensitive[E::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase); |
|
58 for (const relpipe::common::type::StringX& attribute : currentRelation.attributes) currentRelation.attributePatterns.push_back(currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(attribute) : std::wregex(attribute, std::regex_constants::icase)); |
|
59 c.relationConfigurations.push_back(currentRelation); |
|
60 currentRelation = RelationConfiguration(); |
|
61 } |
|
62 } |
|
63 |
|
64 public: |
|
65 |
|
66 static const relpipe::common::type::StringX OPTION_RELATION; |
|
67 static const relpipe::common::type::StringX OPTION_ATTRIBUTE; |
|
68 static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE; |
|
69 static const relpipe::common::type::StringX OPTION_INVERT_MATCH; |
|
70 |
|
71 Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
72 Configuration c; |
|
73 RelationConfiguration currentRelation; |
|
74 |
|
75 for (int i = 0; i < arguments.size();) { |
|
76 relpipe::common::type::StringX option = readNext(arguments, i); |
|
77 |
|
78 if (option == OPTION_RELATION) { |
|
79 addRelation(c, currentRelation); // previous relation |
|
80 currentRelation.relation = readNext(arguments, i); |
|
81 } else if (option == OPTION_ATTRIBUTE) { |
|
82 currentRelation.attributes.push_back(readNext(arguments, i)); |
|
83 } else if (option == OPTION_CASE_SENSITIVE) { |
|
84 RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i)); |
|
85 currentRelation.caseSensitive[entity] = parseBoolean(readNext(arguments, i)); |
|
86 } else if (option == OPTION_INVERT_MATCH) { |
|
87 RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i)); |
|
88 currentRelation.invertMatch[entity] = parseBoolean(readNext(arguments, i)); |
|
89 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
90 } |
|
91 addRelation(c, currentRelation); // last relation |
|
92 |
|
93 return c; |
|
94 } |
|
95 |
|
96 virtual ~CLIParser() { |
|
97 } |
|
98 }; |
|
99 |
|
100 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
101 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute"; |
|
102 const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive"; |
|
103 const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match"; |
|
104 |
|
105 } |
|
106 } |
|
107 } |