src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 27 Nov 2021 23:15:15 +0100
branchv_0
changeset 93 979848ccdc9c
parent 52 fea625f0a096
permissions -rw-r--r--
exception: fix typo: getMessge() → getMessage()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     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
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/writer/typedefs.h>
10
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    22
#include <relpipe/cli/CLI.h>
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "FileAttributeFinder.h"
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace filesystem {
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
10
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    33
private:
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    34
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    35
	string_t readNext(std::vector<string_t> arguments, int& i) {
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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);
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    38
	}
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    41
		if (group.size()) {
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    42
			c.fields.push_back(RequestedField(group, name, aliases, options));
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    43
			group.clear();
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    44
			name.clear();
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    45
			aliases.clear();
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    46
			options.clear();
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    47
		}
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    48
	}
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    49
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
public:
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
10
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    52
	static const string_t OPTION_FILE;
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    55
	static const string_t OPTION_AS;
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    59
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	Configuration parse(const std::vector<string_t>& arguments) {
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    64
			string_t currentGroup;
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
			string_t currentName;
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    68
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    69
			for (int i = 0; i < arguments.size();) {
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    70
				string_t option = readNext(arguments, i);
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    73
					addField(c, currentGroup, currentName, currentAliases, currentOptions); // previous field
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    74
					currentGroup = option.substr(2); // cut off --
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
					currentName = readNext(arguments, i);
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    76
				} else if (option == OPTION_AS) {
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
					currentAliases.push_back(readNext(arguments, i));
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    86
				} else {
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    87
					throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    88
				}
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    91
			addField(c, currentGroup, currentName, currentAliases, currentOptions); // last field
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    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
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
		return c;
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
	}
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	virtual ~CLIParser() {
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
	}
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
};
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
10
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   120
const string_t CLIParser::OPTION_FILE = L"--" + RequestedField::GROUP_FILE;
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   123
const string_t CLIParser::OPTION_AS = L"--as";
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   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
3e1608320b6c parse CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   127
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
}