31 #if defined (ENABLE_BUILD_VERSION)
69 m_shortName = basename.substr (0, basename.rfind (
".cc"));
93 std::copy (
cmd.m_options.begin (),
cmd.m_options.end (),
m_options.end ());
147 if (args.size () > 0)
149 args.erase (args.begin ());
151 for (
auto param : args)
164 "unexpected error parsing command line parameter: '"
170 #ifdef ENABLE_DES_METRICS
180 std::string arg = param;
181 std::string::size_type cur = arg.find (
"--");
184 arg = arg.substr (2, arg.size () - 2);
188 cur = arg.find (
"-");
191 arg = arg.substr (1, arg.size () - 1);
200 cur = arg.find (
"=");
201 std::string name, value;
202 if (cur == std::string::npos)
209 name = arg.substr (0, cur);
210 value = arg.substr (cur + 1, arg.size () - (cur + 1));
228 item->
m_name =
"extra-non-option-argument";
229 item->
m_help =
"Extra non-option argument encountered.";
235 if (!i->Parse (value))
237 std::cerr <<
"Invalid non-option argument value "
238 << value <<
" for " << i->m_name
253 std::vector<std::string> args (argv, argv + argc);
266 << (
m_options.size () ?
" [Program Options]" :
"")
267 << (nonOptions.size () ?
" [Program Arguments]" :
"")
268 <<
" [General Arguments]"
277 std::size_t width = 0;
280 width =
std::max (width, it->m_name.size ());
282 for (
auto it : nonOptions)
284 width =
std::max (width, it->m_name.size ());
291 os <<
"Program Options:" << std::endl;
295 << std::left << std::setw (width) << ( i->m_name +
":")
299 if ( i->HasDefault ())
301 os <<
" [" << i->GetDefault () <<
"]";
307 if (!nonOptions.empty ())
311 os <<
"Program Arguments:" << std::endl;
312 for (
auto i : nonOptions)
315 << std::left << std::setw (width) << ( i->m_name +
":")
319 if ( i->HasDefault ())
321 os <<
" [" << i->GetDefault () <<
"]";
329 <<
"General Arguments:\n"
330 <<
" --PrintGlobals: Print the list of globals.\n"
331 <<
" --PrintGroups: Print the list of groups.\n"
332 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
333 <<
" --PrintTypeIds: Print all TypeIds.\n"
334 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
335 <<
" --PrintVersion: Print the ns-3 version.\n"
336 <<
" --PrintHelp: Print this help message.\n"
344 #if defined (ENABLE_BUILD_VERSION)
347 return std::string{
"Build version support is not enabled, reconfigure with "
348 "--enable-build-version flag"};
363 const char * envVar = std::getenv (
"NS_COMMANDLINE_INTROSPECTION");
364 if (envVar == 0 || std::strlen (envVar) == 0)
371 NS_FATAL_ERROR (
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
381 NS_LOG_INFO (
"Writing CommandLine doxy to " << outf);
383 std::fstream os (outf, std::fstream::out);
387 <<
"<h3>Usage</h3>\n"
389 << (
m_options.size () ?
" [Program Options]" :
"")
390 << (nonOptions.size () ?
" [Program Arguments]" :
"")
401 os <<
"<h3>Program Options</h3>\n"
405 os <<
" <dt>\\c --" << i->m_name <<
" </dt>\n"
406 <<
" <dd>" << i->m_help;
408 if ( i->HasDefault ())
410 os <<
" [" << i->GetDefault () <<
"]";
417 if (!nonOptions.empty ())
420 os <<
"<h3>Program Arguments</h3>\n"
422 for (
auto i : nonOptions)
424 os <<
" <dt> \\c " << i->m_name <<
" </dt>\n"
425 <<
" <dd>" << i->m_help;
427 if ( i->HasDefault ())
429 os <<
" [" << i->GetDefault () <<
"]";
436 os <<
"*/" << std::endl;
448 os <<
"Global values:" << std::endl;
451 std::vector<std::string> globals;
457 std::stringstream ss;
458 ss <<
" --" << (*i)->GetName () <<
"=[";
462 ss << v.
Get () <<
"]" << std::endl;
463 ss <<
" " << (*i)->GetHelp () << std::endl;
464 globals.push_back (ss.str ());
466 std::sort (globals.begin (), globals.end ());
467 for (std::vector<std::string>::const_iterator it = globals.begin ();
483 NS_FATAL_ERROR (
"Unknown type=" << type <<
" in --PrintAttributes");
486 os <<
"Attributes for TypeId " << tid.
GetName () << std::endl;
489 std::vector<std::string> attributes;
493 std::stringstream ss;
498 ss <<
" " << info.
help << std::endl;
499 attributes.push_back (ss.str ());
501 std::sort (attributes.begin (), attributes.end ());
502 for (std::vector<std::string>::const_iterator it = attributes.begin ();
503 it < attributes.end ();
516 os <<
"TypeIds in group " << group <<
":" << std::endl;
519 std::vector<std::string> groupTypes;
523 std::stringstream ss;
527 ss <<
" " << tid.
GetName () << std::endl;
529 groupTypes.push_back (ss.str ());
531 std::sort (groupTypes.begin (), groupTypes.end ());
532 for (std::vector<std::string>::const_iterator it = groupTypes.begin ();
533 it < groupTypes.end ();
544 os <<
"Registered TypeIds:" << std::endl;
547 std::vector<std::string> types;
551 std::stringstream ss;
553 ss <<
" " << tid.
GetName () << std::endl;
554 types.push_back (ss.str ());
556 std::sort (types.begin (), types.end ());
557 for (std::vector<std::string>::const_iterator it = types.begin ();
570 std::set<std::string> groups;
577 os <<
"Registered TypeId groups:" << std::endl;
579 for (std::set<std::string>::const_iterator
k = groups.begin ();
583 os <<
" " << *
k << std::endl;
592 NS_LOG_DEBUG (
"Handle arg name=" << name <<
" value=" << value);
595 if (name ==
"PrintHelp" || name ==
"help")
601 if (name ==
"PrintVersion" || name ==
"version")
607 else if (name ==
"PrintGroups")
613 else if (name ==
"PrintTypeIds")
619 else if (name ==
"PrintGlobals")
625 else if (name ==
"PrintGroup")
631 else if (name ==
"PrintAttributes")
641 if (i->m_name == name)
643 if (!i->Parse (value))
645 std::cerr <<
"Invalid argument value: "
646 << name <<
"=" << value << std::endl;
661 std::cerr <<
"Invalid command-line arguments: --"
662 << name <<
"=" << value << std::endl;
671 return m_default !=
"";
684 NS_LOG_DEBUG (
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
685 return m_callback (value);
690 const std::string &help,
692 std::string defaultValue )
706 const std::string &attributePath)
710 std::size_t colon = attributePath.rfind (
"::");
711 const std::string typeName = attributePath.substr (0, colon);
712 NS_LOG_DEBUG (
"typeName: '" << typeName <<
"', colon: " << colon);
720 const std::string attrName = attributePath.substr (colon + 2);
727 std::stringstream ss;
729 <<
" (" << attributePath <<
") ["
769 const std::string value)
810 std::ostringstream oss;
811 oss << std::boolalpha << val;
819 std::string src = value;
820 std::transform (src.begin (), src.end (), src.begin (),
821 [](
char c) {return static_cast<char> (std::tolower (c)); });
822 if (src.length () == 0)
827 else if ( (src ==
"true") || (src ==
"t") )
832 else if ( (src ==
"false") || (src ==
"f") )
839 std::istringstream iss;
842 return !iss.bad () && !iss.fail ();
850 std::ostringstream oss;
859 uint8_t oldVal = val;
864 newVal = std::stoi (value);
866 catch (std::invalid_argument & ia)
872 catch (std::out_of_range & oor)
878 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 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 waf --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.
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.
Vector::const_iterator Iterator
Iterator type for the list of all global values.
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.
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.
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