src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 04 Mar 2022 19:43:16 +0100
branchv_0
changeset 3 b71fc3b5e56b
parent 2 1eef3d465863
permissions -rw-r--r--
new option: --unlink-on-close (delete the MQ file at the end)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
2
1eef3d465863 update copyright year
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
e8205d9206fb new project relpipe-in-posixmq
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
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/typedefs.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace posixmq {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
e8205d9206fb new project relpipe-in-posixmq
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);
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    40
	/**
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    41
	 * TODO: use a common method
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
	 */
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    44
		if (value == L"true") return true;
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    45
		else if (value == L"false") return false;
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
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);
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    47
	}
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	static const relpipe::writer::string_t OPTION_RELATION;
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
	static const relpipe::writer::string_t OPTION_UNLINK_ON_CLOSE;
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
	static const relpipe::writer::string_t OPTION_QUEUE;
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		Configuration c;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		for (int i = 0; i < arguments.size();) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			relpipe::writer::string_t option = readNext(arguments, i);
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			if (option == OPTION_RELATION) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
				c.relation = readNext(arguments, i);
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
			} else if (option == OPTION_UNLINK_ON_CLOSE) {
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
				c.unlinkOnClose = parseBoolean(readNext(arguments, i));
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
			} else if (option == OPTION_QUEUE) {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
				c.queue = readNext(arguments, i);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
			} else if (option == OPTION_MESSAGE_COUNT) {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
				c.messageCount = std::stoull(readNext(arguments, i));
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		return c;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	virtual ~CLIParser() {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
};
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    81
const relpipe::writer::string_t CLIParser::OPTION_UNLINK_ON_CLOSE = L"--unlink-on-close";
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
const relpipe::writer::string_t CLIParser::OPTION_QUEUE = L"--queue";
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}