A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
command-line.h
Go to the documentation of this file.
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 
153 {
154 public:
156  CommandLine ();
162  CommandLine (const CommandLine &cmd);
169  CommandLine &operator = (const CommandLine &cmd);
171  ~CommandLine ();
172 
178  void Usage (const std::string usage);
179 
189  template <typename T>
190  void AddValue (const std::string &name,
191  const std::string &help,
192  T &value);
193 
194 
206  void AddValue (const std::string &name,
207  const std::string &help,
208  Callback<bool, std::string> callback);
209 
224  void Parse (int argc, char *argv[]);
225 
231  std::string GetName () const;
232 
247  void PrintHelp (std::ostream &os) const;
248 
249 private:
250 
255  class Item
256  {
257  public:
258  std::string m_name;
259  std::string m_help;
260  virtual ~Item ();
267  virtual bool Parse (const std::string value) = 0;
271  virtual bool HasDefault () const;
275  virtual std::string GetDefault () const;
276  };
277 
282  template <typename T>
283  class UserItem : public Item
284  {
285  public:
292  virtual bool Parse (const std::string value);
293 
294  bool HasDefault () const;
295  std::string GetDefault () const;
296 
298  std::string m_default;
299  };
300 
305  class CallbackItem : public Item
306  {
307  public:
314  virtual bool Parse (const std::string value);
316  };
317 
325  void HandleArgument (const std::string &name, const std::string &value) const;
327  void PrintGlobals (std::ostream &os) const;
333  void PrintAttributes (std::ostream &os, const std::string &type) const;
339  void PrintGroup (std::ostream &os, const std::string &group) const;
341  void PrintTypeIds (std::ostream &os) const;
343  void PrintGroups (std::ostream &os) const;
349  void Copy (const CommandLine &cmd);
351  void Clear (void);
352 
353  typedef std::list<Item *> Items;
355  std::string m_usage;
356  std::string m_name;
357 }; // class CommandLine
358 
359 
367 namespace CommandLineHelper {
368 
378  template <typename T>
379  bool UserItemParse (const std::string value, T & val);
380  template <>
381  bool UserItemParse<bool> (const std::string value, bool & val);
392  template <typename T>
393  std::string GetDefault (const T & val);
394  template <>
395  std::string GetDefault<bool> (const bool & val);
398 } // namespace CommandLineHelper
399 
400 
401 
402 } // namespace ns3
403 
404 namespace ns3 {
405 
406 template <typename T>
407 void
408 CommandLine::AddValue (const std::string &name,
409  const std::string &help,
410  T &value)
411 {
412  UserItem<T> *item = new UserItem<T> ();
413  item->m_name = name;
414  item->m_help = help;
415  item->m_valuePtr = &value;
416 
417  std::stringstream ss;
418  ss << value;
419  ss >> item->m_default;
420 
421  m_items.push_back (item);
422 }
423 
424 
425 template <typename T>
426 bool
428 {
429  return true;
430 }
431 
432 template <typename T>
433 std::string
435 {
436  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
437 }
438 
439 template <typename T>
440 std::string
442 {
443  std::ostringstream oss;
444  oss << val;
445  return oss.str ();
446 }
447 
448 
449 template <typename T>
450 bool
451 CommandLine::UserItem<T>::Parse (const std::string value)
452 {
453  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
454 }
455 
456 template <typename T>
457 bool
458 CommandLineHelper::UserItemParse (const std::string value, T & val)
459 {
460  std::istringstream iss;
461  iss.str (value);
462  iss >> val;
463  return !iss.bad () && !iss.fail ();
464 }
465 
466 } // namespace ns3
467 
468 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:54
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:297
Items m_items
The list of arguments.
Definition: command-line.h:354
std::string m_default
String representation of default value.
Definition: command-line.h:298
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:451
The argument base class.
Definition: command-line.h:255
An argument Item assigning to POD.
Definition: command-line.h:283
virtual bool Parse(const std::string value)=0
Parse from a string.
std::string GetDefault< bool >(const bool &val)
Helper to specialize UserItem::GetDefault on bool.
std::list< Item * > Items
Argument list container.
Definition: command-line.h:353
std::string GetName() const
Get the program name.
Definition: command-line.cc:93
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:87
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:73
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type.
An argument Item using a Callback to parse the input.
Definition: command-line.h:305
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
std::string m_usage
The Usage string.
Definition: command-line.h:355
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:356
Parse command-line arguments.
Definition: command-line.h:152
virtual bool Parse(const std::string value)
Parse from a string.
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
std::string GetDefault(const T &val)
Helper to specialize UserItem::GetDefault on bool.
Definition: command-line.h:441
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:48
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:258
bool UserItemParse< bool >(const std::string value, bool &val)
Helper to specialize UserItem::Parse on bool.
Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:315
virtual bool HasDefault() const
virtual ~Item()
Destructor.
Definition: command-line.cc:98
std::string m_help
Argument help string.
Definition: command-line.h:259
CommandLine()
Constructor.
Definition: command-line.cc:39
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
std::string GetDefault() const
Definition: command-line.h:434
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:60
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
void Parse(int argc, char *argv[])
Parse the program arguments.
bool UserItemParse(const std::string value, T &val)
Helper to specialize UserItem::Parse on bool.
Definition: command-line.h:458
void HandleArgument(const std::string &name, const std::string &value) const
Match name against the program or general arguments, and dispatch to the appropriate handler...