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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
8ef1980db907 configurable JACK client name
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
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace jack {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
8ef1980db907 configurable JACK client name
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) {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
8ef1980db907 configurable JACK client name
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);
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
8ef1980db907 configurable JACK client name
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);
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    51
	static const relpipe::common::type::StringX OPTION_CLIENT;
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    52
	static const relpipe::common::type::StringX OPTION_PORT;
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    53
	static const relpipe::common::type::StringX OPTION_CONNECT_TO;
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    54
	static const relpipe::common::type::StringX OPTION_REQUIRED_CONNECTIONS;
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    55
	static const relpipe::common::type::StringX OPTION_LIST_PORTS;
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    56
	static const relpipe::common::type::StringX OPTION_LIST_CONNECTIONS;
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    57
	static const relpipe::common::type::StringX OPTION_LIST_MIDI_MESSAGES;
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		Configuration c;
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		for (int i = 0; i < arguments.size();) {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			relpipe::common::type::StringX option = readNext(arguments, i);
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    65
			if (option == OPTION_CLIENT) {
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    66
				c.client = readNext(arguments, i);
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    67
			} else if (option == OPTION_PORT) {
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    68
				c.port = readNext(arguments, i);
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    69
			} else if (option == OPTION_CONNECT_TO) {
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    70
				c.connectTo.push_back(readNext(arguments, i));
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    71
			} else if (option == OPTION_REQUIRED_CONNECTIONS) {
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    72
				c.requiredConnections = std::stoi(readNext(arguments, i));
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    73
			} else if (option == OPTION_LIST_PORTS) {
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    74
				c.listPorts = parseBoolean(readNext(arguments, i));
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    75
			} else if (option == OPTION_LIST_CONNECTIONS) {
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    76
				c.listConnections = parseBoolean(readNext(arguments, i));
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    77
			} else if (option == OPTION_LIST_MIDI_MESSAGES) {
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    78
				c.listMidiMessages = parseBoolean(readNext(arguments, i));
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		return c;
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	virtual ~CLIParser() {
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
};
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    89
const relpipe::common::type::StringX CLIParser::OPTION_CLIENT = L"--client";
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    90
const relpipe::common::type::StringX CLIParser::OPTION_PORT = L"--port";
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    91
const relpipe::common::type::StringX CLIParser::OPTION_CONNECT_TO = L"--connect-to";
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    92
const relpipe::common::type::StringX CLIParser::OPTION_REQUIRED_CONNECTIONS = L"--required-connections";
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    93
const relpipe::common::type::StringX CLIParser::OPTION_LIST_PORTS = L"--list-ports";
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    94
const relpipe::common::type::StringX CLIParser::OPTION_LIST_CONNECTIONS = L"--list-connections";
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    95
const relpipe::common::type::StringX CLIParser::OPTION_LIST_MIDI_MESSAGES = L"--list-midi-messages";
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
}