20 #include "ns3/core-module.h"
69 #define DefaultFinal(label, defaultValue, finalValue) \
70 std::left << std::setw(20) << label + std::string(":") << std::setw(20) << defaultValue \
74 main(
int argc,
char* argv[])
79 std::string strArg =
"strArg default";
82 const std::string attrClass =
"ns3::RandomVariableStream";
83 const std::string attrName =
"Antithetic";
84 const std::string attrPath = attrClass +
"::" + attrName;
87 constexpr
int CHARBUF_SIZE = 10;
88 char charbuf[CHARBUF_SIZE] =
"charstar";
96 const int intDef = intArg;
97 const bool boolDef = boolArg;
98 const std::string strDef = strArg;
99 const std::string cbDef =
g_cbArg;
108 const std::string charbufDef{charbuf};
109 const int nonOpt1Def = nonOpt1;
110 const int nonOpt2Def = nonOpt2;
113 cmd.Usage(
"CommandLine example program.\n"
115 "This little program demonstrates how to use CommandLine.");
116 cmd.AddValue(
"intArg",
"an int argument", intArg);
117 cmd.AddValue(
"boolArg",
"a bool argument", boolArg);
118 cmd.AddValue(
"strArg",
"a string argument", strArg);
119 cmd.AddValue(
"anti", attrPath);
121 cmd.AddValue(
"charbuf",
"a char* buffer", charbuf, CHARBUF_SIZE);
122 cmd.AddNonOption(
"nonOpt1",
"first non-option", nonOpt1);
123 cmd.AddNonOption(
"nonOpt2",
"second non-option", nonOpt2);
124 cmd.Parse(argc, argv);
127 std::cout << std::endl;
128 std::cout <<
cmd.GetName() <<
":" << std::endl;
131 std::cout <<
"Program Version: ";
132 cmd.PrintVersion(std::cout);
133 std::cout << std::endl;
135 std::cout <<
"Argument Initial Value Final Value\n"
136 << std::left << std::boolalpha;
140 (boolDef ?
"true" :
"false"),
141 (boolArg ?
"true" :
"false"))
142 <<
DefaultFinal(
"strArg",
"\"" + strDef +
"\"",
"\"" + strArg +
"\"");
152 std::cout <<
DefaultFinal(
"anti",
"\"" + attrDef +
"\"",
"\"" + antiArg +
"\"")
155 "\"" + charbufDef +
"\"",
156 "\"" + std::string(charbuf) +
"\"")
158 <<
DefaultFinal(
"nonOpt2", nonOpt2Def, nonOpt2) << std::endl;
160 std::cout << std::setw(40)
161 <<
"Number of extra non-option arguments:" <<
cmd.GetNExtraNonOptions() << std::endl;
163 for (std::size_t i = 0; i <
cmd.GetNExtraNonOptions(); ++i)
167 "\"" +
cmd.GetExtraNonOption(i) +
"\"");
169 std::cout << std::endl;
Parse command-line arguments.
a unique identifier for an interface.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
#define DefaultFinal(label, defaultValue, finalValue)
Print a row containing the name, the default and the final values of an argument.
std::string g_cbArg
Global variable to illustrate command line arguments handled by a Callback function.
bool SetCbArg(const std::string &val)
Function to illustrate command line arguments handled by a Callback function.
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...