src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 20 Jan 2020 12:39:22 +0100
branchv_0
changeset 52 fea625f0a096
parent 31 c64e1588f428
permissions -rw-r--r--
parallel processing: prepare infrastructure

/**
 * Relational pipes
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <vector>

#include <relpipe/writer/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"
#include "FileAttributeFinder.h"

namespace relpipe {
namespace in {
namespace filesystem {

class CLIParser {
private:

	string_t readNext(std::vector<string_t> arguments, int& i) {
		if (i < arguments.size()) return arguments[i++];
		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);
	}

	void addField(Configuration& c, string_t& group, string_t& name, std::vector<string_t>& aliases, std::vector<string_t>& options) {
		if (group.size()) {
			c.fields.push_back(RequestedField(group, name, aliases, options));
			group.clear();
			name.clear();
			aliases.clear();
			options.clear();
		}
	}

public:

	static const string_t OPTION_FILE;
	static const string_t OPTION_XATTR;
	static const string_t OPTION_STREAMLET;
	static const string_t OPTION_AS;
	static const string_t OPTION_OPTION;
	static const string_t OPTION_RELATION;
	static const string_t OPTION_PARALLEL;

	Configuration parse(const std::vector<string_t>& arguments) {
		Configuration c;

		{
			string_t currentGroup;
			string_t currentName;
			std::vector<string_t> currentAliases;
			std::vector<string_t> currentOptions;

			for (int i = 0; i < arguments.size();) {
				string_t option = readNext(arguments, i);

				if (option == CLIParser::OPTION_FILE || option == CLIParser::OPTION_XATTR || option == CLIParser::OPTION_STREAMLET) {
					addField(c, currentGroup, currentName, currentAliases, currentOptions); // previous field
					currentGroup = option.substr(2); // cut off --
					currentName = readNext(arguments, i);
				} else if (option == OPTION_AS) {
					currentAliases.push_back(readNext(arguments, i));
				} else if (option == OPTION_OPTION) {
					currentOptions.push_back(readNext(arguments, i));
					currentOptions.push_back(readNext(arguments, i));
				} else if (option == OPTION_RELATION) {
					c.relation = readNext(arguments, i);
				} else if (option == OPTION_PARALLEL) {
					c.parallelism = std::stoi(readNext(arguments, i));
					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);
				} else {
					throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
				}
			}

			addField(c, currentGroup, currentName, currentAliases, currentOptions); // last field
		}


		if (c.fields.empty()) {
			// default set of fields:
			c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ORIGINAL));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_ABSOLUTE));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_PATH_CANONICAL));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_NAME));
			c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE,{L"type_a", L"type_b"})); // one field → two attributes with same value
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET_TYPE));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SYMLINK_TARGET));
			c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_SIZE));
			c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_OWNER));
			c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_GROUP));
			// c.fields.push_back(RequestedField(RequestedField::GROUP_XATTR, L"user.xdg.origin.url"));
		}

		for (int i = 0; i < c.fields.size(); i++) c.fields[i].id = i;

		return c;
	}

	virtual ~CLIParser() {
	}
};

const string_t CLIParser::OPTION_FILE = L"--" + RequestedField::GROUP_FILE;
const string_t CLIParser::OPTION_XATTR = L"--" + RequestedField::GROUP_XATTR;
const string_t CLIParser::OPTION_STREAMLET = L"--" + RequestedField::GROUP_STREAMLET;
const string_t CLIParser::OPTION_AS = L"--as";
const string_t CLIParser::OPTION_OPTION = L"--option";
const string_t CLIParser::OPTION_RELATION = L"--relation";
const string_t CLIParser::OPTION_PARALLEL = L"--parallel";

}
}
}