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 
178 {
179 public:
181  CommandLine ();
187  CommandLine (const CommandLine &cmd);
194  CommandLine &operator = (const CommandLine &cmd);
196  ~CommandLine ();
197 
203  void Usage (const std::string usage);
204 
214  template <typename T>
215  void AddValue (const std::string &name,
216  const std::string &help,
217  T &value);
218 
219 
231  void AddValue (const std::string &name,
232  const std::string &help,
233  Callback<bool, std::string> callback);
234 
249  void Parse (int argc, char *argv[]);
250 
256  std::string GetName () const;
257 
272  void PrintHelp (std::ostream &os) const;
273 
274 private:
275 
280  class Item
281  {
282  public:
283  std::string m_name;
284  std::string m_help;
285  virtual ~Item ();
292  virtual bool Parse (const std::string value) = 0;
296  virtual bool HasDefault () const;
300  virtual std::string GetDefault () const;
301  };
302 
307  template <typename T>
308  class UserItem : public Item
309  {
310  public:
317  virtual bool Parse (const std::string value);
318 
319  bool HasDefault () const;
320  std::string GetDefault () const;
321 
323  std::string m_default;
324  };
325 
330  class CallbackItem : public Item
331  {
332  public:
339  virtual bool Parse (const std::string value);
341  };
342 
350  void HandleArgument (const std::string &name, const std::string &value) const;
352  void PrintGlobals (std::ostream &os) const;
359  void PrintAttributes (std::ostream &os, const std::string &type) const;
366  void PrintGroup (std::ostream &os, const std::string &group) const;
368  void PrintTypeIds (std::ostream &os) const;
370  void PrintGroups (std::ostream &os) const;
376  void Copy (const CommandLine &cmd);
378  void Clear (void);
379 
380  typedef std::list<Item *> Items;
382  std::string m_usage;
383  std::string m_name;
384 }; // class CommandLine
385 
386 
394 namespace CommandLineHelper {
395 
405  template <typename T>
406  bool UserItemParse (const std::string value, T & val);
407  template <>
408  bool UserItemParse<bool> (const std::string value, bool & val);
419  template <typename T>
420  std::string GetDefault (const T & val);
421  template <>
422  std::string GetDefault<bool> (const bool & val);
425 } // namespace CommandLineHelper
426 
427 
428 
429 } // namespace ns3
430 
431 namespace ns3 {
432 
433 template <typename T>
434 void
435 CommandLine::AddValue (const std::string &name,
436  const std::string &help,
437  T &value)
438 {
439  UserItem<T> *item = new UserItem<T> ();
440  item->m_name = name;
441  item->m_help = help;
442  item->m_valuePtr = &value;
443 
444  std::stringstream ss;
445  ss << value;
446  ss >> item->m_default;
447 
448  m_items.push_back (item);
449 }
450 
451 
452 template <typename T>
453 bool
455 {
456  return true;
457 }
458 
459 template <typename T>
460 std::string
462 {
463  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
464 }
465 
466 template <typename T>
467 std::string
469 {
470  std::ostringstream oss;
471  oss << val;
472  return oss.str ();
473 }
474 
475 
476 template <typename T>
477 bool
478 CommandLine::UserItem<T>::Parse (const std::string value)
479 {
480  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
481 }
482 
483 template <typename T>
484 bool
485 CommandLineHelper::UserItemParse (const std::string value, T & val)
486 {
487  std::istringstream iss;
488  iss.str (value);
489  iss >> val;
490  return !iss.bad () && !iss.fail ();
491 }
492 
508 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
509 
510 } // namespace ns3
511 
512 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:54
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:322
Items m_items
The list of arguments.
Definition: command-line.h:381
std::string m_default
String representation of default value.
Definition: command-line.h:323
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:478
The argument base class.
Definition: command-line.h:280
An argument Item assigning to POD.
Definition: command-line.h:308
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:380
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:330
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:382
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:383
Parse command-line arguments.
Definition: command-line.h:177
virtual bool Parse(const std::string value)
Parse from a string.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
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:468
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:48
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:283
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:340
virtual bool HasDefault() const
virtual ~Item()
Destructor.
Definition: command-line.cc:98
std::string m_help
Argument help string.
Definition: command-line.h:284
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:435
std::string GetDefault() const
Definition: command-line.h:461
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:485
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...