From 10baa77dbc06d75af09d9a46473e693e57650cdb Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Tue, 9 Oct 2007 11:38:01 +0100 Subject: [PATCH] Bug 81 (CommandLine::AddArgValue not working correctly) --- samples/main-default-value.cc | 2 +- src/core/command-line.cc | 97 ++++++++++++++++++++++++++++++++++- src/core/command-line.h | 10 ++-- 3 files changed, 103 insertions(+), 6 deletions(-) diff --git a/samples/main-default-value.cc b/samples/main-default-value.cc index dcaa8a83a..35ccb5547 100644 --- a/samples/main-default-value.cc +++ b/samples/main-default-value.cc @@ -62,7 +62,7 @@ int main (int argc, char* argv[]) //utilize the loops variable to show that it can be read from the command line if(loops>0) { - cout<<"You requested "< namespace ns3 { @@ -109,8 +110,15 @@ CommandLine::Parse (int argc, char *argv[]) std::string name, value; if (cur == std::string::npos) { + if (argc == 1) + { + // invalid argument. ignore it. + continue; + } + argv++; + argc--; name = param; - value = ""; + value = *argv; } else { @@ -124,7 +132,12 @@ CommandLine::Parse (int argc, char *argv[]) DefaultValueBase *item = *i; if (item->GetName () == name) { - item->ParseValue (value); + if (!item->ParseValue (value)) + { + std::cerr << "Warning: failed to parse command line argument `" + << name << "' of type '" << item->GetType () + << "' with value `" << value << "'." << std::endl; + } continue; } } @@ -144,3 +157,83 @@ CommandLine::Parse (int argc, char *argv[]) } }//namespace ns3 + + + +#ifdef RUN_SELF_TESTS +#include "test.h" +#include +#include + +namespace ns3 { + + +class CommandLineTest : public Test +{ +public: + CommandLineTest () : Test ("CommandLine") {} + virtual bool RunTests (void) + { + bool result = true; + + // redirect stderr temporarily (else warnings appear during unit testing, which is not nice) + std::ostringstream nullout; + std::streambuf *origcerr = std::cerr.rdbuf (nullout.rdbuf ()); + { + char *argv[] = {"run-tests", "--loops", "bad-value", NULL}; + int argc = sizeof (argv) / sizeof (argv[0]) - 1; + + uint32_t loops = 123; + CommandLine::AddArgValue ("loops","a test of the command line", loops); + CommandLine::Parse (argc, argv); + + NS_TEST_ASSERT_EQUAL (loops, 123); + } + + { + char *argv[] = {"run-tests", "--loops=bad-value", NULL}; + int argc = sizeof (argv) / sizeof (argv[0]) - 1; + + uint32_t loops = 123; + CommandLine::AddArgValue ("loops","a test of the command line", loops); + CommandLine::Parse (argc, argv); + + NS_TEST_ASSERT_EQUAL (loops, 123); + } + + { + char *argv[] = {"run-tests", "--loops", "456", NULL}; + int argc = sizeof (argv) / sizeof (argv[0]) - 1; + + uint32_t loops = 123; + CommandLine::AddArgValue ("loops","a test of the command line", loops); + CommandLine::Parse (argc, argv); + + NS_TEST_ASSERT_EQUAL (loops, 456); + } + + { + char *argv[] = {"run-tests", "--loops=456", NULL}; + int argc = sizeof (argv) / sizeof (argv[0]) - 1; + + uint32_t loops = 123; + CommandLine::AddArgValue ("loops","a test of the command line", loops); + CommandLine::Parse (argc, argv); + + NS_TEST_ASSERT_EQUAL (loops, 456); + } + + // unredirect cerr + std::cerr.rdbuf (origcerr); + + + return result; + } +}; + + +static CommandLineTest g_commandLineTests; + +}//namespace ns3 + +#endif /* RUN_SELF_TESTS */ diff --git a/src/core/command-line.h b/src/core/command-line.h index 36e5382c9..8a1c49daa 100644 --- a/src/core/command-line.h +++ b/src/core/command-line.h @@ -123,14 +123,18 @@ CommandLine::UserDefaultValue::DoParseValue (const std::string &value) iss.str (value); T v; iss >> v; - *m_valuePtr = v; - return !iss.bad () && !iss.fail (); + bool ok = (!iss.bad () && !iss.fail ()); + if (ok) + { + *m_valuePtr = v; + } + return ok; } template std::string CommandLine::UserDefaultValue::DoGetType (void) const { - return ""; + return TypeNameGet (); } template std::string