[Bug 1653] Extension of CommandLine interface
authorPeter D. Barnes, Jr. <barnes26@llnl.gov>
Thu, 20 Feb 2014 22:00:09 -0800
changeset 10621 c23778df1349
parent 10620 30da3895c9b5
child 10622 9d057a27ede2
[Bug 1653] Extension of CommandLine interface Restore operator << (std::ostream, CommandLine) erroneously removed in rev 10155.
RELEASE_NOTES
src/core/model/command-line.cc
src/core/model/command-line.h
--- a/RELEASE_NOTES	Thu Feb 20 22:41:33 2014 +0100
+++ b/RELEASE_NOTES	Thu Feb 20 22:00:09 2014 -0800
@@ -31,6 +31,7 @@
 
 Bugs fixed
 ----------
+- Bug 1653 - Extension of CommandLine interface: restored operator << (CommandLine)
 - Bug 1739 - The endpoint is not deallocated for UDP sockets
 - Bug 1786 - os << int64x64_t prints un-normalized fractional values
 - Bug 1787 - Runtime error when using AnimationInterface::EnablePacketMetadata() to fetch metadata of CSMA packet
--- a/src/core/model/command-line.cc	Thu Feb 20 22:41:33 2014 +0100
+++ b/src/core/model/command-line.cc	Thu Feb 20 22:00:09 2014 -0800
@@ -457,4 +457,11 @@
     }
 }
 
+std::ostream &
+operator << (std::ostream & os, const CommandLine & cmd)
+{
+  cmd.PrintHelp (os);
+  return os;
+}
+
 } // namespace ns3
--- a/src/core/model/command-line.h	Thu Feb 20 22:41:33 2014 +0100
+++ b/src/core/model/command-line.h	Thu Feb 20 22:00:09 2014 -0800
@@ -91,7 +91,7 @@
  *   --SchedulerType=HeapScheduler
  * \endcode
  *
- * A simple example is in \c src/core/example/command-line-example.cc
+ * A simple example is in `src/core/example/ command-line-example.cc`
  * The heart of that example is this code:
  *
  * \code
@@ -110,7 +110,7 @@
  *  cmd.Parse (argc, argv);
  * \endcode
  * after which it prints the values of each variable.  (The \c SetCbArg function
- * is not shown.)
+ * is not shown here; see `src/core/example/ command-line-example.cc`)
  *
  * Here is the output from a few runs of that program:
  *
@@ -148,6 +148,31 @@
  *       --PrintAttributes=[typeid]:  Print all attributes of typeid.
  *       --PrintHelp:                 Print this help message.
  * \endcode
+ *
+ * Having parsed the arguments, some programs will need to perform
+ * some additional validation of the received values.  A common issue at this
+ * point is to discover that the supplied arguments are incomplete or
+ * incompatible.  Suggested best practice is to supply an error message
+ * and the complete usage message.  For example,
+ *
+ * \code
+ *   int value1;
+ *   int value2;
+ *   
+ *   CommandLine cmd;
+ *   cmd.Usage ("...");
+ *   cmd.AddValue ("value1", "first value", value1);
+ *   cmd.AddValue ("value2", "second value", value1);
+ *
+ *   cmd.Parse (argc, argv);
+ *
+ *   if (value1 * value2 < 0)
+ *     {
+ *       std::cerr << "value1 and value2 must have the same sign!" << std::endl;
+ *       std::cerr << cmd;
+ *       exit (-1);
+ *     }
+ * \endcode
  */
 class CommandLine
 {
@@ -463,6 +488,23 @@
   return !iss.bad () && !iss.fail ();
 }
 
+/**
+ * Overloaded operator << to print program usage
+ * (shortcut for CommandLine::PrintHelper)
+ *
+ * \see CommandLine::PrintHelper
+ *
+ * Example usage:
+ * \code
+ *    CommandLine cmd;
+ *    cmd.Parse (argc, argv);
+ *    ...
+ *    
+ *    std::cerr << cmd;
+ * \endcode
+ */
+std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
+
 } // namespace ns3
 
 #endif /* COMMAND_LINE_H */