# HG changeset patch # User František Kučera # Date 1566228062 -7200 # Node ID 148f8dd077e85825aa04dc6599b927e2c9c336fc # Parent 975f38eb1e123897c36e7e91b9fec2d4bad497c3 CLI options and parser of configuration parameters instead of hardcoded values diff -r 975f38eb1e12 -r 148f8dd077e8 src/CLI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLI.h Mon Aug 19 17:21:02 2019 +0200 @@ -0,0 +1,80 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +/** + * a common header-only library for CLI programs + */ +class CLI { +public: + + CLI(int argc, char* argv[]) { + setlocale(LC_ALL, ""); + + this->argc = &argc; + this->argv = &argv; + + program = convertor.from_bytes(argv[0]); + + for (int i = 1; i < argc; i++) { + args.insert(args.end(), convertor.from_bytes(argv[i])); + } + + } + + CLI(const CLI& orig) { + } + + virtual ~CLI() { + } + + const std::wstring programName() { + return (const std::wstring) program; + } + + const std::vector arguments() { + return (const std::vector)args; + } + + static const int EXIT_CODE_SUCCESS = 0; + static const int EXIT_CODE_UNEXPECTED_ERROR = 1; + static const int EXIT_CODE_BAD_CLI_ARGUMENTS = 6; + + /** + * std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout. + * Call this to "untie" them and have less write() calls (better buffering). + */ + static void untieStdIO() { + std::cin.tie(nullptr); + } + +private: + int* argc; + char*** argv; + std::wstring program; + std::vector args; + std::wstring_convert> convertor; // TODO: support also other encodings. +}; diff -r 975f38eb1e12 -r 148f8dd077e8 src/CLIConfiguration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIConfiguration.h Mon Aug 19 17:21:02 2019 +0200 @@ -0,0 +1,30 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include "CadMouseConfig.h" + +class CLIConfiguration { +public: + + + CadMouseConfig cadMouseConfig; + + virtual ~CLIConfiguration() { + } +}; diff -r 975f38eb1e12 -r 148f8dd077e8 src/CLIException.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIException.h Mon Aug 19 17:21:02 2019 +0200 @@ -0,0 +1,41 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include "CLI.h" + +class CLIException { +private: + std::wstring message; + int exitCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; +public: + + CLIException(std::wstring message, int exitCode) : message(message), exitCode(exitCode) { + } + + std::wstring getMessge() const { + return message; + } + + int getExitCode() const { + return exitCode; + } + +}; diff -r 975f38eb1e12 -r 148f8dd077e8 src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Mon Aug 19 17:21:02 2019 +0200 @@ -0,0 +1,80 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include "CLI.h" +#include "CLIConfiguration.h" +#include "CLIException.h" + +class CLIParser { +private: + + std::wstring readNext(std::vector arguments, int& i) { + if (i < arguments.size()) return arguments[i++]; + else throw CLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + bool parseBoolean(std::wstring value) { + if (value == L"true") return true; + else if (value == L"false") return false; + else throw CLIException(L"Unable to parse boolean value „" + value + L"“ – expecting „true“ or „false“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + Frequency parseFrequency(std::wstring value) { + if (value == L"125") return Frequency::Hz_0125; + else if (value == L"250") return Frequency::Hz_0250; + else if (value == L"500") return Frequency::Hz_0500; + else if (value == L"1000") return Frequency::Hz_1000; + 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); + } + +public: + + static const std::wstring OPTION_FREQUENCY; + static const std::wstring OPTION_SMART_SCROLLING; + static const std::wstring OPTION_LIFT_OFF_DETECTION; + static const std::wstring OPTION_KEEP_FILE; + + CLIConfiguration parse(const std::vector& arguments) { + CLIConfiguration c; + + for (int i = 0; i < arguments.size();) { + std::wstring option = readNext(arguments, i); + + if (option == OPTION_FREQUENCY) { + c.cadMouseConfig.setFrequency(parseFrequency(readNext(arguments, i))); + } else if (option == OPTION_SMART_SCROLLING) { + c.cadMouseConfig.setSmartScrolling(parseBoolean(readNext(arguments, i))); + } else if (option == OPTION_LIFT_OFF_DETECTION) { + c.cadMouseConfig.setLiftOffDetection(parseBoolean(readNext(arguments, i))); + } else throw CLIException(L"Unsupported CLI option: " + option, CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + return c; + } + + virtual ~CLIParser() { + } +}; + + +const std::wstring CLIParser::OPTION_FREQUENCY = L"--frequency"; +const std::wstring CLIParser::OPTION_SMART_SCROLLING = L"--smart-scrolling"; +const std::wstring CLIParser::OPTION_LIFT_OFF_DETECTION = L"--lift-off-detection"; diff -r 975f38eb1e12 -r 148f8dd077e8 src/cadMousePro.cpp --- a/src/cadMousePro.cpp Mon Aug 19 15:30:01 2019 +0200 +++ b/src/cadMousePro.cpp Mon Aug 19 17:21:02 2019 +0200 @@ -18,10 +18,13 @@ #include #include "CadMouseConfig.h" +#include "CLI.h" +#include "CLIParser.h" #include "HID.h" int main(int argc, char** argv) { try { + CLI cli(argc, argv); std::wcout << L"cadMousePro" << std::endl; HIDDevice mouse(0x256f, 0xc652, nullptr); @@ -30,13 +33,18 @@ std::wcout << L"product: " << mouse.getProductName() << std::endl; // std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception - CadMouseConfig config; + CLIParser cliParser; + CLIConfiguration configuration = cliParser.parse(cli.arguments()); - mouse.sendFeatureReport(config.serialize()); + mouse.sendFeatureReport(configuration.cadMouseConfig.serialize()); - return 0; + return CLI::EXIT_CODE_SUCCESS; } catch (const HIDException& e) { std::wcout << L"HIDException: " << e.getMessage() << std::endl; - return 1; + return CLI::EXIT_CODE_UNEXPECTED_ERROR; + } catch (const CLIException& e) { + std::wcout << L"CLIException: " << e.getMessge() << std::endl; + return e.getExitCode(); } + }