20#if defined(ENABLE_BUILD_VERSION)
53 buffer.reserve(1.1 * source.size());
55 for (
size_t pos = 0; pos != source.size(); ++pos)
60 buffer.append(
"&");
63 buffer.append(
""");
66 buffer.append(
"'");
74 if (buffer.empty() || buffer.back() ==
' ')
76 buffer.append(
"<");
86 buffer.append(&source[pos], 1);
116 m_shortName = basename.substr(0, basename.rfind(
".cc"));
144 std::copy(cmd.m_options.begin(), cmd.m_options.end(),
m_options.end());
145 std::copy(cmd.m_nonOptions.begin(), cmd.m_nonOptions.end(),
m_nonOptions.end());
193 args.erase(args.begin());
197 for (
const auto& param : args)
210 "unexpected error parsing command line parameter: '" << param <<
"'");
214#ifdef ENABLE_DES_METRICS
223 std::string arg = param;
224 std::string::size_type cur = arg.find(
"--");
227 arg = arg.substr(2, arg.size() - 2);
234 arg = arg.substr(1, arg.size() - 1);
239 return {
false, param,
""};
247 if (cur == std::string::npos)
254 name = arg.substr(0, cur);
255 value = arg.substr(cur + 1, arg.size() - (cur + 1));
258 return {
true, name, value};
266 for (
const auto& param : args)
275 if (name ==
"PrintHelp" || name ==
"help")
281 if (name ==
"PrintVersion" || name ==
"version")
287 else if (name ==
"PrintGroups")
293 else if (name ==
"PrintTypeIds")
299 else if (name ==
"PrintGlobals")
305 else if (name ==
"PrintGroup")
311 else if (name ==
"PrintAttributes")
344 auto item = std::make_shared<StringItem>();
345 item->m_name =
"extra-non-option-argument";
346 item->m_help =
"Extra non-option argument encountered.";
347 item->m_value = value;
352 if (!i->Parse(value))
354 std::cerr <<
"Invalid non-option argument value " << value <<
" for " << i->m_name
367 std::vector<std::string> args(argv, argv + argc);
379 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
" [General Arguments]"
388 std::size_t width = 0;
389 auto max_width = [&width](
const std::shared_ptr<Item> item) {
390 width = std::max(width, item->m_name.size());
393 std::for_each(nonOptions.begin(), nonOptions.end(), max_width);
396 auto optionsHelp = [&os, width](
const std::string& head,
bool option,
const Items& items) {
397 os <<
"\n" << head <<
"\n";
398 for (
const auto& item : items)
400 os <<
" " << (option ?
"--" :
" ") << std::left << std::setw(width)
401 << (item->m_name +
":") << std::right << item->m_help;
403 if (item->HasDefault())
405 os <<
" [" << item->GetDefault() <<
"]";
413 optionsHelp(
"Program Options:",
true,
m_options);
416 if (!nonOptions.empty())
418 optionsHelp(
"Program Arguments:",
false, nonOptions);
422 os <<
"General Arguments:\n"
423 <<
" --PrintGlobals: Print the list of globals.\n"
424 <<
" --PrintGroups: Print the list of groups.\n"
425 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
426 <<
" --PrintTypeIds: Print all TypeIds.\n"
427 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
428 <<
" --PrintVersion: Print the ns-3 version.\n"
429 <<
" --PrintHelp: Print this help message.\n"
436#if defined(ENABLE_BUILD_VERSION)
439 return std::string{
"Build version support is not enabled, reconfigure with "
440 "--enable-build-version flag"};
463 NS_FATAL_ERROR(
"No file name on example-to-run; forgot to use CommandLine var (__FILE__)?");
472 NS_LOG_INFO(
"Writing CommandLine doxy to " << outf);
474 std::fstream os(outf, std::fstream::out);
477 <<
"<h3>Usage</h3>\n"
479 << (!nonOptions.empty() ?
" [Program Arguments]" :
"") <<
"\"</code>\n";
486 auto listOptions = [&os](
const std::string& head,
const Items& items, std::string pre) {
487 os <<
"\n<h3>" << head <<
"</h3>\n<dl>\n";
488 for (
const auto& i : items)
490 os <<
" <dt>" << pre << i->m_name <<
" </dt>\n"
491 <<
" <dd>" << Encode(i->m_help);
495 os <<
" [" << Encode(i->GetDefault()) <<
"]";
504 listOptions(
"Program Options",
m_options,
"\\c --");
507 if (!nonOptions.empty())
509 listOptions(
"Program Arguments", nonOptions,
"\\c ");
512 os <<
"*/" << std::endl;
524 os <<
"Global values:" << std::endl;
527 std::vector<std::string> globals;
531 std::stringstream ss;
532 ss <<
" --" << (*i)->GetName() <<
"=[";
536 ss << v.
Get() <<
"]" << std::endl;
537 ss <<
" " << (*i)->GetHelp() << std::endl;
538 globals.push_back(ss.str());
540 std::sort(globals.begin(), globals.end());
541 for (
const auto& s : globals)
556 os << header.str() <<
"\n";
558 std::vector<std::string> attributes;
562 std::stringstream ss;
566 <<
" " << info.
help <<
"\n";
567 attributes.push_back(ss.str());
569 std::sort(attributes.begin(), attributes.end());
570 for (
const auto& s : attributes)
584 NS_FATAL_ERROR(
"Unknown type=" << type <<
" in --PrintAttributes");
587 std::stringstream header;
588 header <<
"Attributes for TypeId " << tid.
GetName();
598 header <<
"Attributes defined in parent class " << tmp.
GetName();
611 os <<
"TypeIds in group " << group <<
":" << std::endl;
614 std::vector<std::string> groupTypes;
618 std::stringstream ss;
622 ss <<
" " << tid.
GetName() << std::endl;
624 groupTypes.push_back(ss.str());
626 std::sort(groupTypes.begin(), groupTypes.end());
627 for (
const auto& s : groupTypes)
637 os <<
"Registered TypeIds:" << std::endl;
640 std::vector<std::string> types;
644 std::stringstream ss;
646 ss <<
" " << tid.
GetName() << std::endl;
647 types.push_back(ss.str());
649 std::sort(types.begin(), types.end());
650 for (
const auto& s : types)
661 std::set<std::string> groups;
668 os <<
"Registered TypeId groups:" << std::endl;
670 for (
const auto& s : groups)
672 os <<
" " << s << std::endl;
681 NS_LOG_DEBUG(
"Handle arg name=" << name <<
" value=" << value);
683 auto errorExit = [
this, name, value]() {
684 std::cerr <<
"Invalid command-line argument: --" << name;
687 std::cerr <<
"=" << value;
689 std::cerr << std::endl;
694 auto item = std::find_if(
m_options.begin(),
m_options.end(), [name](std::shared_ptr<Item> it) {
695 return it->m_name == name;
699 if (!(*item)->Parse(value))
717 return !m_default.empty();
730 NS_LOG_DEBUG(
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
731 return m_callback(value);
736 const std::string& help,
741 auto item = std::make_shared<CharStarItem>();
744 item->m_buffer = value;
746 item->m_default.assign(value);
752 const std::string& help,
754 const std::string& defaultValue )
758 auto item = std::make_shared<CallbackItem>();
761 item->m_callback = callback;
762 item->m_default = defaultValue;
771 std::size_t colon = attributePath.rfind(
"::");
772 const std::string typeName = attributePath.substr(0, colon);
773 NS_LOG_DEBUG(
"typeName: '" << typeName <<
"', colon: " << colon);
781 const std::string attrName = attributePath.substr(colon + 2);
788 std::stringstream ss;
789 ss << info.
help <<
" (" << attributePath <<
") ["
860 if (value.size() > m_size - 1)
862 std::cerr <<
"Value \"" << value <<
"\" (" << value.size() <<
" bytes) is too long for "
863 << m_name <<
" buffer (" << m_size <<
" bytes, including terminating null)."
868 std::strncpy(m_buffer, value.c_str(), m_size);
889 std::istringstream iss(defaultValue);
891 std::ostringstream oss;
892 oss << std::boolalpha << value;
907 std::string src = value;
908 std::transform(src.begin(), src.end(), src.begin(), [](
char c) {
909 return static_cast<char>(std::tolower(c));
911 if (src ==
"true" || src ==
"t")
916 else if (src ==
"false" || src ==
"f")
923 std::istringstream iss;
926 return !iss.bad() && !iss.fail();
934 std::ostringstream oss;
935 oss <<
Time(defaultValue).
As();
943 uint8_t oldDest = dest;
948 newDest = std::stoi(value);
950 catch (std::invalid_argument& ia)
956 catch (std::out_of_range& oor)
962 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::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.
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()
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 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.
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