From 0e798da6d41efb396bb99202b917a4f7e9f3c973 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Tue, 26 Jan 2021 15:46:49 +0100 Subject: [PATCH 01/12] iox-#404 Pass cmdLineArgs to RouDiApp instead of cmdLineParser Signed-off-by: Simon Hoinkis --- .../iceperf/roudi_main_static_config.cpp | 6 +- .../iceoryx_posh/roudi/cmd_line_args.hpp | 59 +++++++++++++++++++ .../iceoryx_posh/roudi/iceoryx_roudi_app.hpp | 2 +- .../include/iceoryx_posh/roudi/roudi_app.hpp | 17 +----- .../roudi/roudi_cmd_line_parser.hpp | 11 ++-- ...udi_cmd_line_parser_config_file_option.hpp | 10 ++-- .../roudi/roudi_config_toml_file_provider.hpp | 4 +- .../roudi/application/iceoryx_roudi_app.cpp | 4 +- .../source/roudi/application/roudi_app.cpp | 59 ++++++++----------- .../source/roudi/application/roudi_main.cpp | 9 ++- .../source/roudi/roudi_cmd_line_parser.cpp | 16 +---- ...udi_cmd_line_parser_config_file_option.cpp | 19 +++--- .../roudi/roudi_config_toml_file_provider.cpp | 8 +-- 13 files changed, 123 insertions(+), 101 deletions(-) create mode 100644 iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp diff --git a/iceoryx_examples/iceperf/roudi_main_static_config.cpp b/iceoryx_examples/iceperf/roudi_main_static_config.cpp index 36b7e7326a..dedc79ebc1 100644 --- a/iceoryx_examples/iceperf/roudi_main_static_config.cpp +++ b/iceoryx_examples/iceperf/roudi_main_static_config.cpp @@ -25,7 +25,7 @@ int main(int argc, char* argv[]) static constexpr uint32_t ONE_MEGABYTE = 1024U * 1024; iox::config::CmdLineParserConfigFileOption cmdLineParser; - cmdLineParser.parse(argc, argv); + iox::config::CmdLineArgs_t cmdLineArgs = cmdLineParser.parse(argc, argv); iox::RouDiConfig_t roudiConfig; // roudiConfig.setDefaults(); can be used if you want to use the default config only. @@ -61,9 +61,7 @@ int main(int argc, char* argv[]) /// mempoolConfig}) /// @endcode - cmdLineParser.printParameters(); - - IceOryxRouDiApp roudi(cmdLineParser, roudiConfig); + IceOryxRouDiApp roudi(cmdLineArgs, roudiConfig); return roudi.run(); } diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp new file mode 100644 index 0000000000..9862dc2285 --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp @@ -0,0 +1,59 @@ +// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP +#define IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP + +#include "iceoryx_posh/iceoryx_posh_types.hpp" +#include "iceoryx_posh/version/compatibility_check_level.hpp" +#include "iceoryx_utils/log/logstream.hpp" + +#include + +namespace iox +{ +namespace config +{ +struct CmdLineArgs_t +{ + roudi::MonitoringMode monitoringMode{roudi::MonitoringMode::ON}; + iox::log::LogLevel logLevel{iox::log::LogLevel::kWarn}; + version::CompatibilityCheckLevel compatibilityCheckLevel{version::CompatibilityCheckLevel::PATCH}; + units::Duration processKillDelay{roudi::PROCESS_DEFAULT_KILL_DELAY}; + cxx::optional uniqueRouDiId{0}; + bool run{false}; + roudi::ConfigFilePathString_t configFilePath; +}; + +inline iox::log::LogStream& operator<<(iox::log::LogStream& logstream, const CmdLineArgs_t& cmdLineArgs) noexcept +{ + logstream << "Log level: " << cmdLineArgs.logLevel << "\n"; + logstream << "Monitoring mode: " << cmdLineArgs.monitoringMode << "\n"; + logstream << "Compatibility check level: " << cmdLineArgs.compatibilityCheckLevel << "\n"; + cmdLineArgs.uniqueRouDiId.and_then([&logstream](auto& id) { logstream << "Unique RouDi ID: " << id << "\n"; }) + .or_else([&logstream] { logstream << "Unique RouDi ID: < unset >\n"; }); + logstream << "Process kill delay: " << cmdLineArgs.processKillDelay.seconds() << " ms\n"; + if (!cmdLineArgs.configFilePath.empty()) + { + logstream << "Config file used is: " << cmdLineArgs.configFilePath; + } + else + { + logstream << "Config file used is: < none >"; + } + return logstream; +} +} // namespace config +} // namespace iox + +#endif // IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp index 45b2d9cc45..460d771881 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp @@ -26,7 +26,7 @@ class IceOryxRouDiApp : public RouDiApp /// @brief constructor to create the RouDi daemon with a given config /// @param[in] Command liner parser object, that provides the settings /// @param[in] RouDi config for mempool configuration - IceOryxRouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& roudiConfig) noexcept; + IceOryxRouDiApp(const iox::config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept; /// @brief starts the execution of the RouDi daemon /// @return Return code for programm execution diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp index e5dad4a5ad..84eaacdcd0 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp @@ -16,9 +16,7 @@ #include "iceoryx_posh/iceoryx_posh_config.hpp" #include "iceoryx_posh/mepoo/mepoo_config.hpp" -#include "iceoryx_posh/roudi/roudi_cmd_line_parser.hpp" -#include "iceoryx_posh/roudi/roudi_config_file_provider.hpp" -#include "iceoryx_posh/version/compatibility_check_level.hpp" +#include "iceoryx_posh/roudi/cmd_line_args.hpp" #include "iceoryx_utils/log/logcommon.hpp" #include "iceoryx_utils/posix_wrapper/semaphore.hpp" @@ -39,7 +37,7 @@ class RouDiApp /// @brief C'tor with command line parser, which has already parsed the command line parameters /// @param[in] cmdLineParser reference to a command line parser object /// @param[in] config the configuration to use - RouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& config) noexcept; + RouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& config) noexcept; virtual ~RouDiApp() noexcept {}; @@ -55,17 +53,6 @@ class RouDiApp /// @brief Tells the OS which signals shall be hooked void registerSigHandler() noexcept; - void parseCmdLineArguments(int argc, - char* argv[], - config::CmdLineParser::CmdLineArgumentParsingMode cmdLineParsingMode = - config::CmdLineParser::CmdLineArgumentParsingMode::ALL) noexcept; - - /// @brief Extracts from CmdLineParser and sets them - void setCmdLineParserResults(const config::CmdLineParser& cmdLineParser) noexcept; - - /// @brief initialize the RouDi daemon - void init() noexcept; - /// @brief waits for the next signal to RouDi daemon bool waitForSignal() const noexcept; diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index 058c6990b6..ee02dfeb72 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -15,6 +15,7 @@ #define IOX_POSH_ROUDI_ROUDI_CMD_LINE_PARSER_HPP #include "iceoryx_posh/iceoryx_posh_types.hpp" +#include "iceoryx_posh/roudi/cmd_line_args.hpp" #include "iceoryx_posh/version/compatibility_check_level.hpp" #include "iceoryx_utils/cxx/expected.hpp" #include "iceoryx_utils/cxx/optional.hpp" @@ -45,11 +46,11 @@ class CmdLineParser /// @param[in] argc forwarding of command line arguments /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options - virtual void parse(int argc, - char* argv[], - const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept; - - void printParameters() const noexcept; + /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct + virtual CmdLineArgs_t + parse(int argc, + char* argv[], + const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept; bool getRun() const noexcept; iox::log::LogLevel getLogLevel() const noexcept; diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp index 2d81553fe1..924672b097 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp @@ -34,14 +34,14 @@ class CmdLineParserConfigFileOption : public CmdLineParser /// @param[in] argc forwarding of command line arguments /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options - void parse(int argc, - char* argv[], - const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override; + /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct + CmdLineArgs_t + parse(int argc, + char* argv[], + const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override; roudi::ConfigFilePathString_t getConfigFilePath() const; - void printParameters() noexcept; - protected: roudi::ConfigFilePathString_t m_customConfigFilePath; }; diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp index d8e13b5a01..ab27e1127e 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp @@ -14,7 +14,7 @@ #ifndef IOX_POSH_ROUDI_ROUDI_CONFIG_TOML_FILE_PROVIDER_HPP #define IOX_POSH_ROUDI_ROUDI_CONFIG_TOML_FILE_PROVIDER_HPP -#include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp" +#include "iceoryx_posh/roudi/cmd_line_args.hpp" #include "iceoryx_posh/roudi/roudi_config_file_provider.hpp" namespace iox @@ -26,7 +26,7 @@ static constexpr char defaultConfigFilePath[] = "/etc/iceoryx/roudi_config.toml" class TomlRouDiConfigFileProvider : public iox::roudi::RouDiConfigFileProvider { public: - TomlRouDiConfigFileProvider(CmdLineParserConfigFileOption& cmdLineParserValue); + TomlRouDiConfigFileProvider(iox::config::CmdLineArgs_t& cmdLineArgs); iox::cxx::expected parse() override; }; diff --git a/iceoryx_posh/source/roudi/application/iceoryx_roudi_app.cpp b/iceoryx_posh/source/roudi/application/iceoryx_roudi_app.cpp index c4c2b536b0..afd3b206bd 100644 --- a/iceoryx_posh/source/roudi/application/iceoryx_roudi_app.cpp +++ b/iceoryx_posh/source/roudi/application/iceoryx_roudi_app.cpp @@ -23,8 +23,8 @@ namespace iox { namespace roudi { -IceOryxRouDiApp::IceOryxRouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& roudiConfig) noexcept - : RouDiApp(cmdLineParser, roudiConfig) +IceOryxRouDiApp::IceOryxRouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept + : RouDiApp(cmdLineArgs, roudiConfig) { } diff --git a/iceoryx_posh/source/roudi/application/roudi_app.cpp b/iceoryx_posh/source/roudi/application/roudi_app.cpp index fae76e9418..32f0c73157 100644 --- a/iceoryx_posh/source/roudi/application/roudi_app.cpp +++ b/iceoryx_posh/source/roudi/application/roudi_app.cpp @@ -18,6 +18,7 @@ #include "iceoryx_posh/internal/log/posh_logging.hpp" #include "iceoryx_posh/internal/popo/building_blocks/typed_unique_id.hpp" #include "iceoryx_posh/internal/roudi/roudi.hpp" +#include "iceoryx_posh/roudi/cmd_line_args.hpp" #include "iceoryx_utils/cxx/helplets.hpp" #include "iceoryx_utils/cxx/optional.hpp" #include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/memory_map.hpp" @@ -90,17 +91,30 @@ void RouDiApp::registerSigHandler() noexcept } } -RouDiApp::RouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& config) noexcept - : RouDiApp(config) -{ - setCmdLineParserResults(cmdLineParser); - init(); -} - -RouDiApp::RouDiApp(const RouDiConfig_t& config) noexcept - : m_run(checkAndOptimizeConfig(config)) +RouDiApp::RouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& config) noexcept + : m_logLevel(cmdLineArgs.logLevel) + , m_monitoringMode(cmdLineArgs.monitoringMode) + , m_run(checkAndOptimizeConfig(config)) , m_config(config) + , m_compatibilityCheckLevel(cmdLineArgs.compatibilityCheckLevel) + , m_processKillDelay(cmdLineArgs.processKillDelay) { + // the "and" is intentional, just in case the the provided RouDiConfig_t is empty + m_run &= cmdLineArgs.run; + if (cmdLineArgs.uniqueRouDiId) + { + popo::internal::setUniqueRouDiId(cmdLineArgs.uniqueRouDiId.value()); + } + + // be silent if not running + if (m_run) + { + iox::log::LogManager::GetLogManager().SetDefaultLogLevel(m_logLevel); + + registerSigHandler(); + + LogVerbose() << "Command line parameters are:\n" << cmdLineArgs; + } } bool RouDiApp::checkAndOptimizeConfig(const RouDiConfig_t& config) noexcept @@ -123,37 +137,10 @@ bool RouDiApp::checkAndOptimizeConfig(const RouDiConfig_t& config) noexcept return true; } -void RouDiApp::init() noexcept -{ - // be silent if not running - if (m_run) - { - iox::log::LogManager::GetLogManager().SetDefaultLogLevel(m_logLevel); - - registerSigHandler(); - } -} - bool RouDiApp::waitForSignal() const noexcept { return !m_semaphore.wait().has_error(); } -void RouDiApp::setCmdLineParserResults(const config::CmdLineParser& cmdLineParser) noexcept -{ - m_monitoringMode = cmdLineParser.getMonitoringMode(); - m_logLevel = cmdLineParser.getLogLevel(); - // the "and" is intentional, just in case the the provided RouDiConfig_t is empty - m_run &= cmdLineParser.getRun(); - m_compatibilityCheckLevel = cmdLineParser.getCompatibilityCheckLevel(); - m_processKillDelay = cmdLineParser.getProcessKillDelay(); - auto uniqueId = cmdLineParser.getUniqueRouDiId(); - if (uniqueId) - { - popo::internal::setUniqueRouDiId(*uniqueId); - } -} - - } // namespace roudi } // namespace iox diff --git a/iceoryx_posh/source/roudi/application/roudi_main.cpp b/iceoryx_posh/source/roudi/application/roudi_main.cpp index 10f0bc32c2..6568fb160c 100644 --- a/iceoryx_posh/source/roudi/application/roudi_main.cpp +++ b/iceoryx_posh/source/roudi/application/roudi_main.cpp @@ -15,6 +15,7 @@ #include "iceoryx_posh/iceoryx_posh_config.hpp" #include "iceoryx_posh/iceoryx_posh_types.hpp" #include "iceoryx_posh/internal/log/posh_logging.hpp" +#include "iceoryx_posh/roudi/cmd_line_args.hpp" #include "iceoryx_posh/roudi/iceoryx_roudi_app.hpp" #include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp" #include "iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp" @@ -24,9 +25,9 @@ int main(int argc, char* argv[]) using iox::roudi::IceOryxRouDiApp; iox::config::CmdLineParserConfigFileOption cmdLineParser; - cmdLineParser.parse(argc, argv); + iox::config::CmdLineArgs_t cmdLineArgs = cmdLineParser.parse(argc, argv); - iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineParser); + iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs); iox::RouDiConfig_t roudiConfig = configFileProvider.parse() @@ -38,9 +39,7 @@ int main(int argc, char* argv[]) }) .value(); - cmdLineParser.printParameters(); - - IceOryxRouDiApp roudi(cmdLineParser, roudiConfig); + IceOryxRouDiApp roudi(cmdLineArgs, roudiConfig); return roudi.run(); } diff --git a/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp b/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp index d3dd2cd8f5..fa77b87408 100644 --- a/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp +++ b/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp @@ -24,7 +24,7 @@ namespace iox { namespace config { -void CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept +CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept { constexpr option longOptions[] = {{"help", no_argument, nullptr, 'h'}, {"version", no_argument, nullptr, 'v'}, @@ -209,6 +209,8 @@ void CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMo break; } } + return CmdLineArgs_t{ + m_monitoringMode, m_logLevel, m_compatibilityCheckLevel, m_processKillDelay, m_uniqueRouDiId, m_run, ""}; } // namespace roudi bool CmdLineParser::getRun() const noexcept { @@ -238,17 +240,5 @@ units::Duration CmdLineParser::getProcessKillDelay() const noexcept return m_processKillDelay; } -void CmdLineParser::printParameters() const noexcept -{ - LogVerbose() << "Command line parameters are.."; - LogVerbose() << "Log level: " << m_logLevel; - LogVerbose() << "Monitoring mode: " << m_monitoringMode; - LogVerbose() << "Compatibility check level: " << m_compatibilityCheckLevel; - m_uniqueRouDiId.and_then([](auto& id) { LogVerbose() << "Unique RouDi ID: " << id; }).or_else([] { - LogVerbose() << "Unique RouDi ID: < unset >"; - }); - LogVerbose() << "Process kill delay: " << m_processKillDelay.seconds() << " ms"; -} - } // namespace config } // namespace iox diff --git a/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp b/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp index 4583704e35..c0641336ad 100644 --- a/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp +++ b/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp @@ -23,9 +23,9 @@ namespace iox { namespace config { -void CmdLineParserConfigFileOption::parse(int argc, - char* argv[], - const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept +CmdLineArgs_t CmdLineParserConfigFileOption::parse(int argc, + char* argv[], + const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept { constexpr option longOptions[] = {{"help", no_argument, nullptr, 'h'}, {"config-file", required_argument, nullptr, 'c'}, @@ -72,17 +72,18 @@ void CmdLineParserConfigFileOption::parse(int argc, break; } } + return CmdLineArgs_t{m_monitoringMode, + m_logLevel, + m_compatibilityCheckLevel, + m_processKillDelay, + m_uniqueRouDiId, + m_run, + m_customConfigFilePath}; } roudi::ConfigFilePathString_t CmdLineParserConfigFileOption::getConfigFilePath() const { return m_customConfigFilePath; } -void CmdLineParserConfigFileOption::printParameters() noexcept -{ - CmdLineParser::printParameters(); - LogVerbose() << "Config file used is: " << m_customConfigFilePath; -} - } // namespace config } // namespace iox diff --git a/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp b/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp index 8102e5da2b..69b09f5d07 100644 --- a/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp +++ b/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp @@ -28,12 +28,12 @@ namespace iox { namespace config { -TomlRouDiConfigFileProvider::TomlRouDiConfigFileProvider(CmdLineParserConfigFileOption& cmdLineParser) +TomlRouDiConfigFileProvider::TomlRouDiConfigFileProvider(config::CmdLineArgs_t& cmdLineArgs) { /// don't print additional output if not running - if (cmdLineParser.getRun()) + if (cmdLineArgs.run) { - if (cmdLineParser.getConfigFilePath().size() == 0) + if (cmdLineArgs.configFilePath.size() == 0) { /// @todo Replace with C++17 std::filesystem::exists() cxx::FileReader configFile(defaultConfigFilePath, "", cxx::FileReader::ErrorMode::Ignore); @@ -49,7 +49,7 @@ TomlRouDiConfigFileProvider::TomlRouDiConfigFileProvider(CmdLineParserConfigFile << "'. Falling back to built-in config."; } } - m_customConfigFilePath = cmdLineParser.getConfigFilePath(); + m_customConfigFilePath = cmdLineArgs.configFilePath; } } From ab6db2ec0cc7676425fc031184e26df7913e9c52 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Tue, 26 Jan 2021 15:50:35 +0100 Subject: [PATCH 02/12] iox-#404 Remove unecessary namespace Signed-off-by: Simon Hoinkis --- iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp index 460d771881..ca86823c1a 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/iceoryx_roudi_app.hpp @@ -26,7 +26,7 @@ class IceOryxRouDiApp : public RouDiApp /// @brief constructor to create the RouDi daemon with a given config /// @param[in] Command liner parser object, that provides the settings /// @param[in] RouDi config for mempool configuration - IceOryxRouDiApp(const iox::config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept; + IceOryxRouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept; /// @brief starts the execution of the RouDi daemon /// @return Return code for programm execution From afd04d6f016268f8d4f4d7261287de6f6314a787 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 27 Jan 2021 11:51:46 +0100 Subject: [PATCH 03/12] iox-#404 Remove out-dated method in RouDi app Signed-off-by: Simon Hoinkis --- iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp index 84eaacdcd0..b9633dad55 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp @@ -46,10 +46,6 @@ class RouDiApp virtual uint8_t run() noexcept = 0; protected: - /// @brief this is needed for the child classes for custom CmdLineParser - /// @param[in] config the configuration to use - RouDiApp(const RouDiConfig_t& config) noexcept; - /// @brief Tells the OS which signals shall be hooked void registerSigHandler() noexcept; From 28f571595b2e8784026363b31056985fba072d7b Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 27 Jan 2021 14:51:06 +0100 Subject: [PATCH 04/12] iox-#404 Add missing copyright info Signed-off-by: Simon Hoinkis --- iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp index 9862dc2285..ac827620d9 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved. +// Copyright (c) 2019, 2021 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 026f92e4dd7f876009a0171d5b0efd9244e7bc87 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Thu, 28 Jan 2021 19:43:09 +0100 Subject: [PATCH 05/12] iox-#404 Address review findings from Christian Eltzschig Signed-off-by: Simon Hoinkis --- iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp | 2 +- iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp index ac827620d9..521481dedf 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp @@ -30,7 +30,7 @@ struct CmdLineArgs_t iox::log::LogLevel logLevel{iox::log::LogLevel::kWarn}; version::CompatibilityCheckLevel compatibilityCheckLevel{version::CompatibilityCheckLevel::PATCH}; units::Duration processKillDelay{roudi::PROCESS_DEFAULT_KILL_DELAY}; - cxx::optional uniqueRouDiId{0}; + cxx::optional uniqueRouDiId; bool run{false}; roudi::ConfigFilePathString_t configFilePath; }; diff --git a/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp b/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp index 69b09f5d07..7563bff776 100644 --- a/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp +++ b/iceoryx_posh/source/roudi/roudi_config_toml_file_provider.cpp @@ -33,7 +33,7 @@ TomlRouDiConfigFileProvider::TomlRouDiConfigFileProvider(config::CmdLineArgs_t& /// don't print additional output if not running if (cmdLineArgs.run) { - if (cmdLineArgs.configFilePath.size() == 0) + if (cmdLineArgs.configFilePath.empty()) { /// @todo Replace with C++17 std::filesystem::exists() cxx::FileReader configFile(defaultConfigFilePath, "", cxx::FileReader::ErrorMode::Ignore); @@ -56,7 +56,7 @@ TomlRouDiConfigFileProvider::TomlRouDiConfigFileProvider(config::CmdLineArgs_t& iox::cxx::expected TomlRouDiConfigFileProvider::parse() { // Early exit in case no config file path was provided - if (m_customConfigFilePath.size() == 0) + if (m_customConfigFilePath.empty()) { iox::RouDiConfig_t defaultConfig; defaultConfig.setDefaults(); From f116f7178e276b630e0d9de31dc53452a8248f41 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Fri, 29 Jan 2021 18:18:54 +0100 Subject: [PATCH 06/12] iox-#404 Fix copyright header Signed-off-by: Simon Hoinkis --- .../include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index ee02dfeb72..f87ad92030 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved. +// Copyright (c) 2021 by Apex.AI Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From c582303295a28fcbad40fb7e8bae3919becc4fa8 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Tue, 2 Feb 2021 11:53:53 +0100 Subject: [PATCH 07/12] iox-#404 Add unit test for CmdLineParser* Signed-off-by: Simon Hoinkis --- .../iceperf/roudi_main_static_config.cpp | 10 +- .../iceoryx_posh/roudi/cmd_line_args.hpp | 4 +- .../roudi/roudi_cmd_line_parser.hpp | 15 +- ...udi_cmd_line_parser_config_file_option.hpp | 4 +- .../source/roudi/application/roudi_main.cpp | 12 +- .../source/roudi/roudi_cmd_line_parser.cpp | 41 +- ...udi_cmd_line_parser_config_file_option.cpp | 29 +- .../test_roudi_cmd_line_parser.cpp | 389 ++++++++++++++++++ ...udi_cmd_line_parser_config_file_option.cpp | 102 +++++ 9 files changed, 540 insertions(+), 66 deletions(-) create mode 100644 iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp create mode 100644 iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp diff --git a/iceoryx_examples/iceperf/roudi_main_static_config.cpp b/iceoryx_examples/iceperf/roudi_main_static_config.cpp index dedc79ebc1..8df37c5af4 100644 --- a/iceoryx_examples/iceperf/roudi_main_static_config.cpp +++ b/iceoryx_examples/iceperf/roudi_main_static_config.cpp @@ -25,7 +25,13 @@ int main(int argc, char* argv[]) static constexpr uint32_t ONE_MEGABYTE = 1024U * 1024; iox::config::CmdLineParserConfigFileOption cmdLineParser; - iox::config::CmdLineArgs_t cmdLineArgs = cmdLineParser.parse(argc, argv); + auto cmdLineArgs = cmdLineParser.parse(argc, argv).or_else([](iox::config::CmdLineParserResult& error) { + if (error == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) + { + iox::LogFatal() << "Unable to parse command line arguments!"; + std::terminate(); + } + }); iox::RouDiConfig_t roudiConfig; // roudiConfig.setDefaults(); can be used if you want to use the default config only. @@ -61,7 +67,7 @@ int main(int argc, char* argv[]) /// mempoolConfig}) /// @endcode - IceOryxRouDiApp roudi(cmdLineArgs, roudiConfig); + IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig); return roudi.run(); } diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp index 67dacbfaa2..0bc31d85a4 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp @@ -30,8 +30,8 @@ struct CmdLineArgs_t iox::log::LogLevel logLevel{iox::log::LogLevel::kWarn}; version::CompatibilityCheckLevel compatibilityCheckLevel{version::CompatibilityCheckLevel::PATCH}; units::Duration processKillDelay{roudi::PROCESS_DEFAULT_KILL_DELAY}; - cxx::optional uniqueRouDiId; - bool run{false}; + cxx::optional uniqueRouDiId{cxx::nullopt}; + bool run{true}; roudi::ConfigFilePathString_t configFilePath; }; diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index f87ad92030..01b4bc03e1 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -26,6 +26,12 @@ namespace iox { namespace config { +enum class CmdLineParserResult +{ + UNKNOWN_OPTION_USED, + USAGE_OUTPUT_REQUESTED /// @todo use this instead of CmdLineArgs_t.run after modularisation of RouDi +}; + class CmdLineParser { public: @@ -47,18 +53,11 @@ class CmdLineParser /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct - virtual CmdLineArgs_t + virtual cxx::expected parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept; - bool getRun() const noexcept; - iox::log::LogLevel getLogLevel() const noexcept; - roudi::MonitoringMode getMonitoringMode() const noexcept; - version::CompatibilityCheckLevel getCompatibilityCheckLevel() const noexcept; - cxx::optional getUniqueRouDiId() const noexcept; - units::Duration getProcessKillDelay() const noexcept; - protected: bool m_run{true}; iox::log::LogLevel m_logLevel{iox::log::LogLevel::kWarn}; diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp index 924672b097..d199861029 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp @@ -35,13 +35,11 @@ class CmdLineParserConfigFileOption : public CmdLineParser /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct - CmdLineArgs_t + cxx::expected parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override; - roudi::ConfigFilePathString_t getConfigFilePath() const; - protected: roudi::ConfigFilePathString_t m_customConfigFilePath; }; diff --git a/iceoryx_posh/source/roudi/application/roudi_main.cpp b/iceoryx_posh/source/roudi/application/roudi_main.cpp index 6568fb160c..7ca68ea1f4 100644 --- a/iceoryx_posh/source/roudi/application/roudi_main.cpp +++ b/iceoryx_posh/source/roudi/application/roudi_main.cpp @@ -25,9 +25,15 @@ int main(int argc, char* argv[]) using iox::roudi::IceOryxRouDiApp; iox::config::CmdLineParserConfigFileOption cmdLineParser; - iox::config::CmdLineArgs_t cmdLineArgs = cmdLineParser.parse(argc, argv); + auto cmdLineArgs = cmdLineParser.parse(argc, argv).or_else([](iox::config::CmdLineParserResult& error) { + if (error == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) + { + iox::LogFatal() << "Unable to parse command line arguments!"; + std::terminate(); + } + }); - iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs); + iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs.value()); iox::RouDiConfig_t roudiConfig = configFileProvider.parse() @@ -39,7 +45,7 @@ int main(int argc, char* argv[]) }) .value(); - IceOryxRouDiApp roudi(cmdLineArgs, roudiConfig); + IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig); return roudi.run(); } diff --git a/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp b/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp index fa77b87408..29e972d4d2 100644 --- a/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp +++ b/iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp @@ -24,20 +24,20 @@ namespace iox { namespace config { -CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept +cxx::expected +CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept { constexpr option longOptions[] = {{"help", no_argument, nullptr, 'h'}, {"version", no_argument, nullptr, 'v'}, {"monitoring-mode", required_argument, nullptr, 'm'}, {"log-level", required_argument, nullptr, 'l'}, - {"ignore-version", required_argument, nullptr, 'i'}, {"unique-roudi-id", required_argument, nullptr, 'u'}, - {"compatibility", required_argument, nullptr, 'c'}, + {"compatibility", required_argument, nullptr, 'x'}, {"kill-delay", required_argument, nullptr, 'k'}, {nullptr, 0, nullptr, 0}}; // colon after shortOption means it requires an argument, two colons mean optional argument - constexpr const char* shortOptions = "hvm:l:u:c:k:"; + constexpr const char* shortOptions = "hvm:l:u:x:k:"; int32_t index; int32_t opt{-1}; while ((opt = getopt_long(argc, argv, shortOptions, longOptions, &index), opt != -1)) @@ -58,7 +58,7 @@ CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgument std::cout << "-l, --log-level Set log level." << std::endl; std::cout << " {off, fatal, error, warning, info, debug, verbose}" << std::endl; - std::cout << "-c, --compatibility Set compatibility check level between runtime and RouDi." + std::cout << "-x, --compatibility Set compatibility check level between runtime and RouDi." << std::endl; std::cout << " off: no check" << std::endl; std::cout << " major: same major version " << std::endl; @@ -201,6 +201,7 @@ CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgument { // CmdLineParser did not understand the parameters, don't run m_run = false; + return cxx::error(CmdLineParserResult::UNKNOWN_OPTION_USED); } }; @@ -209,36 +210,10 @@ CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgument break; } } - return CmdLineArgs_t{ - m_monitoringMode, m_logLevel, m_compatibilityCheckLevel, m_processKillDelay, m_uniqueRouDiId, m_run, ""}; + return cxx::success(CmdLineArgs_t{ + m_monitoringMode, m_logLevel, m_compatibilityCheckLevel, m_processKillDelay, m_uniqueRouDiId, m_run, ""}); } // namespace roudi -bool CmdLineParser::getRun() const noexcept -{ - return m_run; -} -iox::log::LogLevel CmdLineParser::getLogLevel() const noexcept -{ - return m_logLevel; -} -roudi::MonitoringMode CmdLineParser::getMonitoringMode() const noexcept -{ - return m_monitoringMode; -} - -version::CompatibilityCheckLevel CmdLineParser::getCompatibilityCheckLevel() const noexcept -{ - return m_compatibilityCheckLevel; -} - -cxx::optional CmdLineParser::getUniqueRouDiId() const noexcept -{ - return m_uniqueRouDiId; -} -units::Duration CmdLineParser::getProcessKillDelay() const noexcept -{ - return m_processKillDelay; -} } // namespace config } // namespace iox diff --git a/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp b/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp index c0641336ad..f4573c6274 100644 --- a/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp +++ b/iceoryx_posh/source/roudi/roudi_cmd_line_parser_config_file_option.cpp @@ -23,9 +23,8 @@ namespace iox { namespace config { -CmdLineArgs_t CmdLineParserConfigFileOption::parse(int argc, - char* argv[], - const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept +cxx::expected CmdLineParserConfigFileOption::parse( + int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept { constexpr option longOptions[] = {{"help", no_argument, nullptr, 'h'}, {"config-file", required_argument, nullptr, 'c'}, @@ -63,7 +62,11 @@ CmdLineArgs_t CmdLineParserConfigFileOption::parse(int argc, { // we want to parse the help option again, therefore we need to decrement the option index of getopt optind--; - CmdLineParser::parse(argc, argv, CmdLineArgumentParsingMode::ONE); + auto result = CmdLineParser::parse(argc, argv, CmdLineArgumentParsingMode::ONE); + if (result.has_error()) + { + return cxx::error(result.get_error()); + } } }; @@ -72,17 +75,13 @@ CmdLineArgs_t CmdLineParserConfigFileOption::parse(int argc, break; } } - return CmdLineArgs_t{m_monitoringMode, - m_logLevel, - m_compatibilityCheckLevel, - m_processKillDelay, - m_uniqueRouDiId, - m_run, - m_customConfigFilePath}; -} -roudi::ConfigFilePathString_t CmdLineParserConfigFileOption::getConfigFilePath() const -{ - return m_customConfigFilePath; + return cxx::success(CmdLineArgs_t{m_monitoringMode, + m_logLevel, + m_compatibilityCheckLevel, + m_processKillDelay, + m_uniqueRouDiId, + m_run, + m_customConfigFilePath}); } } // namespace config diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp new file mode 100644 index 0000000000..8b7b31a043 --- /dev/null +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp @@ -0,0 +1,389 @@ +// Copyright (c) 2021 by Apex.AI Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "test.hpp" + +#include "iceoryx_posh/roudi/roudi_cmd_line_parser.hpp" + +using namespace ::testing; +using ::testing::Return; + +using namespace iox::roudi; +using namespace iox::config; +using namespace iox::log; +using namespace iox::units; +using namespace iox::version; + +namespace iox +{ +namespace config +{ +bool operator==(const CmdLineArgs_t& lhs, const CmdLineArgs_t& rhs) +{ + return (lhs.monitoringMode == rhs.monitoringMode) && (lhs.logLevel == rhs.logLevel) + && (lhs.compatibilityCheckLevel == rhs.compatibilityCheckLevel) + && (lhs.processKillDelay == rhs.processKillDelay) && (lhs.uniqueRouDiId == rhs.uniqueRouDiId) + && (lhs.run == rhs.run) && (lhs.configFilePath == rhs.configFilePath); +} +} // namespace config +} // namespace iox + + +class CmdLineParser_test : public Test +{ + public: + void SetUp(){}; + void TearDown() + { + // Reset optind to be able to parse again + optind = 0; + }; + + void testLogLevel(uint8_t numberOfArgs, char* args[], LogLevel level) + { + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().logLevel, Eq(level)); + + // Reset optind to be able to parse again + optind = 0; + } + + void testMonitoringMode(uint8_t numberOfArgs, char* args[], MonitoringMode mode) + { + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().monitoringMode, Eq(mode)); + + // Reset optind to be able to parse again + optind = 0; + } + + void testCompatibilityLevel(uint8_t numberOfArgs, char* args[], CompatibilityCheckLevel level) + { + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().compatibilityCheckLevel, Eq(level)); + + // Reset optind to be able to parse again + optind = 0; + } +}; + +TEST_F(CmdLineParser_test, NoOptionLeadsToDefaultValues) +{ + constexpr uint8_t numberOfArgs{1U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + args[0] = &appName[0]; + const CmdLineArgs_t defaultValues; + + CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value(), Eq(defaultValues)); +} + +TEST_F(CmdLineParser_test, WrongOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--ICanHazLulz"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(true)); + EXPECT_THAT(result.get_error(), Eq(CmdLineParserResult::UNKNOWN_OPTION_USED)); +} + +TEST_F(CmdLineParser_test, HelpLongOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--help"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, HelpShortOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-h"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, VersionShortOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-v"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, VersionLongOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--version"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, MonitoringModeOptionsLeadToCorrectMode) +{ + constexpr uint8_t numberOfArgs{3U}; + MonitoringMode modeArray[] = {MonitoringMode::ON, MonitoringMode::OFF}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char optionArray[][20] = {"-m", "--monitoring-mode"}; + char valueArray[][10] = {"on", "off"}; + args[0] = &appName[0]; + + for (auto optionValue : optionArray) + { + args[1] = optionValue; + uint8_t i{0U}; + for (auto expectedValue : modeArray) + { + args[2] = valueArray[i]; + testMonitoringMode(numberOfArgs, args, expectedValue); + i++; + } + } +} + +TEST_F(CmdLineParser_test, WrongMonitoringModeOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-m"; + char wrongValue[] = "DontBlink"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, LogLevelOptionsLeadToCorrectLogLevel) +{ + constexpr uint8_t numberOfArgs{3U}; + LogLevel loglevelArray[] = {LogLevel::kOff, + LogLevel::kFatal, + LogLevel::kError, + LogLevel::kWarn, + LogLevel::kInfo, + LogLevel::kDebug, + LogLevel::kVerbose}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char optionArray[][20] = {"-l", "--log-level"}; + char valueArray[][10] = {"off", "fatal", "error", "warn", "info", "debug", "verbose"}; + args[0] = &appName[0]; + + for (auto optionValue : optionArray) + { + args[1] = optionValue; + uint8_t i{0U}; + for (auto expectedValue : loglevelArray) + { + args[2] = valueArray[i]; + testLogLevel(numberOfArgs, args, expectedValue); + i++; + } + } +} + +TEST_F(CmdLineParser_test, WrongLogLevelOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-l"; + char wrongValue[] = "TimeyWimey"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, KillDelayLongOptionLeadsToCorrectDelay) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--kill-delay"; + char value[] = "73"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &value[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().processKillDelay, Eq(Duration::seconds(73))); +} + +TEST_F(CmdLineParser_test, KillDelayShortOptionLeadsToCorrectDelay) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-k"; + char value[] = "42"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &value[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().processKillDelay, Eq(Duration::seconds(42))); +} + +TEST_F(CmdLineParser_test, KillDelayOptionOutOfBoundsLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--kill-delay"; + char value[] = "4294967296"; // MAX_PROCESS_KILL_DELAY + 1 + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &value[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, CompatibilityLevelOptionsLeadToCorrectCompatibilityLevel) +{ + constexpr uint8_t numberOfArgs{3U}; + CompatibilityCheckLevel loglevelArray[] = {CompatibilityCheckLevel::OFF, + CompatibilityCheckLevel::MAJOR, + CompatibilityCheckLevel::MINOR, + CompatibilityCheckLevel::PATCH, + CompatibilityCheckLevel::COMMIT_ID, + CompatibilityCheckLevel::BUILD_DATE}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char optionArray[][20] = {"-x", "--compatibility"}; + char valueArray[][10] = {"off", "major", "minor", "patch", "commitId", "buildDate"}; + args[0] = &appName[0]; + + for (auto optionValue : optionArray) + { + args[1] = optionValue; + uint8_t i{0U}; + for (auto expectedValue : loglevelArray) + { + args[2] = valueArray[i]; + testCompatibilityLevel(numberOfArgs, args, expectedValue); + i++; + } + } +} + +TEST_F(CmdLineParser_test, WrongCompatibilityLevelOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-x"; + char wrongValue[] = "AmyPond"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} + +TEST_F(CmdLineParser_test, OutOfBoundsUniqueIdOptionLeadsToProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-u"; + char wrongValue[] = "65536"; // MAX_ROUDI_ID + 1 + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} \ No newline at end of file diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp new file mode 100644 index 0000000000..c642d3e7a8 --- /dev/null +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp @@ -0,0 +1,102 @@ +// Copyright (c) 2021 by Apex.AI Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "test.hpp" + +#include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp" +#include "iceoryx_utils/cxx/string.hpp" + +#include + +using namespace ::testing; +using ::testing::Return; + +using namespace iox::cxx; + +class CmdLineParserConfigFileOption_test : public Test +{ + public: + void SetUp(){}; + void TearDown() + { + // Reset optind to be able to parse again + optind = 0; + }; +}; + +TEST_F(CmdLineParserConfigFileOption_test, NoConfigPathOptionLeadsToEmptyPath) +{ + constexpr uint8_t numberOfArgs{1U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + args[0] = &appName[0]; + + iox::config::CmdLineParserConfigFileOption sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().configFilePath.c_str(), StrEq("")); +} + +TEST_F(CmdLineParserConfigFileOption_test, ConfigPathShortOptionIsCorrectlyRead) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-c"; + char path[] = "/foo/bar.toml"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &path[0]; + + iox::config::CmdLineParserConfigFileOption sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().configFilePath.c_str(), StrEq(path)); +} + +TEST_F(CmdLineParserConfigFileOption_test, ConfigPathLongOptionIsCorrectlyRead) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--config-file"; + char path[] = "/foo/bar/baz.toml"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &path[0]; + + iox::config::CmdLineParserConfigFileOption sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().configFilePath.c_str(), StrEq(path)); +} + +TEST_F(CmdLineParserConfigFileOption_test, HelpLongOptionLeadsProgrammNotRunning) +{ + constexpr uint8_t numberOfArgs{2U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--help"; + args[0] = &appName[0]; + args[1] = &option[0]; + + iox::config::CmdLineParserConfigFileOption sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().run, Eq(false)); +} \ No newline at end of file From 6499b52f012565ab05e071608a4c65cfd0addfbc Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 3 Feb 2021 12:07:48 +0100 Subject: [PATCH 08/12] iox-#404 Fix the fix f116f71 Signed-off-by: Simon Hoinkis --- iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp | 2 +- .../include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp index 0bc31d85a4..874d037a3a 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2021 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved. +// Copyright (c) 2021 by Apex.AI Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index 01b4bc03e1..c0b91601bf 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 by Apex.AI Inc. All rights reserved. +// Copyright (c) 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From bd873da118269ef08a75ab7b282d4ce3932986a2 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 3 Feb 2021 12:26:55 +0100 Subject: [PATCH 09/12] iox-#404 Fix doxygen and add two more CmdLineParser test cases Signed-off-by: Simon Hoinkis --- .../roudi/roudi_cmd_line_parser.hpp | 3 +- ...udi_cmd_line_parser_config_file_option.hpp | 3 +- .../test_roudi_cmd_line_parser.cpp | 42 ++++++++++++++++++- ...udi_cmd_line_parser_config_file_option.cpp | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index c0b91601bf..3aaef8f04e 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -52,7 +52,8 @@ class CmdLineParser /// @param[in] argc forwarding of command line arguments /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options - /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct + /// @param[out] Result wrapped in an cxx::expected, either the parsed arguments as CmdLineArgs_t struct or + /// CmdLineParserResult virtual cxx::expected parse(int argc, char* argv[], diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp index d199861029..bb6da90426 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp @@ -34,7 +34,8 @@ class CmdLineParserConfigFileOption : public CmdLineParser /// @param[in] argc forwarding of command line arguments /// @param[in] argv forwarding of command line arguments /// @param[in] cmdLineParsingMode selects to parse a single option or all options - /// @param[out] Result of the parsed arguments as CmdLineArgs_t struct + /// @param[out] Result wrapped in an cxx::expected, either the parsed arguments as CmdLineArgs_t struct or + /// CmdLineParserResult cxx::expected parse(int argc, char* argv[], diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp index 8b7b31a043..f612c35fc6 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp @@ -102,7 +102,7 @@ TEST_F(CmdLineParser_test, NoOptionLeadsToDefaultValues) EXPECT_THAT(result.value(), Eq(defaultValues)); } -TEST_F(CmdLineParser_test, WrongOptionLeadsToProgrammNotRunning) +TEST_F(CmdLineParser_test, WrongOptionLeadsUnkownOptionResult) { constexpr uint8_t numberOfArgs{2U}; char* args[numberOfArgs]; @@ -370,6 +370,44 @@ TEST_F(CmdLineParser_test, WrongCompatibilityLevelOptionLeadsToProgrammNotRunnin EXPECT_THAT(result.value().run, Eq(false)); } +TEST_F(CmdLineParser_test, UniqueIdLongOptionLeadsToCorrectUniqueId) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "--unique-roudi-id"; + char wrongValue[] = "4242"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().uniqueRouDiId.has_value(), Eq(true)); + EXPECT_THAT(result.value().uniqueRouDiId.value(), Eq(4242)); +} + +TEST_F(CmdLineParser_test, UniqueIdShortOptionLeadsToCorrectUniqueId) +{ + constexpr uint8_t numberOfArgs{3U}; + char* args[numberOfArgs]; + char appName[] = "./foo"; + char option[] = "-u"; + char wrongValue[] = "4242"; + args[0] = &appName[0]; + args[1] = &option[0]; + args[2] = &wrongValue[0]; + + iox::config::CmdLineParser sut; + auto result = sut.parse(numberOfArgs, args); + + EXPECT_THAT(result.has_error(), Eq(false)); + EXPECT_THAT(result.value().uniqueRouDiId.has_value(), Eq(true)); + EXPECT_THAT(result.value().uniqueRouDiId.value(), Eq(4242)); +} + TEST_F(CmdLineParser_test, OutOfBoundsUniqueIdOptionLeadsToProgrammNotRunning) { constexpr uint8_t numberOfArgs{3U}; @@ -386,4 +424,4 @@ TEST_F(CmdLineParser_test, OutOfBoundsUniqueIdOptionLeadsToProgrammNotRunning) EXPECT_THAT(result.has_error(), Eq(false)); EXPECT_THAT(result.value().run, Eq(false)); -} \ No newline at end of file +} diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp index c642d3e7a8..7710df003a 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp @@ -99,4 +99,4 @@ TEST_F(CmdLineParserConfigFileOption_test, HelpLongOptionLeadsProgrammNotRunning EXPECT_THAT(result.has_error(), Eq(false)); EXPECT_THAT(result.value().run, Eq(false)); -} \ No newline at end of file +} From e3c26ecbc9b63524e15d4a594f6804f3b04d3822 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 3 Feb 2021 12:46:58 +0100 Subject: [PATCH 10/12] iox-#404 Disable CmdLineParser* tests on Windows Signed-off-by: Simon Hoinkis --- iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp | 2 ++ .../test_roudi_cmd_line_parser_config_file_option.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp index f612c35fc6..bfc19f6589 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if !defined(_WIN32) #include "test.hpp" #include "iceoryx_posh/roudi/roudi_cmd_line_parser.hpp" @@ -425,3 +426,4 @@ TEST_F(CmdLineParser_test, OutOfBoundsUniqueIdOptionLeadsToProgrammNotRunning) EXPECT_THAT(result.has_error(), Eq(false)); EXPECT_THAT(result.value().run, Eq(false)); } +#endif diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp index 7710df003a..9238bb03d0 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser_config_file_option.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if !defined(_WIN32) #include "test.hpp" #include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp" @@ -100,3 +101,4 @@ TEST_F(CmdLineParserConfigFileOption_test, HelpLongOptionLeadsProgrammNotRunning EXPECT_THAT(result.has_error(), Eq(false)); EXPECT_THAT(result.value().run, Eq(false)); } +#endif From a0c9f40d1b03b2f1d8e1ceee51c5022167f5c58c Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Wed, 3 Feb 2021 15:29:51 +0100 Subject: [PATCH 11/12] iox-#404 Address review findings from Mathias Kraus Signed-off-by: Simon Hoinkis --- .../iceperf/roudi_main_static_config.cpp | 10 ++++--- .../roudi/roudi_cmd_line_parser.hpp | 2 +- .../source/roudi/application/roudi_main.cpp | 29 ++++++++++--------- .../test_roudi_cmd_line_parser.cpp | 8 ++--- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/iceoryx_examples/iceperf/roudi_main_static_config.cpp b/iceoryx_examples/iceperf/roudi_main_static_config.cpp index 8df37c5af4..dc9b056310 100644 --- a/iceoryx_examples/iceperf/roudi_main_static_config.cpp +++ b/iceoryx_examples/iceperf/roudi_main_static_config.cpp @@ -25,13 +25,15 @@ int main(int argc, char* argv[]) static constexpr uint32_t ONE_MEGABYTE = 1024U * 1024; iox::config::CmdLineParserConfigFileOption cmdLineParser; - auto cmdLineArgs = cmdLineParser.parse(argc, argv).or_else([](iox::config::CmdLineParserResult& error) { - if (error == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) + auto cmdLineArgs = cmdLineParser.parse(argc, argv); + if (cmdLineArgs.has_error()) + { + if (cmdLineArgs.get_error() == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) { iox::LogFatal() << "Unable to parse command line arguments!"; - std::terminate(); + return EXIT_FAILURE; } - }); + } iox::RouDiConfig_t roudiConfig; // roudiConfig.setDefaults(); can be used if you want to use the default config only. diff --git a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp index 3aaef8f04e..d10087acb5 100644 --- a/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp +++ b/iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp @@ -29,7 +29,7 @@ namespace config enum class CmdLineParserResult { UNKNOWN_OPTION_USED, - USAGE_OUTPUT_REQUESTED /// @todo use this instead of CmdLineArgs_t.run after modularisation of RouDi + INFO_OUTPUT_ONLY /// @todo use this instead of CmdLineArgs_t.run after modularisation of RouDi }; class CmdLineParser diff --git a/iceoryx_posh/source/roudi/application/roudi_main.cpp b/iceoryx_posh/source/roudi/application/roudi_main.cpp index 7ca68ea1f4..3d56a23d5a 100644 --- a/iceoryx_posh/source/roudi/application/roudi_main.cpp +++ b/iceoryx_posh/source/roudi/application/roudi_main.cpp @@ -25,27 +25,28 @@ int main(int argc, char* argv[]) using iox::roudi::IceOryxRouDiApp; iox::config::CmdLineParserConfigFileOption cmdLineParser; - auto cmdLineArgs = cmdLineParser.parse(argc, argv).or_else([](iox::config::CmdLineParserResult& error) { - if (error == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) + auto cmdLineArgs = cmdLineParser.parse(argc, argv); + if (cmdLineArgs.has_error()) + { + if (cmdLineArgs.get_error() == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) { iox::LogFatal() << "Unable to parse command line arguments!"; - std::terminate(); + return EXIT_FAILURE; } - }); + } iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs.value()); - iox::RouDiConfig_t roudiConfig = - configFileProvider.parse() - .or_else([](iox::roudi::RouDiConfigFileParseError& parseResult) { - iox::LogFatal() << "Couldn't parse config file. Error: " - << iox::cxx::convertEnumToString(iox::roudi::ROUDI_CONFIG_FILE_PARSE_ERROR_STRINGS, - parseResult); - std::terminate(); - }) - .value(); + auto roudiConfig = configFileProvider.parse(); + if (roudiConfig.has_error()) + { + iox::LogFatal() << "Couldn't parse config file. Error: " + << iox::cxx::convertEnumToString(iox::roudi::ROUDI_CONFIG_FILE_PARSE_ERROR_STRINGS, + roudiConfig.get_error()); + return EXIT_FAILURE; + } - IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig); + IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig.value()); return roudi.run(); } diff --git a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp index bfc19f6589..2c629a3d41 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_cmd_line_parser.cpp @@ -377,10 +377,10 @@ TEST_F(CmdLineParser_test, UniqueIdLongOptionLeadsToCorrectUniqueId) char* args[numberOfArgs]; char appName[] = "./foo"; char option[] = "--unique-roudi-id"; - char wrongValue[] = "4242"; + char value[] = "4242"; args[0] = &appName[0]; args[1] = &option[0]; - args[2] = &wrongValue[0]; + args[2] = &value[0]; iox::config::CmdLineParser sut; auto result = sut.parse(numberOfArgs, args); @@ -396,10 +396,10 @@ TEST_F(CmdLineParser_test, UniqueIdShortOptionLeadsToCorrectUniqueId) char* args[numberOfArgs]; char appName[] = "./foo"; char option[] = "-u"; - char wrongValue[] = "4242"; + char value[] = "4242"; args[0] = &appName[0]; args[1] = &option[0]; - args[2] = &wrongValue[0]; + args[2] = &value[0]; iox::config::CmdLineParser sut; auto result = sut.parse(numberOfArgs, args); From deff5a6774bd2168a6de1288679db2329ca7a080 Mon Sep 17 00:00:00 2001 From: Simon Hoinkis Date: Thu, 4 Feb 2021 10:01:12 +0100 Subject: [PATCH 12/12] iox-#404 Address review findings from Mathias Kraus Signed-off-by: Simon Hoinkis --- iceoryx_examples/iceperf/roudi_main_static_config.cpp | 9 +++------ iceoryx_posh/source/roudi/application/roudi_main.cpp | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/iceoryx_examples/iceperf/roudi_main_static_config.cpp b/iceoryx_examples/iceperf/roudi_main_static_config.cpp index dc9b056310..a8a4e6820f 100644 --- a/iceoryx_examples/iceperf/roudi_main_static_config.cpp +++ b/iceoryx_examples/iceperf/roudi_main_static_config.cpp @@ -26,13 +26,10 @@ int main(int argc, char* argv[]) iox::config::CmdLineParserConfigFileOption cmdLineParser; auto cmdLineArgs = cmdLineParser.parse(argc, argv); - if (cmdLineArgs.has_error()) + if (cmdLineArgs.has_error() && (cmdLineArgs.get_error() != iox::config::CmdLineParserResult::INFO_OUTPUT_ONLY)) { - if (cmdLineArgs.get_error() == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) - { - iox::LogFatal() << "Unable to parse command line arguments!"; - return EXIT_FAILURE; - } + iox::LogFatal() << "Unable to parse command line arguments!"; + return EXIT_FAILURE; } iox::RouDiConfig_t roudiConfig; diff --git a/iceoryx_posh/source/roudi/application/roudi_main.cpp b/iceoryx_posh/source/roudi/application/roudi_main.cpp index 3d56a23d5a..67cdf8d8f4 100644 --- a/iceoryx_posh/source/roudi/application/roudi_main.cpp +++ b/iceoryx_posh/source/roudi/application/roudi_main.cpp @@ -26,13 +26,10 @@ int main(int argc, char* argv[]) iox::config::CmdLineParserConfigFileOption cmdLineParser; auto cmdLineArgs = cmdLineParser.parse(argc, argv); - if (cmdLineArgs.has_error()) + if (cmdLineArgs.has_error() && (cmdLineArgs.get_error() != iox::config::CmdLineParserResult::INFO_OUTPUT_ONLY)) { - if (cmdLineArgs.get_error() == iox::config::CmdLineParserResult::UNKNOWN_OPTION_USED) - { - iox::LogFatal() << "Unable to parse command line arguments!"; - return EXIT_FAILURE; - } + iox::LogFatal() << "Unable to parse command line arguments!"; + return EXIT_FAILURE; } iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs.value());