19#if defined(ENABLE_BUILD_VERSION)
23#ifdef ENABLE_DES_METRICS
57 buffer.reserve(1.1 * source.size());
59 for (
size_t pos = 0; pos != source.size(); ++pos)
64 buffer.append(
"&");
67 buffer.append(
""");
70 buffer.append(
"'");
78 if (buffer.empty() || buffer.back() ==
' ')
80 buffer.append(
"<");
90 buffer.append(&source[pos], 1);
120 m_shortName = basename.substr(0, basename.rfind(
".cc"));
148 std::copy(cmd.m_options.begin(), cmd.m_options.end(),
m_options.end());
149 std::copy(cmd.m_nonOptions.begin(), cmd.m_nonOptions.end(),
m_nonOptions.end());
197 args.erase(args.begin());
201 for (
const auto& param : args)
214 "unexpected error parsing command line parameter: '" << param <<
"'");
218#ifdef ENABLE_DES_METRICS
219 DesMetrics::Get()->Initialize(args);
227 std::string arg = param;
228 std::string::size_type cur = arg.find(
"--");
231 arg = arg.substr(2, arg.size() - 2);
238 arg = arg.substr(1, arg.size() - 1);
243 return {
false, param,
""};
251 if (cur == std::string::npos)
258 name = arg.substr(0, cur);
259 value = arg.substr(cur + 1, arg.size() - (cur + 1));
262 return {
true, name, value};
270 for (
const auto& param : args)
279 if (name ==
"PrintHelp" || name ==
"help")
285 if (name ==
"PrintVersion" || name ==
"version")
291 else if (name ==
"PrintGroups")
297 else if (name ==
"PrintTypeIds")
303 else if (name ==
"PrintGlobals")
309 else if (name ==
"PrintGroup")
315 else if (name ==
"PrintAttributes")
348 auto item = std::make_shared<StringItem>();
349 item->m_name =
"extra-non-option-argument";
350 item->m_help =
"Extra non-option argument encountered.";
351 item->m_value = value;
356 if (!i->Parse(value))
358 std::cerr <<
"Invalid non-option argument value " << value <<
" for " << i->m_name
371 std::vector<std::string> args(argv, argv + argc);
383 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
" [General Arguments]"
392 std::size_t width = 0;
393 auto max_width = [&width](
const std::shared_ptr<Item> item) {
394 width = std::max(width, item->m_name.size());
397 std::for_each(nonOptions.begin(), nonOptions.end(), max_width);
400 auto optionsHelp = [&os, width](
const std::string& head,
bool option,
const Items& items) {
401 os <<
"\n" << head <<
"\n";
402 for (
const auto& item : items)
404 os <<
" " << (option ?
"--" :
" ") << std::left << std::setw(width)
405 << (item->m_name +
":") << std::right << item->m_help;
407 if (item->HasDefault())
409 os <<
" [" << item->GetDefault() <<
"]";
417 optionsHelp(
"Program Options:",
true,
m_options);
420 if (!nonOptions.empty())
422 optionsHelp(
"Program Arguments:",
false, nonOptions);
426 os <<
"General Arguments:\n"
427 <<
" --PrintGlobals: Print the list of globals.\n"
428 <<
" --PrintGroups: Print the list of groups.\n"
429 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
430 <<
" --PrintTypeIds: Print all TypeIds.\n"
431 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
432 <<
" --PrintVersion: Print the ns-3 version.\n"
433 <<
" --PrintHelp: Print this help message.\n"
440#if defined(ENABLE_BUILD_VERSION)
443 return std::string{
"Build version support is not enabled, reconfigure with "
444 "--enable-build-version flag"};
467 NS_FATAL_ERROR(
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
476 NS_LOG_INFO(
"Writing CommandLine doxy to " << outf);
478 std::fstream os(outf, std::fstream::out);
481 <<
"<h3>Usage</h3>\n"
483 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
"\"</code>\n";
490 auto listOptions = [&os](
const std::string& head,
const Items& items, std::string pre) {
491 os <<
"\n<h3>" << head <<
"</h3>\n<dl>\n";
492 for (
const auto& i : items)
494 os <<
" <dt>" << pre << i->m_name <<
" </dt>\n"
495 <<
" <dd>" << Encode(i->m_help);
499 os <<
" [" << Encode(i->GetDefault()) <<
"]";
508 listOptions(
"Program Options",
m_options,
"@c --");
511 if (!nonOptions.empty())
513 listOptions(
"Program Arguments", nonOptions,
"@c ");
516 os <<
"*/" << std::endl;
528 os <<
"Global values:" << std::endl;
531 std::vector<std::string> globals;
535 std::stringstream ss;
536 ss <<
" --" << (*i)->GetName() <<
"=[";
540 ss <<
v.Get() <<
"]" << std::endl;
541 ss <<
" " << (*i)->GetHelp() << std::endl;
542 globals.push_back(ss.str());
544 std::sort(globals.begin(), globals.end());
545 for (
const auto& s : globals)
560 os << header.str() <<
"\n";
562 std::vector<std::string> attributes;
566 std::stringstream ss;
570 <<
" " << info.
help <<
"\n";
571 attributes.push_back(ss.str());
573 std::sort(attributes.begin(), attributes.end());
574 for (
const auto& s : attributes)
588 NS_FATAL_ERROR(
"Unknown type=" << type <<
" in --PrintAttributes");
591 std::stringstream header;
592 header <<
"Attributes for TypeId " << tid.
GetName();
602 header <<
"Attributes defined in parent class " << tmp.
GetName();
615 os <<
"TypeIds in group " << group <<
":" << std::endl;
618 std::vector<std::string> groupTypes;
622 std::stringstream ss;
626 ss <<
" " << tid.
GetName() << std::endl;
628 groupTypes.push_back(ss.str());
630 std::sort(groupTypes.begin(), groupTypes.end());
631 for (
const auto& s : groupTypes)
641 os <<
"Registered TypeIds:" << std::endl;
644 std::vector<std::string> types;
648 std::stringstream ss;
650 ss <<
" " << tid.
GetName() << std::endl;
651 types.push_back(ss.str());
653 std::sort(types.begin(), types.end());
654 for (
const auto& s : types)
665 std::set<std::string> groups;
672 os <<
"Registered TypeId groups:" << std::endl;
674 for (
const auto& s : groups)
676 os <<
" " << s << std::endl;
685 NS_LOG_DEBUG(
"Handle arg name=" << name <<
" value=" << value);
687 auto errorExit = [
this, name, value]() {
688 std::cerr <<
"Invalid command-line argument: --" << name;
691 std::cerr <<
"=" << value;
693 std::cerr << std::endl;
698 auto item = std::find_if(
m_options.begin(),
m_options.end(), [name](std::shared_ptr<Item> it) {
699 return it->m_name == name;
703 if (!(*item)->Parse(value))
734 NS_LOG_DEBUG(
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
740 const std::string& help,
745 auto item = std::make_shared<CharStarItem>();
748 item->m_buffer = value;
750 item->m_default.assign(value);
756 const std::string& help,
758 const std::string& defaultValue )
762 auto item = std::make_shared<CallbackItem>();
765 item->m_callback = callback;
766 item->m_default = defaultValue;
775 std::size_t colon = attributePath.rfind(
"::");
776 const std::string typeName = attributePath.substr(0, colon);
777 NS_LOG_DEBUG(
"typeName: '" << typeName <<
"', colon: " << colon);
785 const std::string attrName = attributePath.substr(colon + 2);
792 std::stringstream ss;
793 ss << info.
help <<
" (" << attributePath <<
") ["
864 if (value.size() >
m_size - 1)
866 std::cerr <<
"Value \"" << value <<
"\" (" << value.size() <<
" bytes) is too long for "
867 <<
m_name <<
" buffer (" <<
m_size <<
" bytes, including terminating null)."
893 std::istringstream iss(defaultValue);
895 std::ostringstream oss;
896 oss << std::boolalpha << value;
911 std::string src = value;
912 std::transform(src.begin(), src.end(), src.begin(), [](
char c) {
913 return static_cast<char>(std::tolower(c));
915 if (src ==
"true" || src ==
"t")
920 else if (src ==
"false" || src ==
"f")
927 std::istringstream iss;
930 return !iss.bad() && !iss.fail();
938 std::ostringstream oss;
939 oss <<
Time(defaultValue).
As();
947 uint8_t oldDest = dest;
952 newDest = std::stoi(value);
954 catch (std::invalid_argument& ia)
960 catch (std::out_of_range& oor)
966 if (newDest < 0 || newDest > 255)
bool HasDefault() const override
std::string GetDefault() const override
bool Parse(const std::string &value) const override
Parse from a string.
std::string m_default
The default value, as a string, if it exists.
ns3::Callback< bool, const std::string & > m_callback
The Callback.
char * m_buffer
The buffer to write in to.
std::string m_default
The default value.
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
std::size_t m_size
The size of the buffer, including terminating null.
bool HasDefault() const override
virtual ~Item()
Destructor.
virtual bool HasDefault() const
std::string m_name
Argument label: --m_name=...
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
std::string m_value
The argument value.
bool HasDefault() const override
Parse command-line arguments.
void PrintAttributeList(std::ostream &os, const TypeId tid, std::stringstream &header) const
Print the Attributes for a single type.
HasOptionName GetOptionName(const std::string ¶m) const
Strip leading -- or - from options.
std::tuple< bool, std::string, std::string > HasOptionName
Tuple type returned by GetOptionName().
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
std::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
std::size_t GetNExtraNonOptions() const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
std::vector< std::shared_ptr< Item > > Items
Argument list container.
void PrintDoxygenUsage() const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
~CommandLine()
Destructor.
std::string GetName() const
Get the program name.
Items m_options
The list of option arguments.
bool HandleNonOption(const std::string &value)
Handle a non-option.
void Parse(int argc, char *argv[])
Parse the program arguments.
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
void Copy(const CommandLine &cmd)
Copy constructor implementation.
std::size_t m_NNonOptions
The expected number of non-option arguments.
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Items m_nonOptions
The list of non-option arguments.
void PrintVersion(std::ostream &os) const
Print ns-3 version to the desired output stream.
void HandleHardOptions(const std::vector< std::string > &args) const
Handle hard-coded options.
std::string m_shortName
The source file name (without .cc), as would be given to ns3 run.
bool HandleOption(const std::string ¶m) const
Handle an option in the form param=value.
std::string m_usage
The Usage string.
void Clear()
Remove all arguments, Usage(), name.
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type as well as its parents.
bool HandleArgument(const std::string &name, const std::string &value) const
Match name against the program or general arguments, and dispatch to the appropriate handler.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
static bool HandleAttribute(const std::string &name, const std::string &value)
Callback function to handle attributes.
CommandLine()
Constructor.
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
std::string GetVersion() const
Get the program version.
CommandLine & operator=(const CommandLine &cmd)
Assignment.
void Usage(const std::string &usage)
Supply the program usage and documentation.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
static Iterator Begin()
The Begin iterator.
static Iterator End()
The End iterator.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
a unique identifier for an interface.
std::string GetGroupName() const
Get the group name.
static uint16_t GetRegisteredN()
Get the number of registered TypeIds.
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
std::size_t GetAttributeN() const
Get the number of attributes.
TypeId GetParent() const
Get the parent of this TypeId.
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
bool LookupAttributeByName(std::string name, AttributeInformation *info, bool permissive=false) const
Find an Attribute by name, retrieving the associated AttributeInformation.
std::string GetName() const
Get the name.
static std::string LongVersion()
Constructs a string containing all of the build details.
ns3::CommandLine declaration.
Declaration of the various ns3::Config functions and classes.
ns3::DesMetrics declaration.
Class Environment declaration.
ns3::GlobalValue declaration.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
std::string GetDefault< Time >(const std::string &defaultValue)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
std::string GetDefault< bool >(const std::string &defaultValue)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
bool SetDefaultFailSafe(std::string fullName, const AttributeValue &value)
bool SetGlobalFailSafe(std::string name, const AttributeValue &value)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
std::string Append(std::string left, std::string right)
Join two file system path elements.
std::string Encode(const std::string &source)
HTML-encode a string, for PrintDoxygenUsage().
bool UserItemParse< bool >(const std::string &value, bool &dest)
Specialization of CommandLine::UserItem::Parse() to bool.
bool UserItemParse< uint8_t >(const std::string &value, uint8_t &dest)
Specialization of CommandLine::UserItem::Parse() to uint8_t to distinguish from char.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
ns3::StringValue attribute value declarations.
ns3::SystemPath declarations.
ns3::TypeId declaration; inline and template implementations.
class ns3::Version definition