From 890ef5ed58a76eaf72b632835712c67ef15d4eea Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Wed, 3 Jul 2024 13:36:31 +0200 Subject: [PATCH] Added argumentparser --- tools/publish_cli/CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ tools/publish_cli/argumentparser.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/publish_cli/argumentparser.h | 39 +++++++++++++++++++++++++++++++++++++++ tools/publish_cli/argumentparserbase.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/publish_cli/argumentparserbase.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 232 insertions(+), 0 deletions(-) create mode 100644 tools/publish_cli/CMakeLists.txt create mode 100644 tools/publish_cli/argumentparserbase.cpp create mode 100644 tools/publish_cli/argumentparserbase.h diff --git a/tools/publish_cli/CMakeLists.txt b/tools/publish_cli/CMakeLists.txt new file mode 100644 index 0000000..8dd52f1 --- /dev/null +++ b/tools/publish_cli/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.12) +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) + +include(projectheader) +project_header(publish_cli) + +find_package( Boost REQUIRED COMPONENTS regex ) + +include(compiler) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../../include +) + +set(SRC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.h + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.h + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp +) + +include(library) +add_libraries( + PUBLIC + Boost::boost + Boost::regex + mqtt-cpp + paho-mqtt3a +) + +include(installation) +install_component() diff --git a/tools/publish_cli/argumentparser.cpp b/tools/publish_cli/argumentparser.cpp index e69de29..0d07e73 100644 --- a/tools/publish_cli/argumentparser.cpp +++ b/tools/publish_cli/argumentparser.cpp @@ -0,0 +1,54 @@ +/* **************************************************************************** + * Copyright 2024 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "argumentparser.h" + +#include + +std::string ArgumentParser::getHost() +{ + return getArgument("-h"); +} + +std::string ArgumentParser::getPort() +{ + return getArgument("-p"); +} + +std::string ArgumentParser::getUserName() +{ + return getArgument("-u"); +} + +std::string ArgumentParser::getPassword() +{ + return getArgument("-pw"); +} + +std::string ArgumentParser::getTopic() +{ + return getArgument("-t"); +} + +std::string ArgumentParser::getFile() +{ + return getArgument("-f");} +std::string ArgumentParser::getMessage(); \ No newline at end of file diff --git a/tools/publish_cli/argumentparser.h b/tools/publish_cli/argumentparser.h index e69de29..23fb5ca 100644 --- a/tools/publish_cli/argumentparser.h +++ b/tools/publish_cli/argumentparser.h @@ -0,0 +1,39 @@ +/* **************************************************************************** + * Copyright 2024 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#pragma once + +#include "argumentparserbase.h" + +class ArgumentParser : public ArgumentParserBase +{ +public: + ArgumentParser(int num_of_args, char* argv[]) : ArgumentParserBase(num_of_args, argv) {} + virtual ~ArgumentParser() {} + + std::string getHost(); + std::string getPort(); + std::string getUserName(); + std::string getPassword(); + std::string getTopic(); + std::string getFile(); + std::string getMessage(); +}; \ No newline at end of file diff --git a/tools/publish_cli/argumentparserbase.cpp b/tools/publish_cli/argumentparserbase.cpp new file mode 100644 index 0000000..2e1b861 --- /dev/null +++ b/tools/publish_cli/argumentparserbase.cpp @@ -0,0 +1,62 @@ +/* **************************************************************************** + * Copyright 2024 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ + +#include "argumentparserbase.h" + +ArgumentParserBase::ArgumentParserBase(int num_of_args, char* argv[]) +{ + // Store the program name + m_programName = argv[0]; + + // Store the arguments + for (int i = 1; i < num_of_args; i++) + { + std::string arg = argv[i]; + if (arg[0] == '-') + { + if (i + 1 < num_of_args) + { + m_arguments[arg] = argv[i + 1]; + } + else + { + m_arguments[arg] = ""; + } + } + } +} + +ArgumentParserBase::~ArgumentParserBase() +{ + m_arguments.clear(); + m_programName.clear(); +} + +std::string ArgumentParserBase::getArgument(const std::string &option) +{ + auto it = m_arguments.find(option); + if (it != m_arguments.end()) + { + return it->second; + } + return ""; +} diff --git a/tools/publish_cli/argumentparserbase.h b/tools/publish_cli/argumentparserbase.h new file mode 100644 index 0000000..f2b0e63 --- /dev/null +++ b/tools/publish_cli/argumentparserbase.h @@ -0,0 +1,44 @@ +/* **************************************************************************** + * Copyright 2024 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include +#include +#include + +/*! + * @brief ArgumentParserBase class + */ +class ArgumentParserBase +{ +public: + ArgumentParserBase(int num_of_args, char* argv[]); + virtual ~ArgumentParserBase(); + + /// @brief Returns the argument for the given option + std::string getArgument(const std::string &option); + + /// @brief Returns the number of arguments + int count() const { return m_arguments.size(); } + +private: // members (Giggity) + std::string m_programName; + std::unordered_map m_arguments; +}; \ No newline at end of file -- libgit2 0.21.4