author | František Kučera <franta-hg@frantovo.cz> |
Tue, 07 May 2019 21:33:59 +0200 | |
branch | v_0 |
changeset 19 | e4558df9ba2d |
parent 14 | f407f2a2871d |
child 26 | cf57e8c78492 |
permissions | -rw-r--r-- |
0 | 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 awk { |
|
31 |
||
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
32 |
/** |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
* This tr-awk CLI options are inspired by tr-guile options. |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
* These configurations are independent and might diverge, but when possible, the should share same option names and same logic for common parts. |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
35 |
*/ |
0 | 36 |
class CLIParser { |
37 |
private: |
|
38 |
||
39 |
// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper |
|
40 |
||
41 |
string_t readNext(std::vector<string_t> arguments, int& i) { |
|
42 |
if (i < arguments.size()) return arguments[i++]; |
|
43 |
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); |
|
44 |
} |
|
45 |
||
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
if (currentRelation.relation.size()) { |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
c.relationConfigurations.push_back(currentRelation); |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
currentRelation = RelationConfiguration(); |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
} |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
} |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
|
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
relpipe::writer::TypeId parseTypeId(const string_t& value) { |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
using t = relpipe::writer::TypeId; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
if (value == L"string") return t::STRING; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
else if (value == L"integer") return t::INTEGER; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
else if (value == L"boolean") return t::BOOLEAN; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
} |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
|
0 | 61 |
public: |
62 |
||
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
static const string_t OPTION_RELATION; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
static const string_t OPTION_OUTPUT_ATTRIBUTE; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
static const string_t OPTION_INPUT_ATTRIBUTES_APPEND; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
static const string_t OPTION_INPUT_ATTRIBUTES_PREPEND; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
static const string_t OPTION_BEFORE_RECORDS; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
static const string_t OPTION_AFTER_RECORDS; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
static const string_t OPTION_FOR_EACH; |
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
70 |
static const string_t OPTION_DROP; |
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
static const string_t OPTION_DEFINE; |
0 | 72 |
|
73 |
Configuration parse(const std::vector<string_t>& arguments) { |
|
74 |
Configuration c; |
|
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
RelationConfiguration currentRelation; |
0 | 76 |
|
9 | 77 |
for (int i = 0; i < arguments.size();) { |
78 |
string_t option = readNext(arguments, i); |
|
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
|
9 | 80 |
if (option == OPTION_BEFORE_RECORDS) currentRelation.awkBeforeRecords = readNext(arguments, i); |
81 |
else if (option == OPTION_AFTER_RECORDS) currentRelation.awkAfterRecords = readNext(arguments, i); |
|
82 |
else if (option == OPTION_FOR_EACH) currentRelation.awkForEach = readNext(arguments, i); |
|
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
83 |
else if (option == OPTION_DROP) currentRelation.drop = true; |
9 | 84 |
else if (option == OPTION_INPUT_ATTRIBUTES_APPEND) currentRelation.inputAttributesAppend = true; |
85 |
else if (option == OPTION_INPUT_ATTRIBUTES_PREPEND) currentRelation.inputAttributesPrepend = true; |
|
86 |
else if (option == OPTION_RELATION) { |
|
87 |
addRelation(c, currentRelation); // previous relation |
|
88 |
currentRelation.relation = readNext(arguments, i); |
|
89 |
} else if (option == OPTION_OUTPUT_ATTRIBUTE) { |
|
90 |
relpipe::writer::AttributeMetadata attribute; |
|
91 |
attribute.attributeName = readNext(arguments, i); |
|
92 |
attribute.typeId = parseTypeId(readNext(arguments, i)); |
|
93 |
currentRelation.writerMetadata.push_back(attribute); |
|
94 |
} else if (option == OPTION_DEFINE) { |
|
95 |
DefinitionRecipe definition; |
|
96 |
definition.name = readNext(arguments, i); |
|
97 |
definition.type = readNext(arguments, i); |
|
98 |
definition.value = readNext(arguments, i); |
|
99 |
if (currentRelation.relation.size()) currentRelation.definitions.push_back(definition); |
|
100 |
else c.definitions.push_back(definition); |
|
101 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
0 | 102 |
} |
14
f407f2a2871d
omit superfluous {} fixed
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
103 |
addRelation(c, currentRelation); // last relation |
0 | 104 |
|
105 |
return c; |
|
106 |
} |
|
107 |
||
108 |
virtual ~CLIParser() { |
|
109 |
} |
|
110 |
}; |
|
111 |
||
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
112 |
const string_t CLIParser::OPTION_RELATION = L"--relation"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
113 |
const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
114 |
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_APPEND = L"--input-attributes-append"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
115 |
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_PREPEND = L"--input-attributes-prepend"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
116 |
const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
117 |
const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records"; |
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
118 |
const string_t CLIParser::OPTION_FOR_EACH = L"--for-each"; |
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
119 |
const string_t CLIParser::OPTION_DROP = L"--drop"; |
8
513ea30d2fa3
more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
120 |
const string_t CLIParser::OPTION_DEFINE = L"--define"; |
0 | 121 |
|
122 |
} |
|
123 |
} |
|
124 |
} |