Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore start_index option #19

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
7 changes: 4 additions & 3 deletions include/popl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ class OptionParser
/// Parse the command line into the added Options
/// @param argc command line argument count
/// @param argv command line arguments
void parse(int argc, const char* const argv[]);
/// @param start_index index of starting argument
void parse(int argc, const char * const argv[], int start_index = 1);

/// Delete all parsed options
void reset();
Expand Down Expand Up @@ -993,9 +994,9 @@ inline void OptionParser::parse(const std::string& ini_filename)
}
}

inline void OptionParser::parse(int argc, const char* const argv[])
inline void OptionParser::parse(int argc, const char* const argv[], int start_index)
{
for (int n = 1; n < argc; ++n)
for (int n = start_index; n < argc; ++n)
{
const std::string arg(argv[n]);
if (arg == "--")
Expand Down
14 changes: 14 additions & 0 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ TEST_CASE("config file")
}
}

TEST_CASE("start index")
{
OptionParser op("Allowed options");
auto help_option = op.add<Switch>("h", "help", "produce help message");
auto int_option = op.add<Value<int>>("i", "int", "test for int value", 42);
std::vector<const char*> args = {"popl", "-h", "--", "-i", "43"};

op.parse(args.size(), args.data(), 3);
REQUIRE(help_option->count() == 0);
REQUIRE(int_option->is_set());
REQUIRE(int_option->count() == 1);
REQUIRE(int_option->value() == 43);
}