Let's take a look at the test case ArgParserTestSuite.MultiValueTest. This test assumes that the can parse multiple values for a single argument (in this scenario, ). However, based on the provided argument string , it looks like only one value is being passed to .
User
Assistant
bool ArgParser::Parse(std::vector<std::string> args) {
if (all_.empty() && args.size() == 1) {
return true;
}
int count = 0;
for (int i = 0; i < args.size(); ++i) {
for (int j = 0; j < all_.size(); ++j) {
TypeFlag type = all_[j]->GetTypeFlag();
switch (type) {
case TypeFlag::INT: {
Int* arg = static_cast<Int*>(all_[j]);
std::string origin_param = "--" + arg->GetParam() + "=";
std::string origin_short_param = "-";
origin_short_param.push_back(arg->GetShortParam());
origin_short_param += "=";
if (!arg->GetParam().empty() && args[i].substr(0, origin_param.length()) == origin_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
++count;
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
++count;
} else if (all_[j]->GetFlagMultiValue()) {
if (args[i].substr(0, origin_param.length()) == origin_param) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
all_[j]->MultiArgsCount();
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
all_[j]->MultiArgsCount();
}
}
if (all_[j]->GetFlagPositional()) {
int count_len = 0;
for (int k = 0; k < args[i].length(); ++k) {
if (!std::isdigit(args[i][k])){
break;
} else {
++count_len;
}
}
if (count_len == args[i].length()) {
arg->PutValue(args[i]);
arg->MultiArgsCount();
if (arg->GetFlagStoreValues()) {
arg->AddStoreValues(arg->GetValue());
all_[j]->AddAllValues(arg->GetValue());
}
}
}
}
break;
case TypeFlag::STRING: {
String* arg = static_cast<String*>(all_[j]);
std::string origin_param = "--" + arg->GetParam() + "=";
std::string origin_short_param = "-";
origin_short_param.push_back(arg->GetShortParam());
origin_short_param += "=";
if (args.size() == 1 && !arg->GetDefaultValue().empty()) {
arg->PutValue(arg->GetDefaultValue());
++count;
} else if (!arg->GetParam().empty() && args[i].substr(0, origin_param.length()) == origin_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
if (arg->GetFlagStoreValue()) {
arg->AddStoreValue(arg->GetValue());
}
++count;
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
if (arg->GetFlagStoreValue()) {
arg->AddStoreValue(arg->GetValue());
}
++count;
} else if (arg->GetFlagMultiValue()) {
if (args[i].substr(0, origin_param.length()) == origin_param) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
arg->MultiArgsCount();
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
arg->MultiArgsCount();
}
}
}
break;
case TypeFlag::BOOL: {
Bool* arg = static_cast<Bool*>(all_[j]);
std::string origin_flag = "--" + arg->GetFlag();
if (arg->GetIsFlag()) {
if (args[i].substr(0, origin_flag.length()) == origin_flag) {
if (arg->GetFlagStoreFlag()) {
arg->AddStoreFlag(true);
}
++count;
} else if (args[i][0] == '-' && args[i][1] != '-' && args[i].length() > 2) {
for (int z = 1; z < args[i].length(); ++z) {
if (args[i][z] == arg->GetShortFlag()) {
if (arg->GetFlagStoreFlag()) {
arg->AddStoreFlag(true);
}
++count;
}
}
}
}
}
break;
case TypeFlag::UNKNOWN: {
Args* arg = static_cast<Args*>(all_[j]);
std::string origin_help = "--" + all_[j]->GetHelp();
std::string origin_short_help = "-";
origin_short_help.push_back(all_[j]->GetShortHelp());
if (args[i].substr(0, origin_help.length()) == origin_help || args[i].substr(0, origin_short_help.length()) == origin_short_help) {
all_[j]->FlagHelp();
++count;
}
}
break;
}
}
}
for (int i = 0; i < all_.size(); ++i) {
if (all_[i]->GetMultiArgsCount() > 0) {
++count;
}
if (static_cast<Bool*>(all_[i])->GetFlagDefaultFlag()) {
++count;
}
if (all_[i]->GetMultiArgsCount() > 0 && all_[i]->GetMinArgsCount() > all_[i]->GetMultiArgsCount()) {
return false;
}
}
if (count == all_.size()) {
return true;
} else {
for (int i = 0; i < all_.size(); ++i) {
if (all_[i]->GetFlagHelp()) {
return true;
}
}
return false;
}
}
String& ArgParser::AddStringArgument(const std::string param) {
String* str = new String();
str->param_ = param;
all_.push_back(str);
return *str;
}
String& ArgParser::AddStringArgument(const char short_param, const std::string param) {
String* str = new String();
str->short_param_ = short_param;
str->param_ = param;
str->flag_short_param_ = true;
all_.push_back(str);
return *str;
}
String& ArgParser::AddStringArgument(const char short_param, const std::string param, const std::string description) {
String* str = new String();
str->short_param_ = short_param;
str->param_ = param;
str->flag_short_param_ = true;
all_.push_back(str);
str->Description(description);
return *str;
}
std::string ArgParser::GetStringValue(const std::string param) {
for (int i = 0; i < (all_).size(); ++i) {
if (static_cast<String*>(all_[i])->param_ == param) {
return static_cast<String*>(all_[i])->GetValue();
}
}
}
Int& ArgParser::AddIntArgument(const std::string number) {
Int* int_val = new Int();
int_val->param_ = number;
all_.push_back(int_val);
return *int_val;
}
Int& ArgParser::AddIntArgument(const std::string number, const std::string description) {
Int* int_val = new Int();
int_val->param_ = number;
all_.push_back(int_val);
int_val->Description(description);
return *int_val;
}
int ArgParser::GetIntValue(const std::string param) {
for (int i = 0; i < all_.size(); ++i) {
if (static_cast<Int*>(all_[i])->GetParam() == param) {
int value = std::stoi(static_cast<Int*>(all_[i])->GetValue());
return value;
}
}
}
Int& ArgParser::AddIntArgument(const char short_param, const std::string param) {
Int* int_val = new Int();
int_val->short_param_ = short_param;
all_.push_back(int_val);
return *int_val;
}
int ArgParser::GetIntValue(const std::string param, const int ind) {
if (static_cast<Int*>(all_[ind])->GetParam() == param) {
return static_cast<Int*>(all_[ind])->GetIndexedValue(ind);
}
}
Bool& ArgParser::AddFlag(const char short_flag, const std::string flag) {
Bool* new_flag = new Bool();
new_flag->AddFlags(short_flag, flag);
all_.push_back(new_flag);
return *new_flag;
}
Bool& ArgParser::AddFlag(const char short_flag, const std::string flag, const std::string description) {
Bool* new_flag = new Bool();
new_flag->AddFlags(short_flag, flag);
all_.push_back(new_flag);
new_flag->Description(description);
return *new_flag;
}
Bool& ArgParser::AddFlag(const std::string flag, const std::string description) {
Bool* new_flag = new Bool();
new_flag->AddFlag(flag);
all_.push_back(new_flag);
new_flag->Description(description);
return *new_flag;
}
bool ArgParser::GetFlag(const std::string flag) {
for (int i = 0; i < all_.size(); ++i) {
if (static_cast<Bool*>(all_[i])->GetFlag() == flag) {
return true;
}
}
return false;
}
bool ArgParser::Help() {
return true;
}
Args& ArgParser::AddHelp(const char short_help, const std::string help, const std::string description) {
Args* new_help = new Args();
new_help->Help(short_help, help);
new_help->Description(description);
all_.push_back(new_help);
return *new_help;
}
std::string ArgParser::HelpDescription() {
std::cout << "My Parser\n";
std::cout << "Some Description about program\n\n";
std::cout << "-i, --input=<string>, File path for input file [repeated, min args = 1]\n";
std::cout << "-s, --flag1, Use some logic [default = true]\n";
std::cout << "-p, --flag2, Use some logic\n";
std::cout << " --number=<int>, Some Number\n\n";
std::cout << "-h, --help Display this help and exit\n";
}
}
#pragma once
#include
#include
namespace ArgumentParser {
enum class TypeFlag {
INT,
STRING,
BOOL,
UNKNOWN // In case no flag is set or Args is a base type that should not be instantiated
};
}9: Test command: /Users/alishashelby/Desktop/labwork4-alishashelby/build/tests/argparser_tests "--gtest_filter=ArgParserTestSuite.MultiValueTest" "--gtest_also_run_disabled_tests"
9: Working Directory: /Users/alishashelby/Desktop/labwork4-alishashelby/build/tests
9: Test timeout computed to be: 10000000
9: Running main() from /Users/alishashelby/Desktop/labwork4-alishashelby/build/_deps/googletest-src/googletest/src/gtest_main.cc
9: Note: Google Test filter = ArgParserTestSuite.MultiValueTest
9: [==========] Running 1 test from 1 test suite.
9: [----------] Global test environment set-up.
9: [----------] 1 test from ArgParserTestSuite
9: [ RUN ] ArgParserTestSuite.MultiValueTest
9: /Users/alishashelby/Desktop/labwork4-alishashelby/tests/argparser_test.cpp:96: Failure
9: Value of: parser.Parse(SplitString("app --param1=1"))
9: Actual: false
9: Expected: true
9: [ FAILED ] ArgParserTestSuite.MultiValueTest (0 ms)
9: [----------] 1 test from ArgParserTestSuite (0 ms total)
9:
9: [----------] Global test environment tear-down
9: [==========] 1 test from 1 test suite ran. (0 ms total)
9: [ PASSED ] 0 tests.
9: [ FAILED ] 1 test, listed below:
9: [ FAILED ] ArgParserTestSuite.MultiValueTest
9:
9: 1 FAILED TEST fix it
It looks like you are trying to run a unit test for a custom argument parser you've implemented, but you're encountering a test failure. The test ArgParserTestSuite.MultiValueTest is failing due to the parser's Parse function returning false when it is expected to return true.
Below are a few potential issues and tips to troubleshoot and fix the problem:
Inspect Test Input:
Check if SplitString("app --param1=1") is doing what it's supposed to do. You expect it to split the string into a vector of strings, but the implementation of SplitString is not visible in the provided code. Ensure that it correctly parses the input into ["app", "--param1=1"].
Test Single-Value Behavior:
Your test case seems to be for a single value, but your call to AddIntArgument uses MultiValue() which suggests support for multiple integers. The assertion ASSERT_EQ(int_values[1], 2); also seems to expect multiple values, yet only one value --param1=1 is provided in the test.
Verify Multi-Value Handling in the Parse Method:
If MultiValue() option allows for multiple values to be passed, your parsing logic should be designed to handle this. The parser should iterate over each argument and store each value accordingly. But based on the code provided, it's unclear how multiple values should be provided (as separate arguments like --param1=1 --param1=2, or in a single argument like --param1=1,2, etc.).
Default Values and Optional Arguments:
Verify whether your argument parsing logic correctly handles default values and optional arguments.
Check Argument Definition Consistency:
Ensure that the way you define and parse arguments is consistent. For example, you're mixing the declaration and definition of multi-valued arguments and their parsing logic. Align these to ensure consistent behavior.
Analyze the Parser Logic:
Deeply inspect the ArgParser::Parse method. It's a complex function with multiple control-flow paths, which increases the risk of logic errors. Use breakpoints or logging to understand at which specific point the parsing fails.
Memory Management Issue:
The Args, Int, String, and Bool classes must be cleaned up properly to avoid memory leaks, since new instances are created with new but we don't see delete used anywhere for cleanup. Consider using smart pointers (such as std::unique_ptr or std::shared_ptr) for automatic memory management.
The Test Assertion May Be Incorrect:
If the test is designed to assert the parser's behavior with a single value, then the multiple assertions checking for additional values (like ASSERT_EQ(int_values[1], 2);) may be incorrect.
To fix the failed test, you'll need to carefully review the logic of the parsing and how multi-value arguments are expected to work. Also, adjust your test case to correctly represent the intended behavior of multi-value argument parsing, or if you're testing single-value functionality, remove the non-applicable assertions.
Remember, each piece of functionality should be distinctly tested. Start with a test case for single values, then proceed to write separate tests for default values, multi-values, flags, and so on. This way, you can ensure each feature of your parser is working as expected.
/*
Функция принимает в качество аргумента строку, разделяет ее по "пробелу"
и возвращает вектор полученных слов
*/
std::vectorstd::string SplitString(const std::string& str) {
std::istringstream iss(str);
TEST(ArgParserTestSuite, HelpStringTest) {
ArgParser parser("My Parser");
parser.AddHelp('h', "help", "Some Description about program");
parser.AddStringArgument('i', "input", "File path for input file").MultiValue(1);
parser.AddFlag('s', "flag1", "Use some logic").Default(true);
parser.AddFlag('p', "flag2", "Use some logic");
parser.AddIntArgument("numer", "Some Number");
code
ASSERT_TRUE(parser.Parse(SplitString("app --help")));
// Проверка закоментирована намеренно. Ождиается, что результат вызова функции будет приблизительно такой же,
// но не с точностью до символа
/*ASSERT_EQ(
parser.HelpDescription(),
"My Parser\n"
"Some Description about program\n"
"\n"
"-i, --input=<string>, File path for input file [repeated, min args = 1]\n"
"-s, --flag1, Use some logic [default = true]\n"
"-p, --flag2, Use some logic\n"
" --number=<int>, Some Number\n"
"\n"
"-h, --help Display this help and exit\n"
);*/
}
all tests are working except of MultyValue test, find the mistake in me code
The first assertion checks for the first value of param1, which seems correct. However, the second and third assertions are expecting param1 to have more values (2 and 3, respectively), but these values are not present in the input SplitString("app --param1=1").
Here's what needs to be done to fix the MultiValueTest:
Update the Test Input:
Modify the input to include multiple --param1 values, for example:
cpp
"app --param1=1 --param1=2 --param1=3"
Adjust Indexing in Assertions:
Since indexing in vectors is zero-based, but your GetIntValue call uses 0 as an index, the subsequent checks for int_values should start from index 0 as well:
cpp
ASSERT_EQ(int_values[0], 1); // First value
ASSERT_EQ(int_values[1], 2); // Second value (not [1])
ASSERT_EQ(int_values[2], 3); // Third value (not [2])
Check Implementation for MultiValue Support:
Review the implementation of the ArgParser::Parse function to ensure it supports multiple values for a single argument. The parsing logic should be able to append each additional value to the corresponding std::vector<int> for the argument.
Once you make these adjustments, your test should look more like this:
Make sure that the parsing logic is capable of separating each --param when it encounters them in the input arguments and that it handles scenarios with multiple repetitions of the same named parameter, adding each found value to the resulting vector.
These changes should help you pass the failing test case.
bool ArgParser::Parse(std::vector<std::string> args) {
if (all_.empty() && args.size() == 1) {
return true;
}
int count = 0;
for (int i = 0; i < args.size(); ++i) {
for (int j = 0; j < all_.size(); ++j) {
TypeFlag type = all_[j]->GetTypeFlag();
switch (type) {
case TypeFlag::INT: {
Int* arg = static_cast<Int*>(all_[j]);
std::string origin_param = "--" + arg->GetParam() + "=";
std::string origin_short_param = "-";
origin_short_param.push_back(arg->GetShortParam());
origin_short_param += "=";
if (!arg->GetParam().empty() && args[i].substr(0, origin_param.length()) == origin_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
++count;
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
++count;
} else if (all_[j]->GetFlagMultiValue()) {
if (args[i].substr(0, origin_param.length()) == origin_param) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
all_[j]->MultiArgsCount();
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
all_[j]->MultiArgsCount();
}
}
if (all_[j]->GetFlagPositional()) {
int count_len = 0;
for (int k = 0; k < args[i].length(); ++k) {
if (!std::isdigit(args[i][k])){
break;
} else {
++count_len;
}
}
if (count_len == args[i].length()) {
arg->PutValue(args[i]);
arg->MultiArgsCount();
if (arg->GetFlagStoreValues()) {
arg->AddStoreValues(arg->GetValue());
all_[j]->AddAllValues(arg->GetValue());
}
}
}
}
break;
case TypeFlag::STRING: {
String* arg = static_cast<String*>(all_[j]);
std::string origin_param = "--" + arg->GetParam() + "=";
std::string origin_short_param = "-";
origin_short_param.push_back(arg->GetShortParam());
origin_short_param += "=";
if (args.size() == 1 && !arg->GetDefaultValue().empty()) {
arg->PutValue(arg->GetDefaultValue());
++count;
} else if (!arg->GetParam().empty() && args[i].substr(0, origin_param.length()) == origin_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
if (arg->GetFlagStoreValue()) {
arg->AddStoreValue(arg->GetValue());
}
++count;
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param && !all_[j]->GetFlagMultiValue()) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
if (arg->GetFlagStoreValue()) {
arg->AddStoreValue(arg->GetValue());
}
++count;
} else if (arg->GetFlagMultiValue()) {
if (args[i].substr(0, origin_param.length()) == origin_param) {
arg->PutValue(args[i].substr(origin_param.length(), args[i].length()));
arg->MultiArgsCount();
} else if (arg->GetFlagShortParam() && args[i].substr(0, origin_short_param.length()) == origin_short_param) {
arg->PutValue(args[i].substr(origin_short_param.length(), args[i].length()));
arg->MultiArgsCount();
}
}
}
break;
case TypeFlag::BOOL: {
Bool* arg = static_cast<Bool*>(all_[j]);
std::string origin_flag = "--" + arg->GetFlag();
if (arg->GetIsFlag()) {
if (args[i].substr(0, origin_flag.length()) == origin_flag) {
if (arg->GetFlagStoreFlag()) {
arg->AddStoreFlag(true);
}
++count;
} else if (args[i][0] == '-' && args[i][1] != '-' && args[i].length() > 2) {
for (int z = 1; z < args[i].length(); ++z) {
if (args[i][z] == arg->GetShortFlag()) {
if (arg->GetFlagStoreFlag()) {
arg->AddStoreFlag(true);
}
++count;
}
}
}
}
}
break;
case TypeFlag::UNKNOWN: {
Args* arg = static_cast<Args*>(all_[j]);
std::string origin_help = "--" + all_[j]->GetHelp();
std::string origin_short_help = "-";
origin_short_help.push_back(all_[j]->GetShortHelp());
if (args[i].substr(0, origin_help.length()) == origin_help || args[i].substr(0, origin_short_help.length()) == origin_short_help) {
all_[j]->FlagHelp();
++count;
}
}
break;
}
}
}
for (int i = 0; i < all_.size(); ++i) {
if (all_[i]->GetMultiArgsCount() > 0) {
++count;
}
if (static_cast<Bool*>(all_[i])->GetFlagDefaultFlag()) {
++count;
}
if (all_[i]->GetMultiArgsCount() > 0 && all_[i]->GetMinArgsCount() > all_[i]->GetMultiArgsCount()) {
return false;
}
}
if (count == all_.size()) {
return true;
} else {
for (int i = 0; i < all_.size(); ++i) {
if (all_[i]->GetFlagHelp()) {
return true;
}
}
return false;
}
}
String& ArgParser::AddStringArgument(const std::string param) {
String* str = new String();
str->param_ = param;
all_.push_back(str);
return *str;
}
String& ArgParser::AddStringArgument(const char short_param, const std::string param) {
String* str = new String();
str->short_param_ = short_param;
str->param_ = param;
str->flag_short_param_ = true;
all_.push_back(str);
return *str;
}
String& ArgParser::AddStringArgument(const char short_param, const std::string param, const std::string description) {
String* str = new String();
str->short_param_ = short_param;
str->param_ = param;
str->flag_short_param_ = true;
all_.push_back(str);
str->Description(description);
return *str;
}
std::string ArgParser::GetStringValue(const std::string param) {
for (int i = 0; i < (all_).size(); ++i) {
if (static_cast<String*>(all_[i])->param_ == param) {
return static_cast<String*>(all_[i])->GetValue();
}
}
}
Int& ArgParser::AddIntArgument(const std::string number) {
Int* int_val = new Int();
int_val->param_ = number;
all_.push_back(int_val);
return *int_val;
}
Int& ArgParser::AddIntArgument(const std::string number, const std::string description) {
Int* int_val = new Int();
int_val->param_ = number;
all_.push_back(int_val);
int_val->Description(description);
return *int_val;
}
int ArgParser::GetIntValue(const std::string param) {
for (int i = 0; i < all_.size(); ++i) {
if (static_cast<Int*>(all_[i])->GetParam() == param) {
int value = std::stoi(static_cast<Int*>(all_[i])->GetValue());
return value;
}
}
}
Int& ArgParser::AddIntArgument(const char short_param, const std::string param) {
Int* int_val = new Int();
int_val->short_param_ = short_param;
all_.push_back(int_val);
return *int_val;
}
int ArgParser::GetIntValue(const std::string param, const int ind) {
if (static_cast<Int*>(all_[ind])->GetParam() == param) {
return static_cast<Int*>(all_[ind])->GetIndexedValue(ind);
}
}
Bool& ArgParser::AddFlag(const char short_flag, const std::string flag) {
Bool* new_flag = new Bool();
new_flag->AddFlags(short_flag, flag);
all_.push_back(new_flag);
return *new_flag;
}
Bool& ArgParser::AddFlag(const char short_flag, const std::string flag, const std::string description) {
Bool* new_flag = new Bool();
new_flag->AddFlags(short_flag, flag);
all_.push_back(new_flag);
new_flag->Description(description);
return *new_flag;
}
Bool& ArgParser::AddFlag(const std::string flag, const std::string description) {
Bool* new_flag = new Bool();
new_flag->AddFlag(flag);
all_.push_back(new_flag);
new_flag->Description(description);
return *new_flag;
}
bool ArgParser::GetFlag(const std::string flag) {
for (int i = 0; i < all_.size(); ++i) {
if (static_cast<Bool*>(all_[i])->GetFlag() == flag) {
return true;
}
}
return false;
}
bool ArgParser::Help() {
return true;
}
Args& ArgParser::AddHelp(const char short_help, const std::string help, const std::string description) {
Args* new_help = new Args();
new_help->Help(short_help, help);
new_help->Description(description);
all_.push_back(new_help);
return *new_help;
}
std::string ArgParser::HelpDescription() {
std::cout << "My Parser\n";
std::cout << "Some Description about program\n\n";
std::cout << "-i, --input=<string>, File path for input file [repeated, min args = 1]\n";
std::cout << "-s, --flag1, Use some logic [default = true]\n";
std::cout << "-p, --flag2, Use some logic\n";
std::cout << " --number=<int>, Some Number\n\n";
std::cout << "-h, --help Display this help and exit\n";
}
}
Looking closely at the ArgParser::Parse function, there are a few points to analyze to fix the issue with MultiValueTest not passing:
Multi-value logic: The ArgParser::Parse currently increments the count for an argument only if it identifies a command-line argument that matches an expected parameter. This will not work correctly for MultiValue arguments because count will be incremented more than once for the same argument. You should instead use a different approach for counting unique arguments and handling multiple values.
Store values for multi-value arguments: When MultiValue is set for an argument, you must store every occurrence of the argument's value. It means that if your command line has --param1=1 --param1=2 --param1=3, you should extract and store each value (1, 2, and 3) separately in related Int objects.
Argument counting logic: The final check in the Parse function compares the count of parsed arguments against the size of all_, which assumes that each item in args map directly to an item in all_. This could fail if args contains multiple values for the same argument (as is the case with MultiValue). You should use a unique count of command-line arguments that have been set, not simply the number parsed.
Here's how you might adjust the multi-value handling in the parsing loop:
cpp
case TypeFlag::INT: {
Int* arg = static_cast<Int*>(all_[j]);
std::string origin_param = "--" + arg->GetParam() + "=";
std::string origin_short_param = "-";
origin_short_param.push_back(arg->GetShortParam());
origin_short_param += "=";
// Handle multi-value arguments
if (arg->GetFlagMultiValue()) {
size_t origin_length = origin_param.length();
if (args[i].substr(0, origin_length) == origin_param) {
// Extract and store the value
std::string value_str = args[i].substr(origin_length);
arg->PutValue(value_str);
arg->AddStoreValues(value_str); // Make sure this function appends to a vector of values
// Do not increment 'count' here, as it's the same argument
// Instead, make sure to have unique count logic based on the actual argument 'param_'
// For each unique 'param_', increment your unique argument count
}
} else {
// Existing non multi-value logic
}
}
break;
After collecting all values, you'll need to update your counting logic at the end of the Parse function to handle the fact that multiple values can be associated with the same argument.
Ensure your AddStoreValues method in the Int class actually appends to a vector of integer values instead of overwriting a single value.
Lastly, don't forget to handle the case where the minimum number of arguments isn't met (for multi-value arguments) appropriately so that Parse returns false.
Once you adjust the multi-value logic in your ArgParser::Parse function based on the above points, your MultiValueTest should work provided the test is also adjusted to pass an argument string with multiple param1 values (as previously discussed).