diff --git a/src/core/model/command-line.cc b/src/core/model/command-line.cc index e56f66364..bd84cc91c 100644 --- a/src/core/model/command-line.cc +++ b/src/core/model/command-line.cc @@ -47,6 +47,58 @@ * ns3::CommandLine implementation. */ +/** CommandLine anonymous namespace. */ +namespace { +/** + * HTML-encode a string, for PrintDoxygenUsage(). + * Usage and help strings, which are intended for text-only display, + * can contain illegal characters for HTML. This function + * encodes '&', '\"', '\'', and '<'. + * \param [in] source The original string. + * \returns The HTML-encoded version. + */ +std::string +Encode (const std::string & source) +{ + std::string buffer; + buffer.reserve (1.1 * source.size ()); + + for(size_t pos = 0; pos != source.size (); ++pos) + { + /* *NS_CHECK_STYLE_OFF* */ + switch (source[pos]) + { + case '&': buffer.append ("&"); break; + case '\"': buffer.append ("""); break; + case '\'': buffer.append ("'"); break; + // case '>': buffer.append (">"); break; + + case '<': { + // Special case: + // "...blah Usage\n" << "$ ./ns3 --run \"" << m_shortName @@ -392,45 +443,38 @@ CommandLine::PrintDoxygenUsage (void) const if (m_usage.length ()) { - os << m_usage << std::endl; + os << Encode (m_usage) << "\n"; } + auto listOptions = [&os](Items items, std::string pre) + { + os << "
\n"; + for (const auto i : items) + { + os << "
" << pre << i->m_name << "
\n" + << "
" << Encode (i->m_help); + + if ( i->HasDefault ()) + { + os << " [" << Encode (i->GetDefault ()) << "]"; + } + os << "
\n"; + } + os << "
\n"; + }; + if (!m_options.empty ()) { os << std::endl; - os << "

Program Options

\n" - << "
\n"; - for (auto i : m_options) - { - os << "
\\c --" << i->m_name << "
\n" - << "
" << i->m_help; - - if ( i->HasDefault ()) - { - os << " [" << i->GetDefault () << "]"; - } - os << "
\n"; - } - os << "
\n"; + os << "

Program Options

\n"; + listOptions (m_options, "\\c --"); } if (!nonOptions.empty ()) { os << std::endl; - os << "

Program Arguments

\n" - << "
\n"; - for (auto i : nonOptions) - { - os << "
\\c " << i->m_name << "
\n" - << "
" << i->m_help; - - if ( i->HasDefault ()) - { - os << " [" << i->GetDefault () << "]"; - } - os << "
\n"; - } - os << "
\n"; + os << "

Program Arguments

\n"; + listOptions (nonOptions, "\\c "); } os << "*/" << std::endl; diff --git a/src/core/model/command-line.h b/src/core/model/command-line.h index c5cc0ec26..e24ae8f16 100644 --- a/src/core/model/command-line.h +++ b/src/core/model/command-line.h @@ -574,6 +574,7 @@ private: /** * Append usage message in Doxygen format to the file indicated * by the NS_COMMANDLINE_INTROSPECTION environment variable. + * This is typically only called once, by Parse(). */ void PrintDoxygenUsage (void) const; @@ -603,7 +604,7 @@ namespace CommandLineHelper { * * \param [in] value The argument name * \param [out] val The argument location - * \tparam \deduced T The type being specialized + * \tparam T \deduced The type being specialized * \return \c true if parsing was successful */ template @@ -695,7 +696,7 @@ template bool CommandLine::UserItem::HasDefault () const { - return true; + return (m_default.size () > 0); } template