31 #if defined(ENABLE_BUILD_VERSION)
64 buffer.reserve(1.1 * source.size());
66 for (
size_t pos = 0; pos != source.size(); ++pos)
71 buffer.append(
"&");
74 buffer.append(
""");
77 buffer.append(
"'");
85 if (buffer.empty() || buffer.back() ==
' ')
87 buffer.append(
"<");
97 buffer.append(&source[pos], 1);
127 m_shortName = basename.substr(0, basename.rfind(
".cc"));
155 std::copy(
cmd.m_options.begin(),
cmd.m_options.end(),
m_options.end());
208 for (
const auto& param :
args)
221 "unexpected error parsing command line parameter: '" << param <<
"'");
225 #ifdef ENABLE_DES_METRICS
234 std::string arg = param;
235 std::string::size_type cur = arg.find(
"--");
238 arg = arg.substr(2, arg.size() - 2);
245 arg = arg.substr(1, arg.size() - 1);
250 return {
false, param,
""};
258 if (cur == std::string::npos)
265 name = arg.substr(0, cur);
266 value = arg.substr(cur + 1, arg.size() - (cur + 1));
269 return {
true, name, value};
277 for (
const auto& param :
args)
286 if (name ==
"PrintHelp" || name ==
"help")
292 if (name ==
"PrintVersion" || name ==
"version")
298 else if (name ==
"PrintGroups")
304 else if (name ==
"PrintTypeIds")
310 else if (name ==
"PrintGlobals")
316 else if (name ==
"PrintGroup")
322 else if (name ==
"PrintAttributes")
355 auto item = std::make_shared<StringItem>();
356 item->m_name =
"extra-non-option-argument";
357 item->m_help =
"Extra non-option argument encountered.";
358 item->m_value = value;
363 if (!i->Parse(value))
365 std::cerr <<
"Invalid non-option argument value " << value <<
" for " << i->m_name
378 std::vector<std::string>
args(argv, argv + argc);
390 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
" [General Arguments]"
399 std::size_t width = 0;
400 auto max_width = [&width](
const std::shared_ptr<Item> item) {
401 width =
std::max(width, item->m_name.size());
404 std::for_each(nonOptions.begin(), nonOptions.end(), max_width);
407 auto optionsHelp = [&os, width](
const std::string& head,
bool option,
const Items& items) {
408 os <<
"\n" << head <<
"\n";
409 for (
const auto& item : items)
411 os <<
" " << (option ?
"--" :
" ") << std::left << std::setw(width)
412 << (item->m_name +
":") << std::right << item->m_help;
414 if (item->HasDefault())
416 os <<
" [" << item->GetDefault() <<
"]";
424 optionsHelp(
"Program Options:",
true,
m_options);
427 if (!nonOptions.empty())
429 optionsHelp(
"Program Arguments:",
false, nonOptions);
433 os <<
"General Arguments:\n"
434 <<
" --PrintGlobals: Print the list of globals.\n"
435 <<
" --PrintGroups: Print the list of groups.\n"
436 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
437 <<
" --PrintTypeIds: Print all TypeIds.\n"
438 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
439 <<
" --PrintVersion: Print the ns-3 version.\n"
440 <<
" --PrintHelp: Print this help message.\n"
447 #if defined(ENABLE_BUILD_VERSION)
450 return std::string{
"Build version support is not enabled, reconfigure with "
451 "--enable-build-version flag"};
474 NS_FATAL_ERROR(
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
483 NS_LOG_INFO(
"Writing CommandLine doxy to " << outf);
485 std::fstream os(outf, std::fstream::out);
488 <<
"<h3>Usage</h3>\n"
490 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
"\"</code>\n";
497 auto listOptions = [&os](
const std::string& head,
const Items& items, std::string pre) {
498 os <<
"\n<h3>" << head <<
"</h3>\n<dl>\n";
499 for (
const auto& i : items)
501 os <<
" <dt>" << pre << i->m_name <<
" </dt>\n"
502 <<
" <dd>" <<
Encode(i->m_help);
506 os <<
" [" <<
Encode(i->GetDefault()) <<
"]";
515 listOptions(
"Program Options",
m_options,
"\\c --");
518 if (!nonOptions.empty())
520 listOptions(
"Program Arguments", nonOptions,
"\\c ");
523 os <<
"*/" << std::endl;
535 os <<
"Global values:" << std::endl;
538 std::vector<std::string> globals;
542 std::stringstream ss;
543 ss <<
" --" << (*i)->GetName() <<
"=[";
547 ss << v.
Get() <<
"]" << std::endl;
548 ss <<
" " << (*i)->GetHelp() << std::endl;
549 globals.push_back(ss.str());
551 std::sort(globals.begin(), globals.end());
552 for (
const auto& s : globals)
567 os << header.str() <<
"\n";
569 std::vector<std::string> attributes;
573 std::stringstream ss;
577 <<
" " << info.
help <<
"\n";
578 attributes.push_back(ss.str());
580 std::sort(attributes.begin(), attributes.end());
581 for (
const auto& s : attributes)
598 std::stringstream header;
599 header <<
"Attributes for TypeId " << tid.
GetName();
609 header <<
"Attributes defined in parent class " << tmp.
GetName();
622 os <<
"TypeIds in group " << group <<
":" << std::endl;
625 std::vector<std::string> groupTypes;
629 std::stringstream ss;
633 ss <<
" " << tid.
GetName() << std::endl;
635 groupTypes.push_back(ss.str());
637 std::sort(groupTypes.begin(), groupTypes.end());
638 for (
const auto& s : groupTypes)
648 os <<
"Registered TypeIds:" << std::endl;
651 std::vector<std::string> types;
655 std::stringstream ss;
657 ss <<
" " << tid.
GetName() << std::endl;
658 types.push_back(ss.str());
660 std::sort(types.begin(), types.end());
661 for (
const auto& s : types)
672 std::set<std::string> groups;
679 os <<
"Registered TypeId groups:" << std::endl;
681 for (
const auto& s : groups)
683 os <<
" " << s << std::endl;
692 NS_LOG_DEBUG(
"Handle arg name=" << name <<
" value=" << value);
694 auto errorExit = [
this, name, value]() {
695 std::cerr <<
"Invalid command-line argument: --" << name;
698 std::cerr <<
"=" << value;
700 std::cerr << std::endl;
705 auto item = std::find_if(
m_options.begin(),
m_options.end(), [name](std::shared_ptr<Item> it) {
706 return it->m_name == name;
710 if (!(*item)->Parse(value))
728 return !m_default.empty();
741 NS_LOG_DEBUG(
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
742 return m_callback(value);
747 const std::string&
help,
752 auto item = std::make_shared<CharStarItem>();
755 item->m_buffer = value;
757 item->m_default.assign(value);
763 const std::string&
help,
765 const std::string& defaultValue )
769 auto item = std::make_shared<CallbackItem>();
772 item->m_callback = callback;
773 item->m_default = defaultValue;
782 std::size_t colon = attributePath.rfind(
"::");
783 const std::string typeName = attributePath.substr(0, colon);
784 NS_LOG_DEBUG(
"typeName: '" << typeName <<
"', colon: " << colon);
792 const std::string attrName = attributePath.substr(colon + 2);
799 std::stringstream ss;
800 ss << info.
help <<
" (" << attributePath <<
") ["
871 if (value.size() > m_size - 1)
873 std::cerr <<
"Value \"" << value <<
"\" (" << value.size() <<
" bytes) is too long for "
874 << m_name <<
" buffer (" << m_size <<
" bytes, including terminating null)."
879 std::strncpy(m_buffer, value.c_str(), m_size);
900 std::istringstream iss(defaultValue);
902 std::ostringstream oss;
903 oss << std::boolalpha << value;
918 std::string src = value;
919 std::transform(src.begin(), src.end(), src.begin(), [](
char c) {
920 return static_cast<char>(std::tolower(c));
922 if (src ==
"true" || src ==
"t")
927 else if (src ==
"false" || src ==
"f")
934 std::istringstream iss;
937 return !iss.bad() && !iss.fail();
945 std::ostringstream oss;
946 oss <<
Time(defaultValue).As();
954 uint8_t oldDest = dest;
959 newDest = std::stoi(value);
961 catch (std::invalid_argument& ia)
967 catch (std::out_of_range& oor)
973 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 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.
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.
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) 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 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