cadMousePro-daemon/src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 19 Aug 2019 17:27:19 +0200
branchv_0
changeset 8 465572518625
parent 7 src/CLIParser.h@148f8dd077e8
child 10 05dbed834852
permissions -rw-r--r--
move sources to a sub-project folder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * cadMousePro
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
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
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include "CLI.h"
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "CLIConfiguration.h"
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include "CLIException.h"
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
class CLIParser {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
private:
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	std::wstring readNext(std::vector<std::wstring> arguments, int& i) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
		if (i < arguments.size()) return arguments[i++];
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
		else throw CLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	bool parseBoolean(std::wstring value) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		if (value == L"true") return true;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		else if (value == L"false") return false;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		else throw CLIException(L"Unable to parse boolean value „" + value + L"“ – expecting „true“ or „false“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	Frequency parseFrequency(std::wstring value) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		if (value == L"125") return Frequency::Hz_0125;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		else if (value == L"250") return Frequency::Hz_0250;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		else if (value == L"500") return Frequency::Hz_0500;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		else if (value == L"1000") return Frequency::Hz_1000;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else throw CLIException(L"Unable to parse frequency value „" + value + L"“ – expecting „125“ or „250“ or „500“ or „1000“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	static const std::wstring OPTION_FREQUENCY;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	static const std::wstring OPTION_SMART_SCROLLING;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	static const std::wstring OPTION_LIFT_OFF_DETECTION;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	static const std::wstring OPTION_KEEP_FILE;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	CLIConfiguration parse(const std::vector<std::wstring>& arguments) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		CLIConfiguration c;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		for (int i = 0; i < arguments.size();) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			std::wstring option = readNext(arguments, i);
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			if (option == OPTION_FREQUENCY) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
				c.cadMouseConfig.setFrequency(parseFrequency(readNext(arguments, i)));
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			} else if (option == OPTION_SMART_SCROLLING) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
				c.cadMouseConfig.setSmartScrolling(parseBoolean(readNext(arguments, i)));
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			} else if (option == OPTION_LIFT_OFF_DETECTION) {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				c.cadMouseConfig.setLiftOffDetection(parseBoolean(readNext(arguments, i)));
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			} else throw CLIException(L"Unsupported CLI option: " + option, CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		return c;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	virtual ~CLIParser() {
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
};
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
const std::wstring CLIParser::OPTION_FREQUENCY = L"--frequency";
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
const std::wstring CLIParser::OPTION_SMART_SCROLLING = L"--smart-scrolling";
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
const std::wstring CLIParser::OPTION_LIFT_OFF_DETECTION = L"--lift-off-detection";