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