author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Nov 2021 23:15:15 +0100 | |
branch | v_0 |
changeset 93 | 979848ccdc9c |
parent 52 | fea625f0a096 |
permissions | -rw-r--r-- |
4 | 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 |
|
24
4353cd19a6b5
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
4 | 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/writer/typedefs.h> |
|
10 | 22 |
#include <relpipe/cli/CLI.h> |
23 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
4 | 24 |
|
25 |
#include "Configuration.h" |
|
26 |
#include "FileAttributeFinder.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace filesystem { |
|
31 |
||
32 |
class CLIParser { |
|
10 | 33 |
private: |
34 |
||
35 |
string_t readNext(std::vector<string_t> 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 |
||
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
40 |
void addField(Configuration& c, string_t& group, string_t& name, std::vector<string_t>& aliases, std::vector<string_t>& options) { |
10 | 41 |
if (group.size()) { |
42 |
c.fields.push_back(RequestedField(group, name, aliases, options)); |
|
43 |
group.clear(); |
|
44 |
name.clear(); |
|
45 |
aliases.clear(); |
|
46 |
options.clear(); |
|
47 |
} |
|
48 |
} |
|
49 |
||
4 | 50 |
public: |
51 |
||
10 | 52 |
static const string_t OPTION_FILE; |
53 |
static const string_t OPTION_XATTR; |
|
31
c64e1588f428
rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
54 |
static const string_t OPTION_STREAMLET; |
10 | 55 |
static const string_t OPTION_AS; |
56 |
static const string_t OPTION_OPTION; |
|
16
6180161335be
support custom relation name through --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
57 |
static const string_t OPTION_RELATION; |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
58 |
static const string_t OPTION_PARALLEL; |
10 | 59 |
|
4 | 60 |
Configuration parse(const std::vector<string_t>& arguments) { |
61 |
Configuration c; |
|
15
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
62 |
|
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
63 |
{ |
10 | 64 |
string_t currentGroup; |
65 |
string_t currentName; |
|
66 |
std::vector<string_t> currentAliases; |
|
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
67 |
std::vector<string_t> currentOptions; |
10 | 68 |
|
69 |
for (int i = 0; i < arguments.size();) { |
|
70 |
string_t option = readNext(arguments, i); |
|
71 |
||
31
c64e1588f428
rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
72 |
if (option == CLIParser::OPTION_FILE || option == CLIParser::OPTION_XATTR || option == CLIParser::OPTION_STREAMLET) { |
10 | 73 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // previous field |
74 |
currentGroup = option.substr(2); // cut off -- |
|
75 |
currentName = readNext(arguments, i); |
|
76 |
} else if (option == OPTION_AS) { |
|
77 |
currentAliases.push_back(readNext(arguments, i)); |
|
78 |
} else if (option == OPTION_OPTION) { |
|
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
79 |
currentOptions.push_back(readNext(arguments, i)); |
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
80 |
currentOptions.push_back(readNext(arguments, i)); |
16
6180161335be
support custom relation name through --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
81 |
} else if (option == OPTION_RELATION) { |
6180161335be
support custom relation name through --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
82 |
c.relation = readNext(arguments, i); |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
83 |
} else if (option == OPTION_PARALLEL) { |
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
84 |
c.parallelism = std::stoi(readNext(arguments, i)); |
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
85 |
if (c.parallelism < 1) throw relpipe::cli::RelpipeCLIException(L"Number of parallel processes must be 1 or more.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
10 | 86 |
} else { |
87 |
throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
88 |
} |
|
89 |
} |
|
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
90 |
|
10 | 91 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // last field |
92 |
} |
|
15
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
93 |
|
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
94 |
|
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
95 |
if (c.fields.empty()) { |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
96 |
// default set of fields: |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
97 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ORIGINAL)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
98 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ABSOLUTE)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
99 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_CANONICAL)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
100 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_NAME)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
101 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
102 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE,{L"type_a", L"type_b"})); // one field → two attributes with same value |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
103 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET_TYPE)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
104 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
105 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SIZE)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
106 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_OWNER)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
107 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_GROUP)); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
108 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_XATTR, L"user.xdg.origin.url")); |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
109 |
} |
e7f05d7c4336
use default set of fields even if there are some other CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
110 |
|
29
6f15f18d2abf
field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
111 |
for (int i = 0; i < c.fields.size(); i++) c.fields[i].id = i; |
6f15f18d2abf
field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
112 |
|
4 | 113 |
return c; |
114 |
} |
|
115 |
||
116 |
virtual ~CLIParser() { |
|
117 |
} |
|
118 |
}; |
|
119 |
||
10 | 120 |
const string_t CLIParser::OPTION_FILE = L"--" + RequestedField::GROUP_FILE; |
121 |
const string_t CLIParser::OPTION_XATTR = L"--" + RequestedField::GROUP_XATTR; |
|
31
c64e1588f428
rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
122 |
const string_t CLIParser::OPTION_STREAMLET = L"--" + RequestedField::GROUP_STREAMLET; |
10 | 123 |
const string_t CLIParser::OPTION_AS = L"--as"; |
124 |
const string_t CLIParser::OPTION_OPTION = L"--option"; |
|
16
6180161335be
support custom relation name through --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
125 |
const string_t CLIParser::OPTION_RELATION = L"--relation"; |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
126 |
const string_t CLIParser::OPTION_PARALLEL = L"--parallel"; |
10 | 127 |
|
4 | 128 |
} |
129 |
} |
|
130 |
} |