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