Skip to content
Snippets Groups Projects
Commit 72ca579a authored by Jö Fahlke's avatar Jö Fahlke
Browse files

[FS1523] Merge branch 'p/joe/fs1523-parametertree-accept-negative-cmdline-args' into master

No comment/response from the other interested parties, so I assume this is OK.
parents 1a87f667 580443e3
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#include <map> #include <map>
#include <algorithm> #include <algorithm>
#include <dune/common/exceptions.hh>
std::string Dune::ParameterTreeParser::ltrim(const std::string& s) std::string Dune::ParameterTreeParser::ltrim(const std::string& s)
{ {
std::size_t firstNonWS = s.find_first_not_of(" \t\n\r"); std::size_t firstNonWS = s.find_first_not_of(" \t\n\r");
...@@ -137,27 +139,17 @@ void Dune::ParameterTreeParser::readINITree(std::istream& in, ...@@ -137,27 +139,17 @@ void Dune::ParameterTreeParser::readINITree(std::istream& in,
void Dune::ParameterTreeParser::readOptions(int argc, char* argv [], void Dune::ParameterTreeParser::readOptions(int argc, char* argv [],
ParameterTree& pt) ParameterTree& pt)
{ {
std::string v = "";
std::string k = "";
for(int i=1; i<argc; i++) for(int i=1; i<argc; i++)
{ {
std::string s(argv[i]);
if ((argv[i][0]=='-') && (argv[i][1]!='\000')) if ((argv[i][0]=='-') && (argv[i][1]!='\000'))
{ {
k = argv[i]+1; if(argv[i+1] == NULL)
continue; DUNE_THROW(RangeError, "last option on command line (" << argv[i]
} << ") does not have an argument");
else pt[argv[i]+1] = argv[i+1];
{ ++i; // skip over option argument
if (k.size())
pt[k] = argv[i];
k.clear();
} }
} }
} }
void Dune::ParameterTreeParser::readNamedOptions(int argc, char* argv[], void Dune::ParameterTreeParser::readNamedOptions(int argc, char* argv[],
......
...@@ -230,6 +230,21 @@ void testFS1527() ...@@ -230,6 +230,21 @@ void testFS1527()
} }
} }
// check that negative values can be given on the command line
void testFS1523()
{
static char arg0[] = "progname";
static char arg1[] = "-setting";
static char arg2[] = "-1";
static char *argv[] = { arg0, arg1, arg2, NULL };
int argc = sizeof argv / sizeof (char *) - 1;
Dune::ParameterTree ptree;
Dune::ParameterTreeParser::readOptions(argc, argv, ptree);
check_assert(ptree.get<int>("setting") == -1);
}
int main() int main()
{ {
try { try {
...@@ -262,6 +277,7 @@ int main() ...@@ -262,6 +277,7 @@ int main()
// check for specific bugs // check for specific bugs
testFS1527(); testFS1527();
testFS1523();
} }
catch (Dune::Exception & e) catch (Dune::Exception & e)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment