src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 06 Oct 2020 16:24:57 +0200
branchv_0
changeset 21 70b252d02a92
parent 15 b3239e4ad328
child 23 d2ad84dcf249
permissions -rw-r--r--
refactoring of includes, fix locking
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3bc3b33ccb02 project skeleton
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
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace out {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace jack {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
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);
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		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);
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
2
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	static const relpipe::common::type::StringX OPTION_JACK_CLIENT_NAME;
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    52
	static const relpipe::common::type::StringX OPTION_JACK_CONNECT_TO_PORT;
9
14cf28d7681c option: --required-jack-connections (start after given number of port connections)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
	static const relpipe::common::type::StringX OPTION_REQUIRED_JACK_CONNECTIONS;
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		Configuration c;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		for (int i = 0; i < arguments.size();) {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			relpipe::common::type::StringX option = readNext(arguments, i);
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
2
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
			if (option == OPTION_JACK_CLIENT_NAME) {
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
				c.jackClientName = readNext(arguments, i);
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    63
			} else if (option == OPTION_JACK_CONNECT_TO_PORT) {
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    64
				c.portsToConnect.push_back(readNext(arguments, i));
9
14cf28d7681c option: --required-jack-connections (start after given number of port connections)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
			} else if (option == OPTION_REQUIRED_JACK_CONNECTIONS) {
14cf28d7681c option: --required-jack-connections (start after given number of port connections)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
				c.requiredJackConnections = std::stoi(readNext(arguments, i));
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		return c;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	virtual ~CLIParser() {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
};
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
2
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
const relpipe::common::type::StringX CLIParser::OPTION_JACK_CLIENT_NAME = L"--jack-client-name";
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    78
const relpipe::common::type::StringX CLIParser::OPTION_JACK_CONNECT_TO_PORT = L"--jack-connect-to-port";
9
14cf28d7681c option: --required-jack-connections (start after given number of port connections)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
const relpipe::common::type::StringX CLIParser::OPTION_REQUIRED_JACK_CONNECTIONS = L"--required-jack-connections";
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}