src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 08 Oct 2020 16:45:50 +0200
branchv_0
changeset 13 326935d1bfab
parent 12 e8aae4d42c01
child 14 cde9bb07ea0a
permissions -rw-r--r--
add option --list-connections for listing JACK connections the output can be later sent to the relpipe-out-jack to restore the connection graph

/**
 * 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/common/type/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"

namespace relpipe {
namespace in {
namespace jack {

class CLIParser {
private:

	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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);
	}

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const relpipe::common::type::StringX& 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);
	}

public:

	static const relpipe::common::type::StringX OPTION_CLIENT;
	static const relpipe::common::type::StringX OPTION_PORT;
	static const relpipe::common::type::StringX OPTION_CONNECT_TO;
	static const relpipe::common::type::StringX OPTION_REQUIRED_CONNECTIONS;
	static const relpipe::common::type::StringX OPTION_LIST_PORTS;
	static const relpipe::common::type::StringX OPTION_LIST_CONNECTIONS;
	static const relpipe::common::type::StringX OPTION_LIST_MIDI_MESSAGES;

	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
		Configuration c;

		for (int i = 0; i < arguments.size();) {
			relpipe::common::type::StringX option = readNext(arguments, i);

			if (option == OPTION_CLIENT) {
				c.client = readNext(arguments, i);
			} else if (option == OPTION_PORT) {
				c.port = readNext(arguments, i);
			} else if (option == OPTION_CONNECT_TO) {
				c.connectTo.push_back(readNext(arguments, i));
			} else if (option == OPTION_REQUIRED_CONNECTIONS) {
				c.requiredConnections = std::stoi(readNext(arguments, i));
			} else if (option == OPTION_LIST_PORTS) {
				c.listPorts = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_LIST_CONNECTIONS) {
				c.listConnections = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_LIST_MIDI_MESSAGES) {
				c.listMidiMessages = parseBoolean(readNext(arguments, i));
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		}

		return c;
	}

	virtual ~CLIParser() {
	}
};

const relpipe::common::type::StringX CLIParser::OPTION_CLIENT = L"--client";
const relpipe::common::type::StringX CLIParser::OPTION_PORT = L"--port";
const relpipe::common::type::StringX CLIParser::OPTION_CONNECT_TO = L"--connect-to";
const relpipe::common::type::StringX CLIParser::OPTION_REQUIRED_CONNECTIONS = L"--required-connections";
const relpipe::common::type::StringX CLIParser::OPTION_LIST_PORTS = L"--list-ports";
const relpipe::common::type::StringX CLIParser::OPTION_LIST_CONNECTIONS = L"--list-connections";
const relpipe::common::type::StringX CLIParser::OPTION_LIST_MIDI_MESSAGES = L"--list-midi-messages";

}
}
}