Skip to content

Commit

Permalink
libcore: Added method for processing all command line option parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 6, 2017
1 parent 13d94f1 commit 6bfd747
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doomsday/sdk/libcore/include/de/core/commandline.h
Expand Up @@ -127,6 +127,19 @@ class DENG2_PUBLIC CommandLine
*/
ArgWithParams check(String const &arg, dint count = 0) const;

/**
* Calls a callback function for each of the parameters given to an argumnet.
* If there are multiple @a arg options found, all of the parameters given
* to each option get called in order.
*
* @param arg Argument to look for. Don't use aliases here.
* @param paramHandler Callback for the parameters.
*
* @return Number of parameters handled.
*/
int forAllParameters(String const &arg,
std::function<void (duint, String const &)> paramHandler) const;

/**
* Gets the parameter for an argument.
*
Expand Down
29 changes: 29 additions & 0 deletions doomsday/sdk/libcore/src/core/commandline.cpp
Expand Up @@ -210,6 +210,35 @@ CommandLine::ArgWithParams CommandLine::check(String const &arg, dint numParams)
return found;
}

int CommandLine::forAllParameters(String const &arg,
std::function<void (duint, String const &)> paramHandler) const
{
int total = 0;
bool inside = false;

for (Impl::Arguments::const_iterator i = d->arguments.begin();
i != d->arguments.end(); ++i)
{
if (matches(arg, *i))
{
inside = true;
}
else if (inside)
{
if (isOption(*i))
{
inside = false;
}
else
{
paramHandler(i - d->arguments.begin(), *i);
++total;
}
}
}
return total;
}

bool CommandLine::getParameter(String const &arg, String &param) const
{
dint pos = check(arg, 1);
Expand Down

0 comments on commit 6bfd747

Please sign in to comment.