Bug 81 (CommandLine::AddArgValue not working correctly)
This commit is contained in:
1 parent
3a41b51160
commit
10baa77dbc
3 files changed
+103
-6
No files matched your search
@@ -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 "<<loops<<" iterations of a loop";
|
||||
std::cerr<<"You requested "<<loops<<" iterations of a loop";
|
||||
for(uint32_t i=0;i<loops;++i)
|
||||
cout<<"iteration "<<i;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include "command-line.h"
|
||||
#include "ns3/debug.h"
|
||||
#include <unistd.h>
|
||||
|
||||
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 <iostream>
|
||||
#include <sstream>
|
||||
|
||||
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 */
|
||||
@@ -123,14 +123,18 @@ CommandLine::UserDefaultValue<T>::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 <typename T>
|
||||
std::string
|
||||
CommandLine::UserDefaultValue<T>::DoGetType (void) const
|
||||
{
|
||||
return "";
|
||||
return TypeNameGet<T> ();
|
||||
}
|
||||
template <typename T>
|
||||
std::string
|
||||
|
||||
Reference in new issue
Block a user