diff --git a/examples/csma-broadcast.cc b/examples/csma-broadcast.cc index d64920114..c3deef7d6 100644 --- a/examples/csma-broadcast.cc +++ b/examples/csma-broadcast.cc @@ -96,7 +96,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/csma-multicast.cc b/examples/csma-multicast.cc index a68aa9a9e..d5993f04f 100644 --- a/examples/csma-multicast.cc +++ b/examples/csma-multicast.cc @@ -97,7 +97,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // diff --git a/examples/csma-one-subnet.cc b/examples/csma-one-subnet.cc index 7b1642fe2..9f6497d7c 100644 --- a/examples/csma-one-subnet.cc +++ b/examples/csma-one-subnet.cc @@ -89,7 +89,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // diff --git a/examples/csma-packet-socket.cc b/examples/csma-packet-socket.cc index 9869f5d5d..e6d4f80fd 100644 --- a/examples/csma-packet-socket.cc +++ b/examples/csma-packet-socket.cc @@ -102,7 +102,8 @@ main (int argc, char *argv[]) LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL); #endif - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/mixed-global-routing.cc b/examples/mixed-global-routing.cc index 0b775fda3..7e40bd978 100644 --- a/examples/mixed-global-routing.cc +++ b/examples/mixed-global-routing.cc @@ -111,7 +111,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind ()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); NS_LOG_INFO ("Create nodes."); Ptr n0 = CreateObject (); diff --git a/examples/simple-alternate-routing.cc b/examples/simple-alternate-routing.cc index e8b3494c2..6c760a355 100644 --- a/examples/simple-alternate-routing.cc +++ b/examples/simple-alternate-routing.cc @@ -103,15 +103,14 @@ main (int argc, char *argv[]) // DefaultValue::Bind () technique to tell the system what subclass of // Queue to use, and what the queue limit is - // The below Bind command tells the queue factory which class to - // instantiate, when the queue factory is invoked in the topology code - DefaultValue::Bind ("Queue", "DropTailQueue"); Config::SetDefault ("OnOffApplication::PacketSize", Uinteger (210)); Config::SetDefault ("OnOffApplication::DataRate", DataRate ("300b/s")); // The below metric, if set to 3 or higher, will cause packets between // n1 and n3 to take the 2-hop route through n2 + + CommandLine cmd; // // Additionally, we plumb this metric into the default value / command // line argument system as well, for exemplary purposes. This means @@ -119,13 +118,13 @@ main (int argc, char *argv[]) // rather than recompiling // e.g. waf --run "simple-alternate-routing --AlternateCost=5" uint16_t sampleMetric = 1; - CommandLine::AddArgValue ("AlternateCost", - "This metric is used in the example script between n3 and n1 ", - sampleMetric); + cmd.AddValue ("AlternateCost", + "This metric is used in the example script between n3 and n1 ", + sampleMetric); // Allow the user to override any of the defaults and the above // DefaultValue::Bind ()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/simple-error-model.cc b/examples/simple-error-model.cc index d9048fb62..a1325c8a0 100644 --- a/examples/simple-error-model.cc +++ b/examples/simple-error-model.cc @@ -92,7 +92,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/simple-global-routing.cc b/examples/simple-global-routing.cc index c505dfb0d..c06c34189 100644 --- a/examples/simple-global-routing.cc +++ b/examples/simple-global-routing.cc @@ -113,7 +113,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // DefaultValue::Bind ()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/simple-point-to-point-olsr.cc b/examples/simple-point-to-point-olsr.cc index 85f6a69d3..f0bf2c358 100644 --- a/examples/simple-point-to-point-olsr.cc +++ b/examples/simple-point-to-point-olsr.cc @@ -109,7 +109,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/simple-point-to-point.cc b/examples/simple-point-to-point.cc index d4b3d94e4..aec485fe9 100644 --- a/examples/simple-point-to-point.cc +++ b/examples/simple-point-to-point.cc @@ -84,7 +84,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create four nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/tcp-large-transfer-errors.cc b/examples/tcp-large-transfer-errors.cc index 57fcdae56..46f565ebe 100644 --- a/examples/tcp-large-transfer-errors.cc +++ b/examples/tcp-large-transfer-errors.cc @@ -141,7 +141,8 @@ int main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create three nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/tcp-large-transfer.cc b/examples/tcp-large-transfer.cc index 7a25ff60f..f9f1909fb 100644 --- a/examples/tcp-large-transfer.cc +++ b/examples/tcp-large-transfer.cc @@ -141,7 +141,8 @@ int main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create three nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/tcp-nonlistening-server.cc b/examples/tcp-nonlistening-server.cc index 645e35be3..c14e9c947 100644 --- a/examples/tcp-nonlistening-server.cc +++ b/examples/tcp-nonlistening-server.cc @@ -103,7 +103,8 @@ int main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create three nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/tcp-small-transfer-oneloss.cc b/examples/tcp-small-transfer-oneloss.cc index 6a9c0dec9..96013edc8 100644 --- a/examples/tcp-small-transfer-oneloss.cc +++ b/examples/tcp-small-transfer-oneloss.cc @@ -122,7 +122,8 @@ int main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create three nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/tcp-small-transfer.cc b/examples/tcp-small-transfer.cc index 18e8f66dc..bf2369366 100644 --- a/examples/tcp-small-transfer.cc +++ b/examples/tcp-small-transfer.cc @@ -133,7 +133,8 @@ int main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above // Bind()s at run-time, via command-line arguments - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // Here, we will explicitly create three nodes. In more sophisticated // topologies, we could configure a node factory. diff --git a/examples/udp-echo.cc b/examples/udp-echo.cc index a70392d32..42cdf87da 100644 --- a/examples/udp-echo.cc +++ b/examples/udp-echo.cc @@ -89,7 +89,8 @@ main (int argc, char *argv[]) // Allow the user to override any of the defaults and the above Bind() at // run-time, via command-line arguments // - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); // // Explicitly create the nodes required by the topology (shown above). // diff --git a/samples/main-adhoc-wifi.cc b/samples/main-adhoc-wifi.cc index 218b17203..89f8c1c3a 100644 --- a/samples/main-adhoc-wifi.cc +++ b/samples/main-adhoc-wifi.cc @@ -177,7 +177,8 @@ int main (int argc, char *argv[]) Config::SetDefault ("WifiRemoteStationManager::FragmentationThreshold", String ("2200")); Config::SetDefault ("WifiRemoteStationManager::RtsCtsThreshold", String ("2200")); - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); Gnuplot gnuplot = Gnuplot ("reference-rates.png"); diff --git a/samples/main-default-value.cc b/samples/main-default-value.cc deleted file mode 100644 index 35ccb5547..000000000 --- a/samples/main-default-value.cc +++ /dev/null @@ -1,85 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ - -#include -#include "ns3/default-value.h" -#include "ns3/command-line.h" -#include "ns3/log.h" - -using namespace ns3; - -// -// This sample file demonstrates how to take some simple member -// variables and hook them into the default variable system -// Typically, you will establish a static variable to maintain the current -// value of the default parameter. Then as other code require the values of -// the defaults, they query them with GetValue() to get the present value. -static BooleanDefaultValue defaultTestBool1 ("testBool1", "helpBool", true); -static NumericDefaultValue defaultTestInt1 ("testInt1", "helpInt1", 33); -static NumericDefaultValue defaultTestInt2 ("testInt2", "helpInt2", 47); - -// -// This test class demonstrates the declaration of variables that -// may be overridden by the default-value system -// -// You will see in the core ns-3 modules that many member variables -// can be overridden in this manner -// -class TestClass { -public: - TestClass(); - virtual ~TestClass () {} - - bool m_testBool1; - int m_testInt1; - uint32_t m_testInt2; -}; - -// -// In the constructor, you can assign default values in the initializer -// list such as below; note that the instance of the created TestClass -// will have the values as dictated by the current value of the default. -// This means that the behavior of this class can be changed on the fly with -// calls to bind. -// -TestClass::TestClass () : - m_testBool1(defaultTestBool1.GetValue()), - m_testInt1(defaultTestInt1.GetValue()), - m_testInt2(defaultTestInt2.GetValue()) -{ -} -using std::cout; -int main (int argc, char* argv[]) -{ - //The following allows the default values established so far to be hooked - //into the command line argument processing unit. Essentially, the command - //line processor is aware of the DefaultValues that have been registered, and - //will accept command line overrides of these. The call automatically - //provides a --help option in addition to allowing overrides of defaults. - uint32_t loops = 0; - CommandLine::AddArgValue("loops","a test of the command line",loops); - CommandLine::Parse(argc,argv); - - //utilize the loops variable to show that it can be read from the command line - if(loops>0) - { - std::cerr<<"You requested "<m_testBool1 << ")"); - NS_LOG_UNCOND("TestInt1 default value (" << testclass->m_testInt1 << ")"); - NS_LOG_UNCOND("TestInt2 default value (" << testclass->m_testInt2 << ")"); - delete testclass; - - return 0; -} diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 33ef2b482..3eb56f7a7 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -13,7 +13,8 @@ using namespace ns3; int main (int argc, char *argv[]) { - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); std::vector > nodes; diff --git a/samples/main-random-topology.cc b/samples/main-random-topology.cc index 35cfc2f24..0f141950a 100644 --- a/samples/main-random-topology.cc +++ b/samples/main-random-topology.cc @@ -4,7 +4,6 @@ #include "ns3/ptr.h" #include "ns3/mobility-model.h" -#include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/simulator.h" #include "ns3/nstime.h" @@ -26,8 +25,8 @@ CourseChange (std::string context, Ptr position) int main (int argc, char *argv[]) { - - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); std::vector > objects; diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index a1cd51a55..724ae65e1 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -34,7 +34,8 @@ int main (int argc, char *argv[]) Config::SetDefault ("RandomWalk2dMobilityModel::Speed", String ("Constant:1.0")); Config::SetDefault ("RandomWalk2dMobilityModel::Bounds", String ("0:200:0:100")); - CommandLine::Parse (argc, argv); + CommandLine cmd; + cmd.Parse (argc, argv); for (uint32_t i = 0; i < 100; i++) { diff --git a/samples/wscript b/samples/wscript index b03196067..b6500b517 100644 --- a/samples/wscript +++ b/samples/wscript @@ -23,10 +23,6 @@ def build(bld): ['node', 'internet-node', 'onoff']) obj.source = 'main-simple.cc' - obj = bld.create_ns3_program('main-default-value', - ['core', 'simulator', 'node', 'point-to-point']) - obj.source = 'main-default-value.cc' - obj = bld.create_ns3_program('main-grid-topology', ['core', 'simulator', 'mobility', 'internet-node']) obj.source = 'main-grid-topology.cc' diff --git a/src/core/command-line.cc b/src/core/command-line.cc index 815bc6aad..84eee3276 100644 --- a/src/core/command-line.cc +++ b/src/core/command-line.cc @@ -1,247 +1,309 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 Georgia Tech University, INRIA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Authors: Raj Bhattacharjea , - * Mathieu Lacage - */ - #include "command-line.h" -#include +#include "log.h" +#include "config.h" +#include "global-value.h" +#include "string.h" +#include + +NS_LOG_COMPONENT_DEFINE ("CommandLine"); namespace ns3 { -CommandLine::List::~List () +CommandLine::~CommandLine () { - for (iterator iter = begin (); iter != end (); iter++) + for (Items::const_iterator i = m_items.begin (); i != m_items.end (); ++i) { - delete *iter; + delete *i; } + m_items.clear (); } - -CommandDefaultValue CommandLine::g_help ("help", - "Print Help text for all commands", - MakeCallback (&CommandLine::PrintHelp)); +CommandLine::Item::~Item () +{} void -CommandLine::AddArgCommand (const std::string &name, - const std::string &help, - Callback cb) -{ - DefaultValueBase *base = - new CommandDefaultValue (name, help, cb); - GetUserList ()->push_back (base); -} - -CommandLine::List * -CommandLine::GetUserList (void) -{ - static List list; - return &list; -} - -void -CommandLine::PrintHelp (void) -{ - for (List::iterator i = GetUserList ()->begin (); - i != GetUserList ()->end (); i++) - { - DefaultValueBase *item = *i; - if (item->GetType () == "" && - item->GetDefaultValue () == "") - { - std::cout << "--" << item->GetName () << "\t" << item->GetHelp () << std::endl; - } - else - { - std::cout << "--" << item->GetName () << "=[" << item->GetType () << ":" - << item->GetDefaultValue () << "]\t" << item->GetHelp () << std::endl; - } - } - for (List::iterator i = DefaultValueList::Begin (); - i != DefaultValueList::End (); i++) - { - DefaultValueBase *item = *i; - if (item->GetType () == "" && - item->GetDefaultValue () == "") - { - std::cout << "--" << item->GetName () << "\t" << item->GetHelp () << std::endl; - } - else - { - std::cout << "--" << item->GetName () << "=[" << item->GetType () << ":" - << item->GetDefaultValue () << "]\t" << item->GetHelp () << std::endl; - } - } - // XXX on win32, do the right thing here. - exit (0); -} - -void -CommandLine::Parse (int argc, char *argv[]) +CommandLine::Parse (int &iargc, char *argv[]) const { + int argc = iargc; for (argc--, argv++; argc > 0; argc--, argv++) { // remove "--" or "-" heading. std::string param = *argv; std::string::size_type cur = param.find ("--"); - if (cur == std::string::npos) - { - cur = param.find ("-"); - if (cur == std::string::npos) - { - // invalid argument. ignore it. - continue; - } - } - if (cur != 0) - { - // invalid argument. ignore it. - continue; - } - param = std::string (param, 2, param.size ()); + if (cur == 0) + { + param = param.substr (2, param.size () - 2); + } + else + { + cur = param.find ("-"); + if (cur == 0) + { + param = param.substr (1, param.size () - 1); + } + else + { + // invalid argument. ignore. + continue; + } + } cur = param.find ("="); std::string name, value; if (cur == std::string::npos) { - if (argc == 1) - { - // invalid argument. ignore it. - continue; - } - argv++; - argc--; - name = param; - value = *argv; + name = param; + value = ""; } else { - name = std::string (param, 0, cur); - value = std::string (param, cur + 1, std::string::npos); - } - // try to find this argument in the user args. - for (List::iterator i = GetUserList ()->begin (); - i != GetUserList ()->end (); i++) - { - DefaultValueBase *item = *i; - if (item->GetName () == name) - { - if (!item->ParseValue (value)) - { - std::cerr << "Warning: failed to parse command line argument `" - << name << "' of type '" << item->GetType () - << "' with value `" << value << "'." << std::endl; - } - continue; - } - } - - // try to find this argument in the default args. - for (List::iterator i = DefaultValueList::Begin (); - i != DefaultValueList::End (); i++) - { - DefaultValueBase *item = *i; - if (item->GetName () == name) - { - item->ParseValue (value); - continue; - } + name = param.substr (0, cur); + value = param.substr (cur + 1, param.size () - (cur+1)); } + HandleArgument (name, value); } } -}//namespace ns3 +void +CommandLine::PrintHelp (void) const +{ + std::cout << "--PrintHelp: Print this help message." << std::endl; + std::cout << "--PrintGroups: Print the list of groups." << std::endl; + std::cout << "--PrintTypeIds: Print all TypeIds." << std::endl; + std::cout << "--PrintGroup=[group]: Print all TypeIds of group." << std::endl; + std::cout << "--PrintAttributes=[typeid]: Print all attributes of typeid." << std::endl; + std::cout << "--PrintGlobals: Print the list of globals." << std::endl; + if (!m_items.empty ()) + { + std::cout << "User Arguments:" << std::endl; + for (Items::const_iterator i = m_items.begin (); i != m_items.end (); ++i) + { + std::cout << " --" << (*i)->m_name << ": " << (*i)->m_help << std::endl; + } + } + exit (0); +} + +void +CommandLine::PrintGlobals (void) const +{ + for (GlobalValue::Iterator i = GlobalValue::Begin (); i != GlobalValue::End (); ++i) + { + std::cout << " --" << (*i)->GetName () << "=["; + Ptr checker = (*i)->GetChecker (); + Attribute value = (*i)->GetValue (); + std::cout << value.SerializeToString (checker) << "]: " + << (*i)->GetHelp () << std::endl; + } + exit (0); +} + +void +CommandLine::PrintAttributes (std::string type) const +{ + TypeId tid; + if (!TypeId::LookupByNameFailSafe (type, &tid)) + { + NS_FATAL_ERROR ("Unknown type="< checker = tid.GetAttributeChecker (i); + Attribute initial = tid.GetAttributeInitialValue (i); + std::cout << initial.SerializeToString (checker) << "]: " + << tid.GetAttributeHelp (i) << std::endl; + } + exit (0); +} +void +CommandLine::PrintGroup (std::string group) const +{ + for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i) + { + TypeId tid = TypeId::GetRegistered (i); + if (tid.GetGroupName () == group) + { + std::cout << " --PrintAttributes=" <::const_iterator j = groups.begin (); j != groups.end (); ++j) + { + if (*j == group) + { + found = true; + break; + } + } + if (!found) + { + groups.push_back (group); + } + } + for (std::list::const_iterator k = groups.begin (); k != groups.end (); ++k) + { + std::cout << " --PrintGroup="<<*k<m_name == name) + { + if (!(*i)->Parse (value)) + { + std::cerr << "Invalid argument value: "< -#include - -namespace ns3 { +#include +using 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 ()); - { - const 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, const_cast (argv)); - - NS_TEST_ASSERT_EQUAL (loops, 123); - } - - { - const 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, const_cast (argv)); - - NS_TEST_ASSERT_EQUAL (loops, 123); - } - - { - const 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, const_cast (argv)); - - NS_TEST_ASSERT_EQUAL (loops, 456); - } - - { - const 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, const_cast (argv)); - - NS_TEST_ASSERT_EQUAL (loops, 456); - } - - // unredirect cerr - std::cerr.rdbuf (origcerr); - - - return result; - } + CommandLineTest (); + virtual bool RunTests (void); +private: + void Parse (const CommandLine &cmd, int n, ...); }; +CommandLineTest::CommandLineTest () + : Test ("CommandLine") +{} +void +CommandLineTest::Parse (const CommandLine &cmd, int n, ...) +{ + char **args = new char* [n+1]; + args[0] = "Test"; + va_list ap; + va_start (ap, n); + int i = 0; + while (i < n) + { + char *arg = va_arg (ap, char *); + args[i+1] = arg; + i++; + } + int argc = n + 1; + cmd.Parse (argc, args); + delete [] args; +} +bool +CommandLineTest::RunTests (void) +{ + bool result = true; -static CommandLineTest g_commandLineTests; + bool myBool = false; + uint32_t myUint32 = 10; + std::string myStr = "MyStr"; + int32_t myInt32 = -1; + CommandLine cmd; -}//namespace ns3 + cmd.AddValue ("my-bool", "help", myBool); + Parse (cmd, 1, "--my-bool=0"); + NS_TEST_ASSERT_EQUAL (myBool, false); + Parse (cmd, 1, "--my-bool=1"); + NS_TEST_ASSERT_EQUAL (myBool, true); + + cmd.AddValue ("my-uint32", "help", myUint32); + Parse (cmd, 2, "--my-bool=0", "--my-uint32=9"); + NS_TEST_ASSERT_EQUAL (myUint32, 9); + + cmd.AddValue ("my-str", "help", myStr); + Parse (cmd, 2, "--my-bool=0", "--my-str=XX"); + NS_TEST_ASSERT_EQUAL (myStr, "XX"); + + cmd.AddValue ("my-int32", "help", myInt32); + Parse (cmd, 2, "--my-bool=0", "--my-int32=-3"); + NS_TEST_ASSERT_EQUAL (myInt32, -3); + Parse (cmd, 2, "--my-bool=0", "--my-int32=+2"); + NS_TEST_ASSERT_EQUAL (myInt32, +2); + + return result; +} + + +static CommandLineTest g_cmdLineTest; #endif /* RUN_SELF_TESTS */ diff --git a/src/core/command-line.h b/src/core/command-line.h index 7edd7becb..c322f1bc2 100644 --- a/src/core/command-line.h +++ b/src/core/command-line.h @@ -1,153 +1,78 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 Georgia Tech University, INRIA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Authors: Raj Bhattacharjea , - * Mathieu Lacage - */ -#ifndef COMMAND_LINE_H -#define COMMAND_LINE_H +#ifndef CMD_LINE_H +#define CMD_LINE_H -#include #include -#include "default-value.h" +#include +#include namespace ns3 { -/** - * \brief Command line argument processing class - * \ingroup config - * - * Often times in simulations, the user will want the ability to change and - * tweak simulation parameters without having to recompile the entire - * simulation. This can be done by passing arguments in from the command - * line invocation of a simulation. - * This class is a collection of static functions to aid in this facility. - * By using it, users get the automatic things like support for --help command - * line arguments. See samples/main-default-value.cc for more info. - */ class CommandLine { public: - /** - * \brief Add a command line argument to the system - * \param name Name of the string to expect on the command line - * \param help A help string for this param, displayed --help is used. - * \param value The desination to store the value read from the command line - */ + ~CommandLine (); + template - static void AddArgValue (const std::string &name, - const std::string &help, - T &value); - /** - * \brief Add a command line triggered function call to the system - * \param name Name of the string to expect on the command line - * \param help A help string for this param, displayed --help is used. - * \param cb An ns3::Callback that gets called if name is present as a - * commandline argument. - */ - static void AddArgCommand (const std::string &name, - const std::string &help, - Callback cb); - /** - * \brief Parse the command line for arguments thus far added - * \param argc Number of strings on the command line; pass this directly from - * the first argument of your main(int,char**) function - * \param argv Array of strings passed in as arguments; pass this directly from - * the second argument of your main(int,char**) function - */ - static void Parse (int argc, char *argv[]); - private: - template - class UserDefaultValue : public DefaultValueBase + void AddValue (const std::string &name, + const std::string &help, + T &value); + + void Parse (int &argc, char *argv[]) const; +private: + class Item { public: - UserDefaultValue (const std::string &name, - const std::string &help, - T &value); - private: - virtual bool DoParseValue (const std::string &value); - virtual std::string DoGetType (void) const; - virtual std::string DoGetDefaultValue (void) const; + std::string m_name; + std::string m_help; + virtual ~Item (); + virtual bool Parse (std::string value) = 0; + }; + template + class UserItem : public Item + { + public: + virtual bool Parse (std::string value); T *m_valuePtr; }; - static void PrintHelp (void); + void HandleArgument (std::string name, std::string value) const; + void PrintHelp (void) const; + void PrintGlobals (void) const; + void PrintAttributes (std::string type) const; + void PrintGroup (std::string group) const; + void PrintTypeIds (void) const; + void PrintGroups (void) const; - class List : public std::list - { - public: - ~List (); - }; - static List *GetUserList (void); - static CommandDefaultValue g_help; + typedef std::list Items; + Items m_items; }; -}//namespace ns3 +} // namespace ns3 namespace ns3 { template void -CommandLine::AddArgValue (const std::string &name, - const std::string &help, - T &value) +CommandLine::AddValue (const std::string &name, + const std::string &help, + T &value) { - DefaultValueBase *base = - new UserDefaultValue (name, help, value); - GetUserList ()->push_back (base); + UserItem *item = new UserItem (); + item->m_name = name; + item->m_help = help; + item->m_valuePtr = &value; + m_items.push_back (item); } - template -CommandLine::UserDefaultValue::UserDefaultValue (const std::string &name, - const std::string &help, - T &value) - : DefaultValueBase (name, help), - m_valuePtr (&value) -{ - // we do not register in the DefaultValueList class on purpose. -} -template -bool -CommandLine::UserDefaultValue::DoParseValue (const std::string &value) +bool +CommandLine::UserItem::Parse (std::string value) { std::istringstream iss; iss.str (value); - T v; - iss >> v; - bool ok = (!iss.bad () && !iss.fail ()); - if (ok) - { - *m_valuePtr = v; - } - return ok; -} -template -std::string -CommandLine::UserDefaultValue::DoGetType (void) const -{ - return TypeNameGet (); -} -template -std::string -CommandLine::UserDefaultValue::DoGetDefaultValue (void) const -{ - return ""; + iss >> (*m_valuePtr); + return !iss.bad () && !iss.fail (); } -}//namespace ns3 +} // namespace ns3 -#endif /* COMMAND_LINE_H */ +#endif /* CMD_LINE_H */