8#include "ns3/command-line.h"
10#include "ns3/global-value.h"
12#include "ns3/string.h"
13#include "ns3/system-path.h"
15#include "ns3/type-id.h"
86 char** argv =
new char*[argc + 1];
89 argv[0] =
new char[strlen(ss.str().c_str()) + 1];
90 strcpy(argv[0], ss.str().c_str());
94 for (
int i = 1; i < argc; ++i)
96 char* arg = va_arg(ap,
char*);
97 argv[i] =
new char[strlen(arg) + 1];
102 cmd.Parse(argc, argv);
105 for (
int i = 0; i < argc; ++i)
129 void DoRun()
override;
142 bool myDefaultFalseBool =
false;
144 cmd.AddValue(
"my-bool",
"help", myBool);
145 cmd.AddValue(
"my-false-bool",
"help", myDefaultFalseBool);
147 Parse(cmd, 1,
"--my-bool=0");
150 "CommandLine did not correctly set a boolean value to false, given 0");
152 Parse(cmd, 1,
"--my-bool=1");
155 "CommandLine did not correctly set a boolean value to true, given 1");
157 Parse(cmd, 1,
"--my-bool");
160 "CommandLine did not correctly toggle a default true boolean value to "
161 "false, given no argument");
163 Parse(cmd, 1,
"--my-false-bool");
166 "CommandLine did not correctly toggle a default false boolean value to "
167 "true, given no argument");
169 Parse(cmd, 1,
"--my-bool=t");
173 "CommandLine did not correctly set a boolean value to true, given 't' argument");
175 Parse(cmd, 1,
"--my-bool=true");
179 "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
199 void DoRun()
override;
211 uint8_t myUint8 = 10;
213 cmd.AddValue(
"my-uint8",
"help", myUint8);
215 Parse(cmd, 1,
"--my-uint8=1");
218 "CommandLine did not correctly set a uint8_t value to 1, given 1");
238 void DoRun()
override;
253 cmd.AddValue(
"my-bool",
"help", myBool);
254 cmd.AddValue(
"my-int32",
"help", myInt32);
256 Parse(cmd, 2,
"--my-bool=0",
"--my-int32=-3");
259 "CommandLine did not correctly set a boolean value to false");
262 Parse(cmd, 2,
"--my-bool=1",
"--my-int32=+2");
265 "CommandLine did not correctly set a boolean value to true");
286 void DoRun()
override;
301 cmd.AddValue(
"my-bool",
"help", myBool);
302 cmd.AddValue(
"my-uint32",
"help", myUint32);
304 Parse(cmd, 2,
"--my-bool=0",
"--my-uint32=9");
308 "CommandLine did not correctly set a boolean value to false");
311 "CommandLine did not correctly set an unsigned integer value to 9");
331 void DoRun()
override;
344 std::string myStr =
"MyStr";
346 cmd.AddValue(
"my-uint32",
"help", myUint32);
347 cmd.AddValue(
"my-str",
"help", myStr);
349 Parse(cmd, 2,
"--my-uint32=9",
"--my-str=XX");
353 "CommandLine did not correctly set an unsigned integer value to 9");
356 "CommandLine did not correctly set a string value to \"XX\"");
376 void DoRun()
override;
390 cmd.AddValue(
"my-uint32",
"help", myUint32);
392 Parse(cmd, 2,
"--my-uint32=1",
"--my-uint32=2");
396 "CommandLine did not correctly set an unsigned integer value to 2");
416 void DoRun()
override;
430 cmd.AddValue(
"my-uint32",
"help", myUint32);
432 Parse(cmd, 2,
"quack",
"--my-uint32=5");
436 "CommandLine did not correctly set an unsigned integer value to 5");
456 void DoRun()
override;
470 std::string myStr =
"MyStr";
472 cmd.AddNonOption(
"my-bool",
"help", myBool);
473 cmd.AddNonOption(
"my-int",
"help", myInt);
474 cmd.AddNonOption(
"my-str",
"help", myStr);
476 Parse(cmd, 2,
"true",
"5");
481 "CommandLine did not correctly set an integer non-option value to 5");
484 Parse(cmd, 5,
"false",
"6",
"newValue",
"extraVal1",
"extraVal2");
489 "CommandLine did not correctly set an integer non-option value to 5");
494 "CommandLine did not parse the correct number of extra non-options.");
497 "CommandLine did not correctly get one extra non-option");
500 "CommandLine did not correctly get two extra non-option");
520 void DoRun()
override;
532 constexpr int CHARBUF_SIZE = 10;
533 char charbuf[CHARBUF_SIZE] =
"charstar";
536 cmd.AddValue(
"charbuf",
"a char* buffer", charbuf, CHARBUF_SIZE);
537 Parse(cmd, 1,
"--charbuf=deadbeef");
539 std::string value{charbuf};
Parse command-line arguments.
void AddTestCase(TestCase *testCase, Duration duration=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.