/**
* Relational pipes
* Copyright © 2020 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 <iostream>
#include <relpipe/writer/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>
#include "Configuration.h"
namespace relpipe {
namespace in {
namespace cli {
class CLIParser {
private:
relpipe::writer::string_t readNext(const std::vector<relpipe::writer::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 readNextRecord(const std::vector<relpipe::writer::string_t>& arguments, int& i, RelationConfiguration& currentRelation) {
for (const auto& a : currentRelation.attributes) currentRelation.values.emplace_back(readNext(arguments, i));
}
void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
if (currentRelation.relation.size()) {
c.relationConfigurations.push_back(currentRelation);
currentRelation = RelationConfiguration();
}
}
/**
* TODO: use a common method
*/
bool parseBoolean(const relpipe::writer::string_t& value) {
if (value == L"true") return true;
else if (value == L"false") return false;
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
}
/**
* TODO: use a common method
*/
relpipe::writer::TypeId parseTypeId(const relpipe::writer::string_t& value) {
using t = relpipe::writer::TypeId;
if (value == L"string") return t::STRING;
else if (value == L"integer") return t::INTEGER;
else if (value == L"boolean") return t::BOOLEAN;
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
}
public:
static const relpipe::writer::string_t OPTION_RELATION;
static const relpipe::writer::string_t OPTION_WRITE_HEADER;
static const relpipe::writer::string_t OPTION_ATTRIBUTE;
static const relpipe::writer::string_t OPTION_RECORD;
static const relpipe::writer::string_t OPTION_RECORDS;
static const relpipe::writer::string_t OPTION_RECORDS_STDIN;
Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
Configuration c;
RelationConfiguration currentRelation;
for (int i = 0; i < arguments.size();) {
relpipe::writer::string_t option = readNext(arguments, i);
if (option == OPTION_RELATION) {
addRelation(c, currentRelation); // previous relation
currentRelation.relation = readNext(arguments, i);
} else if (option == OPTION_WRITE_HEADER) {
currentRelation.writeHeader = parseBoolean(readNext(arguments, i));
} else if (option == OPTION_ATTRIBUTE) {
AttributeRecipe attribute;
attribute.name = readNext(arguments, i);
attribute.type = parseTypeId(readNext(arguments, i));
currentRelation.attributes.push_back(attribute);
} else if (option == OPTION_RECORD) {
readNextRecord(arguments, i, currentRelation);
} else if (option == OPTION_RECORDS) {
while (i < arguments.size()) readNextRecord(arguments, i, currentRelation);
} else if (option == OPTION_RECORDS_STDIN) {
if (parseBoolean(readNext(arguments, i))) {
for (RelationConfiguration r : c.relationConfigurations) if (r.valueStream) throw relpipe::cli::RelpipeCLIException(L"Only one relation can read data from STDIN.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
currentRelation.valueStream = &std::cin;
}
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
}
addRelation(c, currentRelation); // last relation
return c;
}
virtual ~CLIParser() {
}
};
const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
const relpipe::writer::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
const relpipe::writer::string_t CLIParser::OPTION_RECORD = L"--record";
const relpipe::writer::string_t CLIParser::OPTION_RECORDS = L"--records";
const relpipe::writer::string_t CLIParser::OPTION_RECORDS_STDIN = L"--records-on-stdin";
}
}
}