author | František Kučera <franta-hg@frantovo.cz> |
Sun, 05 Jun 2022 00:08:52 +0200 | |
branch | v_0 |
changeset 3 | 62ced7c41c67 |
parent 2 | 1a0fbd17db13 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 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 mqtt { |
|
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 |
public: |
|
41 |
||
42 |
static const relpipe::common::type::StringX OPTION_RELATION; |
|
2 | 43 |
static const relpipe::common::type::StringX OPTION_STREAM; |
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
44 |
static const relpipe::common::type::StringX OPTION_CONNECTION_STRING; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
45 |
static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION; |
0 | 46 |
|
47 |
Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
48 |
Configuration c; |
|
49 |
||
50 |
for (int i = 0; i < arguments.size();) { |
|
51 |
relpipe::common::type::StringX option = readNext(arguments, i); |
|
52 |
||
53 |
if (option == OPTION_RELATION) { |
|
54 |
c.relation = readNext(arguments, i); |
|
2 | 55 |
} else if (option == OPTION_STREAM) { |
56 |
c.stream = readNext(arguments, i); |
|
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
} else if (option == OPTION_CONNECTION_STRING) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
c.connectionString = readNext(arguments, i); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
} else if (option == OPTION_CONNECTION_OPTION) { |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
auto name = readNext(arguments, i); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
auto value = readNext(arguments, i); |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
c.connectionOptions.push_back({name, value}); |
0 | 63 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
64 |
} |
|
65 |
||
66 |
return c; |
|
67 |
} |
|
68 |
||
69 |
virtual ~CLIParser() { |
|
70 |
} |
|
71 |
}; |
|
72 |
||
73 |
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
2 | 74 |
const relpipe::common::type::StringX CLIParser::OPTION_STREAM = L"--stream"; |
3
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string"; |
62ced7c41c67
parse connection string, credentials, check return values
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option"; |
0 | 77 |
|
78 |
} |
|
79 |
} |
|
80 |
} |