A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
command-line.h
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 "callback.h"
28 
29 namespace ns3 {
30 
51 {
52 public:
53  CommandLine ();
54  CommandLine (const CommandLine &cmd);
55  CommandLine &operator = (const CommandLine &cmd);
56  ~CommandLine ();
57 
65  template <typename T>
66  void AddValue (const std::string &name,
67  const std::string &help,
68  T &value);
69 
70 
77  void AddValue (const std::string &name,
78  const std::string &help,
80 
90  void Parse (int argc, char *argv[]) const;
91 private:
92  class Item
93  {
94 public:
95  std::string m_name;
96  std::string m_help;
97  virtual ~Item ();
98  virtual bool Parse (std::string value) = 0;
99  };
100  template <typename T>
101  class UserItem : public Item
102  {
103 public:
104  virtual bool Parse (std::string value);
105  T *m_valuePtr;
106  };
107  class CallbackItem : public Item
108  {
109 public:
110  virtual bool Parse (std::string value);
111  Callback<bool, std::string> m_callback;
112  };
113 
114  void HandleArgument (std::string name, std::string value) const;
115  void PrintHelp (void) const;
116  void PrintGlobals (void) const;
117  void PrintAttributes (std::string type) const;
118  void PrintGroup (std::string group) const;
119  void PrintTypeIds (void) const;
120  void PrintGroups (void) const;
121  void Copy (const CommandLine &cmd);
122  void Clear (void);
123 
124  typedef std::list<Item *> Items;
125  Items m_items;
126 };
127 
128 } // namespace ns3
129 
130 namespace ns3 {
131 
132 template <typename T>
133 void
134 CommandLine::AddValue (const std::string &name,
135  const std::string &help,
136  T &value)
137 {
138  UserItem<T> *item = new UserItem<T> ();
139  item->m_name = name;
140  item->m_help = help;
141  item->m_valuePtr = &value;
142  m_items.push_back (item);
143 }
144 
145 template <typename T>
146 bool
147 CommandLine::UserItem<T>::Parse (std::string value)
148 {
149  std::istringstream iss;
150  iss.str (value);
151  iss >> (*m_valuePtr);
152  return !iss.bad () && !iss.fail ();
153 }
154 
155 } // namespace ns3
156 
157 #endif /* COMMAND_LINE_H */
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84