src/core/command-line.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 02 Jun 2008 10:30:24 -0700
changeset 3190 51fe9001a679
parent 2915 3573d91994cc
child 3929 909b0a724ed3
permissions -rw-r--r--
add some doxygen, remove a couple of XXX
     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 namespace ns3 {
    28 
    29 /**
    30  * \brief parse command-line arguments
    31  * \ingroup core
    32  *
    33  * Instances of this class can be used to parse command-line 
    34  * arguments: users can register new arguments with
    35  * CommandLine::AddValue but the most important functionality
    36  * provided by this class is that it can be used to set the
    37  * 'initial value' of every attribute in the system with the
    38  * --TypeIdName::AttributeName=value syntax and it can be used
    39  * to set the value of every GlobalValue in the system with
    40  * the --GlobalValueName=value syntax.
    41  */
    42 class CommandLine
    43 {
    44 public:
    45   ~CommandLine ();
    46 
    47   /**
    48    * \param name the name of the user-supplied argument
    49    * \param help some help text used by --PrintHelp
    50    * \param value a reference to the variable where the
    51    *        value parsed will be stored (if no value
    52    *        is parsed, this variable is not modified).
    53    */
    54   template <typename T>
    55   void AddValue (const std::string &name,
    56 		 const std::string &help,
    57 		 T &value);
    58 
    59   /**
    60    * \param argc the 'argc' variable: number of arguments (including the
    61    *        main program name as first element).
    62    * \param argv the 'argv' variable: a null-terminated array of strings,
    63    *        each of which identifies a command-line argument.
    64    * 
    65    * Obviously, this method will parse the input command-line arguments and
    66    * will attempt to handle them all.
    67    */
    68   void Parse (int argc, char *argv[]) const;
    69 private:
    70   class Item 
    71   {
    72   public:
    73     std::string m_name;
    74     std::string m_help;
    75     virtual ~Item ();
    76     virtual bool Parse (std::string value) = 0;
    77   };
    78   template <typename T>
    79   class UserItem : public Item
    80   {
    81   public:
    82     virtual bool Parse (std::string value);
    83     T *m_valuePtr;
    84   };
    85   void HandleArgument (std::string name, std::string value) const;
    86   void PrintHelp (void) const;
    87   void PrintGlobals (void) const;
    88   void PrintAttributes (std::string type) const;
    89   void PrintGroup (std::string group) const;
    90   void PrintTypeIds (void) const;
    91   void PrintGroups (void) const;
    92 
    93   typedef std::list<Item *> Items;
    94   Items m_items;
    95 };
    96 
    97 } // namespace ns3
    98 
    99 namespace ns3 {
   100 
   101 template <typename T>
   102 void 
   103 CommandLine::AddValue (const std::string &name,
   104 		   const std::string &help,
   105 		   T &value)
   106 {
   107   UserItem<T> *item = new UserItem<T> ();
   108   item->m_name = name;
   109   item->m_help = help;
   110   item->m_valuePtr = &value;
   111   m_items.push_back (item);
   112 }
   113 
   114 template <typename T>
   115 bool 
   116 CommandLine::UserItem<T>::Parse (std::string value)
   117 {
   118   std::istringstream iss;
   119   iss.str (value);
   120   iss >> (*m_valuePtr);
   121   return !iss.bad () && !iss.fail ();
   122 }
   123 
   124 } // namespace ns3
   125 
   126 #endif /* COMMAND_LINE_H */