author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Nov 2021 23:15:17 +0100 | |
branch | v_0 |
changeset 9 | 42dbdee1d9f0 |
parent 1 | e04e5bbc147b |
permissions | -rw-r--r-- |
0 | 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/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 out { |
|
30 |
namespace ini { |
|
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 |
/** |
|
41 |
* TODO: use a common method |
|
42 |
*/ |
|
43 |
bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
44 |
if (value == L"true") return true; |
|
45 |
else if (value == L"false") return false; |
|
46 |
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); |
|
47 |
} |
|
48 |
||
49 |
void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
50 |
if (currentRelation.relation.size()) { |
|
51 |
c.relationConfigurations.push_back(currentRelation); |
|
52 |
currentRelation = RelationConfiguration(); |
|
53 |
} |
|
54 |
} |
|
55 |
||
56 |
Style parseStyle(relpipe::common::type::StringX style) { |
|
57 |
if (style == L"auto") return Style::Automatic; |
|
58 |
else if (style == L"dropped") return Style::Dropped; |
|
59 |
else if (style == L"literal") return Style::Literal; |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
else if (style == L"section-first") return Style::SectionFirst; |
0 | 61 |
else if (style == L"standard") return Style::Standard; |
62 |
else throw relpipe::cli::RelpipeCLIException(L"Unsupported style option: " + style, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
63 |
} |
|
64 |
||
65 |
public: |
|
66 |
||
67 |
static const relpipe::common::type::StringX OPTION_RELATION; |
|
68 |
static const relpipe::common::type::StringX OPTION_STYLE; |
|
69 |
static const relpipe::common::type::StringX OPTION_WRITER_OPTION; |
|
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_STYLE) { |
|
82 |
currentRelation.style = parseStyle(readNext(arguments, i)); |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
if (currentRelation.relation.empty()) currentRelation.relation = L".*"; |
0 | 84 |
} else if (option == OPTION_WRITER_OPTION) { |
85 |
c.writerOptions.push_back({readNext(arguments, i), readNext(arguments, i)}); |
|
86 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
87 |
} |
|
88 |
addRelation(c, currentRelation); // last relation |
|
89 |
||
90 |
return c; |
|
91 |
} |
|
92 |
||
93 |
virtual ~CLIParser() { |
|
94 |
} |
|
95 |
}; |
|
96 |
||
97 |
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
98 |
const relpipe::common::type::StringX CLIParser::OPTION_STYLE = L"--style"; |
|
99 |
const relpipe::common::type::StringX CLIParser::OPTION_WRITER_OPTION = L"--writer-option"; |
|
100 |
||
101 |
} |
|
102 |
} |
|
103 |
} |