30#if defined(ENABLE_BUILD_VERSION)
63 buffer.reserve(1.1 * source.size());
65 for (
size_t pos = 0; pos != source.size(); ++pos)
70 buffer.append(
"&");
73 buffer.append(
""");
76 buffer.append(
"'");
84 if (buffer.empty() || buffer.back() ==
' ')
86 buffer.append(
"<");
96 buffer.append(&source[pos], 1);
126 m_shortName = basename.substr(0, basename.rfind(
".cc"));
154 std::copy(
cmd.m_options.begin(),
cmd.m_options.end(),
m_options.end());
207 for (
const auto& param :
args)
220 "unexpected error parsing command line parameter: '" << param <<
"'");
224#ifdef ENABLE_DES_METRICS
233 std::string arg = param;
234 std::string::size_type cur = arg.find(
"--");
237 arg = arg.substr(2, arg.size() - 2);
244 arg = arg.substr(1, arg.size() - 1);
249 return {
false, param,
""};
257 if (cur == std::string::npos)
264 name = arg.substr(0, cur);
265 value = arg.substr(cur + 1, arg.size() - (cur + 1));
268 return {
true, name,
value};
276 for (
const auto& param :
args)
285 if (name ==
"PrintHelp" || name ==
"help")
291 if (name ==
"PrintVersion" || name ==
"version")
297 else if (name ==
"PrintGroups")
303 else if (name ==
"PrintTypeIds")
309 else if (name ==
"PrintGlobals")
315 else if (name ==
"PrintGroup")
321 else if (name ==
"PrintAttributes")
354 auto item = std::make_shared<StringItem>();
355 item->m_name =
"extra-non-option-argument";
356 item->m_help =
"Extra non-option argument encountered.";
357 item->m_value =
value;
362 if (!i->Parse(
value))
364 std::cerr <<
"Invalid non-option argument value " <<
value <<
" for " << i->m_name
377 std::vector<std::string>
args(argv, argv + argc);
389 << (nonOptions.size() ?
" [Program Arguments]" :
"") <<
" [General Arguments]" << std::endl;
397 std::size_t width = 0;
398 auto max_width = [&width](
const std::shared_ptr<Item> item) {
399 width =
std::max(width, item->m_name.size());
402 std::for_each(nonOptions.begin(), nonOptions.end(), max_width);
405 auto optionsHelp = [&os, width](
const std::string& head,
bool option,
const Items& items) {
406 os <<
"\n" << head <<
"\n";
407 for (
const auto& item : items)
409 os <<
" " << (option ?
"--" :
" ") << std::left << std::setw(width)
410 << (item->m_name +
":") << std::right << item->m_help;
412 if (item->HasDefault())
414 os <<
" [" << item->GetDefault() <<
"]";
422 optionsHelp(
"Program Options:",
true,
m_options);
425 if (!nonOptions.empty())
427 optionsHelp(
"Program Arguments:",
false, nonOptions);
431 os <<
"General Arguments:\n"
432 <<
" --PrintGlobals: Print the list of globals.\n"
433 <<
" --PrintGroups: Print the list of groups.\n"
434 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
435 <<
" --PrintTypeIds: Print all TypeIds.\n"
436 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
437 <<
" --PrintVersion: Print the ns-3 version.\n"
438 <<
" --PrintHelp: Print this help message.\n"
445#if defined(ENABLE_BUILD_VERSION)
448 return std::string{
"Build version support is not enabled, reconfigure with "
449 "--enable-build-version flag"};
464 const char* envVar = std::getenv(
"NS_COMMANDLINE_INTROSPECTION");
465 if (envVar ==
nullptr || std::strlen(envVar) == 0)
472 NS_FATAL_ERROR(
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
481 NS_LOG_INFO(
"Writing CommandLine doxy to " << outf);
483 std::fstream os(outf, std::fstream::out);
486 <<
"<h3>Usage</h3>\n"
488 << (nonOptions.size() ?
" [Program Arguments]" :
"") <<
"\"</code>\n";
495 auto listOptions = [&os](
const std::string& head,
const Items& items, std::string pre) {
496 os <<
"\n<h3>" << head <<
"</h3>\n<dl>\n";
497 for (
const auto& i : items)
499 os <<
" <dt>" << pre << i->m_name <<
" </dt>\n"
500 <<
" <dd>" <<
Encode(i->m_help);
504 os <<
" [" <<
Encode(i->GetDefault()) <<
"]";
513 listOptions(
"Program Options",
m_options,
"\\c --");
516 if (!nonOptions.empty())
518 listOptions(
"Program Arguments", nonOptions,
"\\c ");
521 os <<
"*/" << std::endl;
533 os <<
"Global values:" << std::endl;
536 std::vector<std::string> globals;
540 std::stringstream ss;
541 ss <<
" --" << (*i)->GetName() <<
"=[";
545 ss << v.
Get() <<
"]" << std::endl;
546 ss <<
" " << (*i)->GetHelp() << std::endl;
547 globals.push_back(ss.str());
549 std::sort(globals.begin(), globals.end());
550 for (
const auto& s : globals)
565 os << header.str() <<
"\n";
567 std::vector<std::string> attributes;
571 std::stringstream ss;
575 <<
" " << info.
help <<
"\n";
576 attributes.push_back(ss.str());
578 std::sort(attributes.begin(), attributes.end());
579 for (
const auto& s : attributes)
596 std::stringstream header;
597 header <<
"Attributes for TypeId " << tid.
GetName();
607 header <<
"Attributes defined in parent class " << tmp.
GetName();
620 os <<
"TypeIds in group " << group <<
":" << std::endl;
623 std::vector<std::string> groupTypes;
627 std::stringstream ss;
631 ss <<
" " << tid.
GetName() << std::endl;
633 groupTypes.push_back(ss.str());
635 std::sort(groupTypes.begin(), groupTypes.end());
636 for (
const auto& s : groupTypes)
646 os <<
"Registered TypeIds:" << std::endl;
649 std::vector<std::string> types;
653 std::stringstream ss;
655 ss <<
" " << tid.
GetName() << std::endl;
656 types.push_back(ss.str());
658 std::sort(types.begin(), types.end());
659 for (
const auto& s : types)
670 std::set<std::string> groups;
677 os <<
"Registered TypeId groups:" << std::endl;
679 for (
const auto& s : groups)
681 os <<
" " << s << std::endl;
692 auto errorExit = [
this, name,
value]() {
693 std::cerr <<
"Invalid command-line argument: --" << name;
696 std::cerr <<
"=" <<
value;
698 std::cerr << std::endl;
703 auto item = std::find_if(
m_options.begin(),
m_options.end(), [name](std::shared_ptr<Item> it) {
704 return it->m_name == name;
708 if (!(*item)->Parse(
value))
726 return m_default !=
"";
740 return m_callback(
value);
745 const std::string&
help,
750 auto item = std::make_shared<CharStarItem>();
753 item->m_buffer =
value;
755 item->m_default.assign(
value);
761 const std::string&
help,
763 const std::string& defaultValue )
767 auto item = std::make_shared<CallbackItem>();
770 item->m_callback = callback;
771 item->m_default = defaultValue;
780 std::size_t colon = attributePath.rfind(
"::");
781 const std::string typeName = attributePath.substr(0, colon);
782 NS_LOG_DEBUG(
"typeName: '" << typeName <<
"', colon: " << colon);
790 const std::string attrName = attributePath.substr(colon + 2);
797 std::stringstream ss;
798 ss << info.
help <<
" (" << attributePath <<
") ["
869 if (
value.size() > m_size - 1)
871 std::cerr <<
"Value \"" <<
value <<
"\" (" <<
value.size() <<
" bytes) is too long for "
872 << m_name <<
" buffer (" << m_size <<
" bytes, including terminating null)."
877 std::strncpy(m_buffer,
value.c_str(), m_size);
898 std::istringstream iss(defaultValue);
900 std::ostringstream oss;
901 oss << std::boolalpha <<
value;
916 std::string src =
value;
917 std::transform(src.begin(), src.end(), src.begin(), [](
char c) {
918 return static_cast<char>(std::tolower(c));
920 if (src ==
"true" || src ==
"t")
925 else if (src ==
"false" || src ==
"f")
932 std::istringstream iss;
935 return !iss.bad() && !iss.fail();
943 std::ostringstream oss;
944 oss <<
Time(defaultValue).As();
952 uint8_t oldDest = dest;
957 newDest = std::stoi(
value);
959 catch (std::invalid_argument& ia)
965 catch (std::out_of_range& oor)
971 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.
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
bool HasDefault() const override
virtual ~Item()
Destructor.
virtual bool HasDefault() const
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
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::vector< std::shared_ptr< Item > > Items
Argument list container.
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...
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.
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
static Iterator Begin()
The Begin iterator.
static Iterator End()
The End iterator.
Smart pointer class similar to boost::intrusive_ptr.
static DesMetrics * Get()
Get a pointer to the singleton instance.
Hold variables of type string.
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.
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information 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.
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
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.
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 SetGlobalFailSafe(std::string name, const AttributeValue &value)
bool SetDefaultFailSafe(std::string fullName, 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.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
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