31 #if defined (ENABLE_BUILD_VERSION)
64 buffer.reserve (1.1 * source.size ());
66 for(
size_t pos = 0; pos != source.size (); ++pos)
71 case '&': buffer.append (
"&");
break;
72 case '\"': buffer.append (
""");
break;
73 case '\'': buffer.append (
"'");
break;
80 if (buffer.back () ==
' ')
82 buffer.append (
"<");
92 default: buffer.append (&source[pos], 1);
break;
108 m_nonOptionCount (0),
116 m_nonOptionCount (0),
121 m_shortName = basename.substr (0, basename.rfind (
".cc"));
145 std::copy (
cmd.m_options.begin (),
cmd.m_options.end (),
m_options.end ());
199 if (args.size () > 0)
201 args.erase (args.begin ());
203 for (
const auto & param : args)
216 "unexpected error parsing command line parameter: '"
222 #ifdef ENABLE_DES_METRICS
232 std::string arg = param;
233 std::string::size_type cur = arg.find (
"--");
236 arg = arg.substr (2, arg.size () - 2);
240 cur = arg.find (
"-");
243 arg = arg.substr (1, arg.size () - 1);
252 cur = arg.find (
"=");
253 std::string name, value;
254 if (cur == std::string::npos)
261 name = arg.substr (0, cur);
262 value = arg.substr (cur + 1, arg.size () - (cur + 1));
280 item->
m_name =
"extra-non-option-argument";
281 item->
m_help =
"Extra non-option argument encountered.";
287 if (!i->Parse (value))
289 std::cerr <<
"Invalid non-option argument value "
290 << value <<
" for " << i->m_name
305 std::vector<std::string> args (argv, argv + argc);
318 << (
m_options.size () ?
" [Program Options]" :
"")
319 << (nonOptions.size () ?
" [Program Arguments]" :
"")
320 <<
" [General Arguments]"
329 std::size_t width = 0;
332 width =
std::max (width, it->m_name.size ());
334 for (
auto it : nonOptions)
336 width =
std::max (width, it->m_name.size ());
343 os <<
"Program Options:" << std::endl;
347 << std::left << std::setw (width) << ( i->m_name +
":")
351 if ( i->HasDefault ())
353 os <<
" [" << i->GetDefault () <<
"]";
359 if (!nonOptions.empty ())
363 os <<
"Program Arguments:" << std::endl;
364 for (
auto i : nonOptions)
367 << std::left << std::setw (width) << ( i->m_name +
":")
371 if ( i->HasDefault ())
373 os <<
" [" << i->GetDefault () <<
"]";
381 <<
"General Arguments:\n"
382 <<
" --PrintGlobals: Print the list of globals.\n"
383 <<
" --PrintGroups: Print the list of groups.\n"
384 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
385 <<
" --PrintTypeIds: Print all TypeIds.\n"
386 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
387 <<
" --PrintVersion: Print the ns-3 version.\n"
388 <<
" --PrintHelp: Print this help message.\n"
396 #if defined (ENABLE_BUILD_VERSION)
399 return std::string{
"Build version support is not enabled, reconfigure with "
400 "--enable-build-version flag"};
415 const char * envVar = std::getenv (
"NS_COMMANDLINE_INTROSPECTION");
416 if (envVar == 0 || std::strlen (envVar) == 0)
423 NS_FATAL_ERROR (
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
433 NS_LOG_INFO (
"Writing CommandLine doxy to " << outf);
435 std::fstream os (outf, std::fstream::out);
438 <<
"<h3>Usage</h3>\n"
440 << (
m_options.size () ?
" [Program Options]" :
"")
441 << (nonOptions.size () ?
" [Program Arguments]" :
"")
449 auto listOptions = [&os](
Items items, std::string pre)
452 for (
const auto i : items)
454 os <<
" <dt>" << pre << i->m_name <<
" </dt>\n"
455 <<
" <dd>" <<
Encode (i->m_help);
457 if ( i->HasDefault ())
459 os <<
" [" <<
Encode (i->GetDefault ()) <<
"]";
469 os <<
"<h3>Program Options</h3>\n";
473 if (!nonOptions.empty ())
476 os <<
"<h3>Program Arguments</h3>\n";
477 listOptions (nonOptions,
"\\c ");
480 os <<
"*/" << std::endl;
492 os <<
"Global values:" << std::endl;
495 std::vector<std::string> globals;
499 std::stringstream ss;
500 ss <<
" --" << (*i)->GetName () <<
"=[";
504 ss << v.
Get () <<
"]" << std::endl;
505 ss <<
" " << (*i)->GetHelp () << std::endl;
506 globals.push_back (ss.str ());
508 std::sort (globals.begin (), globals.end ());
509 for (
const auto & s : globals)
524 os << header.str() <<
"\n";
526 std::vector<std::string> attributes;
530 std::stringstream ss;
534 <<
" " << info.
help <<
"\n";
535 attributes.push_back (ss.str ());
537 std::sort (attributes.begin (), attributes.end ());
538 for (
const auto & s : attributes)
552 NS_FATAL_ERROR (
"Unknown type=" << type <<
" in --PrintAttributes");
555 std::stringstream header;
556 header <<
"Attributes for TypeId " << tid.
GetName ();
566 header <<
"Attributes defined in parent class " << tmp.
GetName ();
580 os <<
"TypeIds in group " << group <<
":" << std::endl;
583 std::vector<std::string> groupTypes;
587 std::stringstream ss;
591 ss <<
" " << tid.
GetName () << std::endl;
593 groupTypes.push_back (ss.str ());
595 std::sort (groupTypes.begin (), groupTypes.end ());
596 for (
const auto & s : groupTypes)
606 os <<
"Registered TypeIds:" << std::endl;
609 std::vector<std::string> types;
613 std::stringstream ss;
615 ss <<
" " << tid.
GetName () << std::endl;
616 types.push_back (ss.str ());
618 std::sort (types.begin (), types.end ());
619 for (
const auto & s : types)
630 std::set<std::string> groups;
637 os <<
"Registered TypeId groups:" << std::endl;
639 for (
const auto & s : groups)
641 os <<
" " << s << std::endl;
650 NS_LOG_DEBUG (
"Handle arg name=" << name <<
" value=" << value);
653 if (name ==
"PrintHelp" || name ==
"help")
659 if (name ==
"PrintVersion" || name ==
"version")
665 else if (name ==
"PrintGroups")
671 else if (name ==
"PrintTypeIds")
677 else if (name ==
"PrintGlobals")
683 else if (name ==
"PrintGroup")
689 else if (name ==
"PrintAttributes")
699 if (i->m_name == name)
701 if (!i->Parse (value))
703 std::cerr <<
"Invalid argument value: "
704 << name <<
"=" << value << std::endl;
719 std::cerr <<
"Invalid command-line arguments: --"
720 << name <<
"=" << value << std::endl;
729 return m_default !=
"";
742 NS_LOG_DEBUG (
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
743 return m_callback (value);
748 const std::string &help,
750 std::string defaultValue )
764 const std::string &attributePath)
768 std::size_t colon = attributePath.rfind (
"::");
769 const std::string typeName = attributePath.substr (0, colon);
770 NS_LOG_DEBUG (
"typeName: '" << typeName <<
"', colon: " << colon);
778 const std::string attrName = attributePath.substr (colon + 2);
785 std::stringstream ss;
787 <<
" (" << attributePath <<
") ["
827 const std::string value)
868 std::ostringstream oss;
869 oss << std::boolalpha << val;
877 std::string src = value;
878 std::transform (src.begin (), src.end (), src.begin (),
879 [](
char c) {return static_cast<char> (std::tolower (c)); });
880 if (src.length () == 0)
885 else if ( (src ==
"true") || (src ==
"t") )
890 else if ( (src ==
"false") || (src ==
"f") )
897 std::istringstream iss;
900 return !iss.bad () && !iss.fail ();
908 std::ostringstream oss;
917 uint8_t oldVal = val;
922 newVal = std::stoi (value);
924 catch (std::invalid_argument & ia)
930 catch (std::out_of_range & oor)
936 if (newVal < 0 || newVal > 255)
An argument Item using a Callback to parse the input.
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
virtual bool Parse(const std::string value)
Parse from a string.
std::string m_default
The default value, as a string, if it exists.
bool HasDefault(void) const
virtual ~Item()
Destructor.
virtual bool HasDefault() const
std::string m_name
Argument label: --m_name=...
std::string m_help
Argument help string.
Extension of Item for strings.
bool Parse(const std::string value)
Parse from a string.
bool HasDefault(void) const
std::string m_value
The argument value.
std::string GetDefault(void) const
Parse command-line arguments.
void PrintAttributeList(std::ostream &os, const TypeId tid, std::stringstream &header) const
Print the Attributes for a single type.
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.
void HandleArgument(const std::string &name, const std::string &value) const
Match name against the program or general arguments, and dispatch to the appropriate handler.
std::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
~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.
std::vector< Item * > Items
Argument list container.
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.
std::size_t m_NNonOptions
The expected number of non-option arguments.
void Clear(void)
Remove all arguments, Usage(), name.
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.
std::string m_shortName
The source file name (without .cc), as would be given to ns3 run
void Usage(const std::string usage)
Supply the program usage and documentation.
bool HandleOption(const std::string ¶m) const
Handle an option in the form param=value.
std::string m_usage
The Usage string.
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.
CommandLine(void)
Constructor.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
std::size_t GetNExtraNonOptions(void) const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
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.
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
void PrintDoxygenUsage(void) const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
static Iterator End(void)
The End iterator.
static Iterator Begin(void)
The Begin iterator.
Smart pointer class similar to boost::intrusive_ptr.
static DesMetrics * Get(void)
Get a pointer to the singleton instance.
Hold variables of type string.
std::string Get(void) const
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const enum 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::size_t GetAttributeN(void) const
Get the number of attributes.
static uint16_t GetRegisteredN(void)
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::string GetGroupName(void) const
Get the group name.
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
TypeId GetParent(void) const
Get the parent of this TypeId.
std::string GetName(void) const
Get the name.
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.
static std::string LongVersion(void)
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 Time &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
std::string GetDefault< bool >(const bool &val)
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.
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
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 &val)
Specialization of CommandLine::UserItem to bool.
bool UserItemParse< uint8_t >(const std::string value, uint8_t &val)
Specialization of CommandLine::UserItem 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