Skip to content

Commit

Permalink
feat: Validate execution options
Browse files Browse the repository at this point in the history
Validation of execution options has been added.
  • Loading branch information
sdvendramini committed Nov 22, 2024
1 parent 5139918 commit 694389a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/agent/include/cmd_ln_parser.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <stdexcept>
#include <string>
#include <vector>

Expand All @@ -13,10 +14,16 @@ class CommandlineParser
/// @brief Constructor
/// @param argc The number of command line arguments
/// @param argv The command line arguments
CommandlineParser(int argc, char** argv)
/// @param options The command line options available
CommandlineParser(int argc, char** argv, const std::vector<std::string>& options)
{
for (int i = 1; i < argc; ++i)
{
auto isValid = std::find(options.begin(), options.end(), argv[i]);
if (isValid == options.end() && argv[i][0] == '-')
{
throw std::invalid_argument("Invalid option: " + std::string(argv[i]));
}
m_tokens.push_back(std::string(argv[i]));
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/agent/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
int main(int argc, char* argv[])
{
Logger logger;
CommandlineParser cmdParser(argc, argv);

std::string configFile;

try
{
CommandlineParser cmdParser(argc, argv, validOptions);

std::string configFile;

if (cmdParser.OptionExists(OPT_CONFIG_FILE))
{
configFile = cmdParser.GetOptionValue(OPT_CONFIG_FILE);
Expand Down
15 changes: 15 additions & 0 deletions src/agent/src/process_options.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <vector>

/// Command-line options
static const auto OPT_STATUS {"--status"};
Expand All @@ -17,6 +18,20 @@ static const auto OPT_KEY {"--key"};
static const auto OPT_NAME {"--name"};
static const auto OPT_HELP {"--help"};

const std::vector<std::string> validOptions = {{OPT_STATUS},
{OPT_STOP},
{OPT_RESTART},
{OPT_CONFIG_FILE},
{OPT_REGISTER_AGENT},
{OPT_INSTALL_SERVICE},
{OPT_REMOVE_SERVICE},
{OPT_RUN_SERVICE},
{OPT_USER},
{OPT_PASSWORD},
{OPT_KEY},
{OPT_NAME},
{OPT_HELP}};

/// @brief Registers the agent with the given parameters.
/// @param user The user to use for authentication with Server Management API.
/// @param password The password to use for authentication with Server Management API.
Expand Down
5 changes: 3 additions & 2 deletions src/agent/tests/cmd_ln_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class CommandlineParserTest : public ::testing::Test
std::string program = "program";
std::string option = "--option";
std::string value = "value";
std::vector<std::string> validOptions = {"--option"};
std::array<char*, 3> args = {program.data(), option.data(), value.data()};
CommandlineParser parser {static_cast<int>(args.size()), args.data()};
CommandlineParser parser {static_cast<int>(args.size()), args.data(), validOptions};
};

TEST_F(CommandlineParserTest, GetOptionValue_OptionExists)
Expand All @@ -38,7 +39,7 @@ TEST_F(CommandlineParserTest, OptionExists_OptionDoesNotExist)
TEST_F(CommandlineParserTest, GetOptionValue_NoValueForOption)
{
std::array<char*, 2> noValueArgs = {program.data(), option.data()};
CommandlineParser noValueParser {static_cast<int>(noValueArgs.size()), noValueArgs.data()};
CommandlineParser noValueParser {static_cast<int>(noValueArgs.size()), noValueArgs.data(), validOptions};

EXPECT_EQ(noValueParser.GetOptionValue(option), "");
}
Expand Down

0 comments on commit 694389a

Please sign in to comment.