src/core/command-line.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3190 51fe9001a679
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 INRIA
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  *
    18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 #ifndef COMMAND_LINE_H
    21 #define COMMAND_LINE_H
    22 
    23 #include <string>
    24 #include <sstream>
    25 #include <list>
    26 
    27 #include "ns3/callback.h"
    28 
    29 namespace ns3 {
    30 
    31 /**
    32  * \brief parse command-line arguments
    33  * \ingroup core
    34  *
    35  * Instances of this class can be used to parse command-line 
    36  * arguments: users can register new arguments with
    37  * CommandLine::AddValue but the most important functionality
    38  * provided by this class is that it can be used to set the
    39  * 'initial value' of every attribute in the system with the
    40  * --TypeIdName::AttributeName=value syntax and it can be used
    41  * to set the value of every GlobalValue in the system with
    42  * the --GlobalValueName=value syntax.
    43  */
    44 class CommandLine
    45 {
    46 public:
    47   ~CommandLine ();
    48 
    49   /**
    50    * \param name the name of the user-supplied argument
    51    * \param help some help text used by --PrintHelp
    52    * \param value a reference to the variable where the
    53    *        value parsed will be stored (if no value
    54    *        is parsed, this variable is not modified).
    55    */
    56   template <typename T>
    57   void AddValue (const std::string &name,
    58 		 const std::string &help,
    59 		 T &value);
    60 
    61 
    62   /**
    63    * \param name the name of the user-supplied argument
    64    * \param help some help text used by --PrintHelp
    65    * \param callback a callback function that will be invoked to parse
    66    * and collect the value.  This normally used by language bindings.
    67    */
    68   void AddValue (const std::string &name,
    69 		 const std::string &help,
    70                  Callback<bool, std::string> callback);
    71 
    72   /**
    73    * \param argc the 'argc' variable: number of arguments (including the
    74    *        main program name as first element).
    75    * \param argv the 'argv' variable: a null-terminated array of strings,
    76    *        each of which identifies a command-line argument.
    77    * 
    78    * Obviously, this method will parse the input command-line arguments and
    79    * will attempt to handle them all.
    80    */
    81   void Parse (int argc, char *argv[]) const;
    82 private:
    83   class Item 
    84   {
    85   public:
    86     std::string m_name;
    87     std::string m_help;
    88     virtual ~Item ();
    89     virtual bool Parse (std::string value) = 0;
    90   };
    91   template <typename T>
    92   class UserItem : public Item
    93   {
    94   public:
    95     virtual bool Parse (std::string value);
    96     T *m_valuePtr;
    97   };
    98   class CallbackItem : public Item
    99   {
   100   public:
   101     virtual bool Parse (std::string value);
   102     Callback<bool, std::string> m_callback;
   103   };
   104 
   105   void HandleArgument (std::string name, std::string value) const;
   106   void PrintHelp (void) const;
   107   void PrintGlobals (void) const;
   108   void PrintAttributes (std::string type) const;
   109   void PrintGroup (std::string group) const;
   110   void PrintTypeIds (void) const;
   111   void PrintGroups (void) const;
   112 
   113   typedef std::list<Item *> Items;
   114   Items m_items;
   115 };
   116 
   117 } // namespace ns3
   118 
   119 namespace ns3 {
   120 
   121 template <typename T>
   122 void 
   123 CommandLine::AddValue (const std::string &name,
   124 		   const std::string &help,
   125 		   T &value)
   126 {
   127   UserItem<T> *item = new UserItem<T> ();
   128   item->m_name = name;
   129   item->m_help = help;
   130   item->m_valuePtr = &value;
   131   m_items.push_back (item);
   132 }
   133 
   134 template <typename T>
   135 bool 
   136 CommandLine::UserItem<T>::Parse (std::string value)
   137 {
   138   std::istringstream iss;
   139   iss.str (value);
   140   iss >> (*m_valuePtr);
   141   return !iss.bad () && !iss.fail ();
   142 }
   143 
   144 } // namespace ns3
   145 
   146 #endif /* COMMAND_LINE_H */