Commit 890ef5ed58a76eaf72b632835712c67ef15d4eea
1 parent
6a79aebe
Added argumentparser
Showing
5 changed files
with
232 additions
and
0 deletions
tools/publish_cli/CMakeLists.txt
0 → 100644
| 1 | +cmake_minimum_required(VERSION 3.12) | |
| 2 | +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) | |
| 3 | + | |
| 4 | +include(projectheader) | |
| 5 | +project_header(publish_cli) | |
| 6 | + | |
| 7 | +find_package( Boost REQUIRED COMPONENTS regex ) | |
| 8 | + | |
| 9 | +include(compiler) | |
| 10 | + | |
| 11 | +include_directories( | |
| 12 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../include | |
| 13 | +) | |
| 14 | + | |
| 15 | +set(SRC_LIST | |
| 16 | + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.h | |
| 17 | + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.cpp | |
| 18 | + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.h | |
| 19 | + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.cpp | |
| 20 | + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp | |
| 21 | +) | |
| 22 | + | |
| 23 | +include(library) | |
| 24 | +add_libraries( | |
| 25 | + PUBLIC | |
| 26 | + Boost::boost | |
| 27 | + Boost::regex | |
| 28 | + mqtt-cpp | |
| 29 | + paho-mqtt3a | |
| 30 | +) | |
| 31 | + | |
| 32 | +include(installation) | |
| 33 | +install_component() | ... | ... |
tools/publish_cli/argumentparser.cpp
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2024 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#include "argumentparser.h" | |
| 23 | + | |
| 24 | +#include <iostream> | |
| 25 | + | |
| 26 | +std::string ArgumentParser::getHost() | |
| 27 | +{ | |
| 28 | + return getArgument("-h"); | |
| 29 | +} | |
| 30 | + | |
| 31 | +std::string ArgumentParser::getPort() | |
| 32 | +{ | |
| 33 | + return getArgument("-p"); | |
| 34 | +} | |
| 35 | + | |
| 36 | +std::string ArgumentParser::getUserName() | |
| 37 | +{ | |
| 38 | + return getArgument("-u"); | |
| 39 | +} | |
| 40 | + | |
| 41 | +std::string ArgumentParser::getPassword() | |
| 42 | +{ | |
| 43 | + return getArgument("-pw"); | |
| 44 | +} | |
| 45 | + | |
| 46 | +std::string ArgumentParser::getTopic() | |
| 47 | +{ | |
| 48 | + return getArgument("-t"); | |
| 49 | +} | |
| 50 | + | |
| 51 | +std::string ArgumentParser::getFile() | |
| 52 | +{ | |
| 53 | + return getArgument("-f");} | |
| 54 | +std::string ArgumentParser::getMessage(); | |
| 0 | 55 | \ No newline at end of file | ... | ... |
tools/publish_cli/argumentparser.h
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2024 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#pragma once | |
| 23 | + | |
| 24 | +#include "argumentparserbase.h" | |
| 25 | + | |
| 26 | +class ArgumentParser : public ArgumentParserBase | |
| 27 | +{ | |
| 28 | +public: | |
| 29 | + ArgumentParser(int num_of_args, char* argv[]) : ArgumentParserBase(num_of_args, argv) {} | |
| 30 | + virtual ~ArgumentParser() {} | |
| 31 | + | |
| 32 | + std::string getHost(); | |
| 33 | + std::string getPort(); | |
| 34 | + std::string getUserName(); | |
| 35 | + std::string getPassword(); | |
| 36 | + std::string getTopic(); | |
| 37 | + std::string getFile(); | |
| 38 | + std::string getMessage(); | |
| 39 | +}; | |
| 0 | 40 | \ No newline at end of file | ... | ... |
tools/publish_cli/argumentparserbase.cpp
0 → 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2024 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | + | |
| 23 | +#include "argumentparserbase.h" | |
| 24 | + | |
| 25 | +ArgumentParserBase::ArgumentParserBase(int num_of_args, char* argv[]) | |
| 26 | +{ | |
| 27 | + // Store the program name | |
| 28 | + m_programName = argv[0]; | |
| 29 | + | |
| 30 | + // Store the arguments | |
| 31 | + for (int i = 1; i < num_of_args; i++) | |
| 32 | + { | |
| 33 | + std::string arg = argv[i]; | |
| 34 | + if (arg[0] == '-') | |
| 35 | + { | |
| 36 | + if (i + 1 < num_of_args) | |
| 37 | + { | |
| 38 | + m_arguments[arg] = argv[i + 1]; | |
| 39 | + } | |
| 40 | + else | |
| 41 | + { | |
| 42 | + m_arguments[arg] = ""; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + } | |
| 46 | +} | |
| 47 | + | |
| 48 | +ArgumentParserBase::~ArgumentParserBase() | |
| 49 | +{ | |
| 50 | + m_arguments.clear(); | |
| 51 | + m_programName.clear(); | |
| 52 | +} | |
| 53 | + | |
| 54 | +std::string ArgumentParserBase::getArgument(const std::string &option) | |
| 55 | +{ | |
| 56 | + auto it = m_arguments.find(option); | |
| 57 | + if (it != m_arguments.end()) | |
| 58 | + { | |
| 59 | + return it->second; | |
| 60 | + } | |
| 61 | + return ""; | |
| 62 | +} | ... | ... |
tools/publish_cli/argumentparserbase.h
0 → 100644
| 1 | +/* **************************************************************************** | |
| 2 | + * Copyright 2024 Open Systems Development BV * | |
| 3 | + * * | |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a * | |
| 5 | + * copy of this software and associated documentation files (the "Software"), * | |
| 6 | + * to deal in the Software without restriction, including without limitation * | |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * | |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the * | |
| 9 | + * Software is furnished to do so, subject to the following conditions: * | |
| 10 | + * * | |
| 11 | + * The above copyright notice and this permission notice shall be included in * | |
| 12 | + * all copies or substantial portions of the Software. * | |
| 13 | + * * | |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * | |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * | |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * | |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * | |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * | |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * | |
| 20 | + * DEALINGS IN THE SOFTWARE. * | |
| 21 | + * ***************************************************************************/ | |
| 22 | +#include <iostream> | |
| 23 | +#include <string> | |
| 24 | +#include <unordered_map> | |
| 25 | + | |
| 26 | +/*! | |
| 27 | + * @brief ArgumentParserBase class | |
| 28 | + */ | |
| 29 | +class ArgumentParserBase | |
| 30 | +{ | |
| 31 | +public: | |
| 32 | + ArgumentParserBase(int num_of_args, char* argv[]); | |
| 33 | + virtual ~ArgumentParserBase(); | |
| 34 | + | |
| 35 | + /// @brief Returns the argument for the given option | |
| 36 | + std::string getArgument(const std::string &option); | |
| 37 | + | |
| 38 | + /// @brief Returns the number of arguments | |
| 39 | + int count() const { return m_arguments.size(); } | |
| 40 | + | |
| 41 | +private: // members (Giggity) | |
| 42 | + std::string m_programName; | |
| 43 | + std::unordered_map<std::string, std::string> m_arguments; | |
| 44 | +}; | |
| 0 | 45 | \ No newline at end of file | ... | ... |