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 |
|
|
43 |
void addField(Configuration& c, string_t& group, string_t& name, std::vector<string_t>& aliases, std::map<string_t, string_t>& options) {
|
|
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;
|
|
59 |
|
4
|
60 |
Configuration parse(const std::vector<string_t>& arguments) {
|
|
61 |
Configuration c;
|
10
|
62 |
if (arguments.empty()) {
|
|
63 |
// default set of fields:
|
|
64 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ORIGINAL));
|
|
65 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ABSOLUTE));
|
|
66 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_CANONICAL));
|
|
67 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_NAME));
|
|
68 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE));
|
|
69 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE,{L"type_a", L"type_b"})); // one field → two attributes with same value
|
|
70 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET_TYPE));
|
|
71 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET));
|
|
72 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SIZE));
|
|
73 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_OWNER));
|
|
74 |
c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_GROUP));
|
|
75 |
// c.fields.push_back(RequestedField(RequestedField::GROUP_XATTR, L"user.xdg.origin.url"));
|
|
76 |
} else {
|
|
77 |
string_t currentGroup;
|
|
78 |
string_t currentName;
|
|
79 |
std::vector<string_t> currentAliases;
|
|
80 |
std::map<string_t, string_t> currentOptions;
|
|
81 |
|
|
82 |
for (int i = 0; i < arguments.size();) {
|
|
83 |
string_t option = readNext(arguments, i);
|
|
84 |
|
|
85 |
if (option == CLIParser::OPTION_FILE || option == CLIParser::OPTION_XATTR) {
|
|
86 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // previous field
|
|
87 |
currentGroup = option.substr(2); // cut off --
|
|
88 |
currentName = readNext(arguments, i);
|
|
89 |
} else if (option == OPTION_AS) {
|
|
90 |
currentAliases.push_back(readNext(arguments, i));
|
|
91 |
} else if (option == OPTION_OPTION) {
|
|
92 |
string_t key = readNext(arguments, i);
|
|
93 |
string_t value = readNext(arguments, i);
|
|
94 |
currentOptions[key] = value;
|
|
95 |
} else {
|
|
96 |
throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
addField(c, currentGroup, currentName, currentAliases, currentOptions); // last field
|
|
101 |
}
|
4
|
102 |
return c;
|
|
103 |
}
|
|
104 |
|
|
105 |
virtual ~CLIParser() {
|
|
106 |
}
|
|
107 |
};
|
|
108 |
|
10
|
109 |
const string_t CLIParser::OPTION_FILE = L"--" + RequestedField::GROUP_FILE;
|
|
110 |
const string_t CLIParser::OPTION_XATTR = L"--" + RequestedField::GROUP_XATTR;
|
|
111 |
const string_t CLIParser::OPTION_AS = L"--as";
|
|
112 |
const string_t CLIParser::OPTION_OPTION = L"--option";
|
|
113 |
|
4
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|