author | František Kučera <franta-hg@frantovo.cz> |
Fri, 01 Feb 2019 21:10:33 +0100 | |
branch | v_0 |
changeset 17 | 87a7607d5def |
parent 16 | 6180161335be |
child 24 | 4353cd19a6b5 |
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 |
|
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> |
|
10 | 23 |
#include <relpipe/cli/CLI.h> |
24 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
4 | 25 |
|
26 |
#include "Configuration.h" |
|
27 |
#include "FileAttributeFinder.h" |
|
28 |
||
29 |
namespace relpipe { |
|
30 |
namespace in { |
|
31 |
namespace filesystem { |
|
32 |
||
33 |
class CLIParser { |
|
10 | 34 |
private: |
35 |
||
36 |
string_t readNext(std::vector<string_t> arguments, int& i) { |
|
37 |
if (i < arguments.size()) return arguments[i++]; |
|
38 |
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); |
|
39 |
} |
|
40 |
||
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
41 |
void addField(Configuration& c, string_t& group, string_t& name, std::vector<string_t>& aliases, std::vector<string_t>& options) { |
10 | 42 |
if (group.size()) { |
43 |
c.fields.push_back(RequestedField(group, name, aliases, options)); |
|
44 |
group.clear(); |
|
45 |
name.clear(); |
|
46 |
aliases.clear(); |
|
47 |
options.clear(); |
|
48 |
} |
|
49 |
} |
|
50 |
||
4 | 51 |
public: |
52 |
||
10 | 53 |
static const string_t OPTION_FILE; |
54 |
static const string_t OPTION_XATTR; |
|
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; |
10 | 58 |
|
4 | 59 |
Configuration parse(const std::vector<string_t>& arguments) { |
60 |
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
|
61 |
|
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 |
{ |
10 | 63 |
string_t currentGroup; |
64 |
string_t currentName; |
|
65 |
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
|
66 |
std::vector<string_t> currentOptions; |
10 | 67 |
|
68 |
for (int i = 0; i < arguments.size();) { |
|
69 |
string_t option = readNext(arguments, i); |
|
70 |
||
71 |
if (option == CLIParser::OPTION_FILE || option == CLIParser::OPTION_XATTR) { |
|
72 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // previous field |
|
73 |
currentGroup = option.substr(2); // cut off -- |
|
74 |
currentName = readNext(arguments, i); |
|
75 |
} else if (option == OPTION_AS) { |
|
76 |
currentAliases.push_back(readNext(arguments, i)); |
|
77 |
} 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
|
78 |
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
|
79 |
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
|
80 |
} 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
|
81 |
c.relation = readNext(arguments, i); |
10 | 82 |
} else { |
83 |
throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
84 |
} |
|
85 |
} |
|
12
0a297eb46ba1
requested field's options as an ordered multi-map
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
86 |
|
10 | 87 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // last field |
88 |
} |
|
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
|
89 |
|
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
|
90 |
|
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
|
91 |
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
|
92 |
// 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
|
93 |
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
|
94 |
// 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
|
95 |
// 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
|
96 |
// 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
|
97 |
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
|
98 |
// 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
|
99 |
// 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
|
100 |
// 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
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
// 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
|
105 |
} |
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 |
|
4 | 107 |
return c; |
108 |
} |
|
109 |
||
110 |
virtual ~CLIParser() { |
|
111 |
} |
|
112 |
}; |
|
113 |
||
10 | 114 |
const string_t CLIParser::OPTION_FILE = L"--" + RequestedField::GROUP_FILE; |
115 |
const string_t CLIParser::OPTION_XATTR = L"--" + RequestedField::GROUP_XATTR; |
|
116 |
const string_t CLIParser::OPTION_AS = L"--as"; |
|
117 |
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
|
118 |
const string_t CLIParser::OPTION_RELATION = L"--relation"; |
10 | 119 |
|
4 | 120 |
} |
121 |
} |
|
122 |
} |