19 #include "ns3/command-line.h"
20 #include "ns3/config.h"
21 #include "ns3/global-value.h"
23 #include "ns3/string.h"
24 #include "ns3/system-path.h"
26 #include "ns3/type-id.h"
97 char** argv =
new char*[argc + 1];
100 argv[0] =
new char[strlen(ss.str().c_str()) + 1];
101 strcpy(argv[0], ss.str().c_str());
105 for (
int i = 1; i < argc; ++i)
107 char* arg = va_arg(ap,
char*);
108 argv[i] =
new char[strlen(arg) + 1];
109 strcpy(argv[i], arg);
113 cmd.Parse(argc, argv);
116 for (
int i = 0; i < argc; ++i)
140 void DoRun()
override;
153 bool myDefaultFalseBool =
false;
155 cmd.AddValue(
"my-bool",
"help", myBool);
156 cmd.AddValue(
"my-false-bool",
"help", myDefaultFalseBool);
161 "CommandLine did not correctly set a boolean value to false, given 0");
166 "CommandLine did not correctly set a boolean value to true, given 1");
171 "CommandLine did not correctly toggle a default true boolean value to "
172 "false, given no argument");
177 "CommandLine did not correctly toggle a default false boolean value to "
178 "true, given no argument");
184 "CommandLine did not correctly set a boolean value to true, given 't' argument");
190 "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
210 void DoRun()
override;
222 uint8_t myUint8 = 10;
224 cmd.AddValue(
"my-uint8",
"help", myUint8);
229 "CommandLine did not correctly set a uint8_t value to 1, given 1");
249 void DoRun()
override;
262 int32_t myInt32 = 10;
264 cmd.AddValue(
"my-bool",
"help", myBool);
265 cmd.AddValue(
"my-int32",
"help", myInt32);
267 Parse(
cmd, 2,
"--my-bool=0",
"--my-int32=-3");
270 "CommandLine did not correctly set a boolean value to false");
273 Parse(
cmd, 2,
"--my-bool=1",
"--my-int32=+2");
276 "CommandLine did not correctly set a boolean value to true");
297 void DoRun()
override;
310 uint32_t myUint32 = 10;
312 cmd.AddValue(
"my-bool",
"help", myBool);
313 cmd.AddValue(
"my-uint32",
"help", myUint32);
315 Parse(
cmd, 2,
"--my-bool=0",
"--my-uint32=9");
319 "CommandLine did not correctly set a boolean value to false");
322 "CommandLine did not correctly set an unsigned integer value to 9");
342 void DoRun()
override;
354 uint32_t myUint32 = 10;
355 std::string myStr =
"MyStr";
357 cmd.AddValue(
"my-uint32",
"help", myUint32);
358 cmd.AddValue(
"my-str",
"help", myStr);
360 Parse(
cmd, 2,
"--my-uint32=9",
"--my-str=XX");
364 "CommandLine did not correctly set an unsigned integer value to 9");
367 "CommandLine did not correctly set a string value to \"XX\"");
387 void DoRun()
override;
399 uint32_t myUint32 = 0;
401 cmd.AddValue(
"my-uint32",
"help", myUint32);
403 Parse(
cmd, 2,
"--my-uint32=1",
"--my-uint32=2");
407 "CommandLine did not correctly set an unsigned integer value to 2");
427 void DoRun()
override;
439 uint32_t myUint32 = 0;
441 cmd.AddValue(
"my-uint32",
"help", myUint32);
443 Parse(
cmd, 2,
"quack",
"--my-uint32=5");
447 "CommandLine did not correctly set an unsigned integer value to 5");
467 void DoRun()
override;
481 std::string myStr =
"MyStr";
483 cmd.AddNonOption(
"my-bool",
"help", myBool);
484 cmd.AddNonOption(
"my-int",
"help", myInt);
485 cmd.AddNonOption(
"my-str",
"help", myStr);
492 "CommandLine did not correctly set an integer non-option value to 5");
495 Parse(
cmd, 5,
"false",
"6",
"newValue",
"extraVal1",
"extraVal2");
500 "CommandLine did not correctly set an integer non-option value to 5");
505 "CommandLine did not parse the correct number of extra non-options.");
508 "CommandLine did not correctly get one extra non-option");
511 "CommandLine did not correctly get two extra non-option");
531 void DoRun()
override;
543 constexpr
int CHARBUF_SIZE = 10;
544 char charbuf[CHARBUF_SIZE] =
"charstar";
547 cmd.AddValue(
"charbuf",
"a char* buffer", charbuf, CHARBUF_SIZE);
548 Parse(
cmd, 1,
"--charbuf=deadbeef");
550 std::string value{charbuf};
Parse command-line arguments.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
TestCase * GetParent() const
Get the parent of this TestCase.
std::string GetName() const
Test boolean Command Line processing.
CommandLineBooleanTestCase()
Constructor.
void DoRun() override
Run the test.
~CommandLineBooleanTestCase() override
Destructor.
Test char* buffer argument.
~CommandLineCharStarTestCase() override
Destructor.
void DoRun() override
Run the test.
CommandLineCharStarTestCase()
Constructor.
Test int Command Line processing.
~CommandLineIntTestCase() override
Destructor.
void DoRun() override
Run the test.
CommandLineIntTestCase()
Constructor.
Test ignoring invalid arguments.
void DoRun() override
Run the test.
~CommandLineInvalidTestCase() override
Destructor.
CommandLineInvalidTestCase()
Constructor.
Test non-option arguments.
CommandLineNonOptionTestCase()
Constructor.
void DoRun() override
Run the test.
~CommandLineNonOptionTestCase() override
Destructor.
Test order of argument parsing.
CommandLineOrderTestCase()
Constructor.
~CommandLineOrderTestCase() override
Destructor.
void DoRun() override
Run the test.
Test string Command Line processing.
void DoRun() override
Run the test.
~CommandLineStringTestCase() override
Destructor.
CommandLineStringTestCase()
Constructor.
A test base class that drives Command Line parsing.
CommandLineTestCaseBase(std::string description)
Constructor.
void Parse(CommandLine &cmd, int n,...)
Exercise the CommandLine with the provided arguments.
static int m_count
Test iteration counter to give each test a unique name.
~CommandLineTestCaseBase() override
Destructor.
The Test Suite that glues all of the Test Cases together.
CommandLineTestSuite()
Constructor.
Test uint8_t Command Line processing.
CommandLineUint8tTestCase()
Constructor.
~CommandLineUint8tTestCase() override
Destructor.
void DoRun() override
Run the test.
Test unsigned int Command Line processing.
void DoRun() override
Run the test.
~CommandLineUnsignedIntTestCase() override
Destructor.
CommandLineUnsignedIntTestCase()
Constructor.
static CommandLineTestSuite g_commandLineTestSuite
CommandLineTestSuite instance variable.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.