A Discrete-Event Network Simulator
API
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 
35 namespace ns3 {
36 
206 {
207 public:
209  CommandLine ();
215  CommandLine (const CommandLine &cmd);
222  CommandLine &operator = (const CommandLine &cmd);
224  ~CommandLine ();
225 
231  void Usage (const std::string usage);
232 
242  template <typename T>
243  void AddValue (const std::string &name,
244  const std::string &help,
245  T &value);
246 
258  void AddValue (const std::string &name,
259  const std::string &help,
260  Callback<bool, std::string> callback);
261 
268  void AddValue (const std::string &name,
269  const std::string &attributePath);
270 
285  void Parse (int argc, char *argv[]);
286 
292  std::string GetName () const;
293 
310  void PrintHelp (std::ostream &os) const;
311 
312 private:
313 
318  class Item
319  {
320  public:
321  std::string m_name;
322  std::string m_help;
323  virtual ~Item ();
330  virtual bool Parse (const std::string value) = 0;
334  virtual bool HasDefault () const;
338  virtual std::string GetDefault () const;
339  };
340 
345  template <typename T>
346  class UserItem : public Item
347  {
348  public:
355  virtual bool Parse (const std::string value);
356 
357  bool HasDefault () const;
358  std::string GetDefault () const;
359 
361  std::string m_default;
362  };
363 
368  class CallbackItem : public Item
369  {
370  public:
377  virtual bool Parse (const std::string value);
379  };
380 
388  void HandleArgument (const std::string &name, const std::string &value) const;
396  static bool HandleAttribute (const std::string name, const std::string value);
397 
399  void PrintGlobals (std::ostream &os) const;
406  void PrintAttributes (std::ostream &os, const std::string &type) const;
413  void PrintGroup (std::ostream &os, const std::string &group) const;
419  void PrintTypeIds (std::ostream &os) const;
425  void PrintGroups (std::ostream &os) const;
431  void Copy (const CommandLine &cmd);
433  void Clear (void);
434 
435  typedef std::list<Item *> Items;
436  Items m_items;
437  std::string m_usage;
438  std::string m_name;
439 }; // class CommandLine
440 
441 
449 namespace CommandLineHelper {
450 
460  template <typename T>
461  bool UserItemParse (const std::string value, T & val);
462  template <>
463  bool UserItemParse<bool> (const std::string value, bool & val);
474  template <typename T>
475  std::string GetDefault (const T & val);
476  template <>
477  std::string GetDefault<bool> (const bool & val);
480 } // namespace CommandLineHelper
481 
482 
483 
484 } // namespace ns3
485 
486 
487 /********************************************************************
488  * Implementation of the templates declared above.
489  ********************************************************************/
490 
491 namespace ns3 {
492 
493 template <typename T>
494 void
495 CommandLine::AddValue (const std::string &name,
496  const std::string &help,
497  T &value)
498 {
499  UserItem<T> *item = new UserItem<T> ();
500  item->m_name = name;
501  item->m_help = help;
502  item->m_valuePtr = &value;
503 
504  std::stringstream ss;
505  ss << value;
506  ss >> item->m_default;
507 
508  m_items.push_back (item);
509 }
510 
511 
512 template <typename T>
513 bool
515 {
516  return true;
517 }
518 
519 template <typename T>
520 std::string
522 {
523  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
524 }
525 
526 template <typename T>
527 std::string
529 {
530  std::ostringstream oss;
531  oss << val;
532  return oss.str ();
533 }
534 
535 
536 template <typename T>
537 bool
538 CommandLine::UserItem<T>::Parse (const std::string value)
539 {
540  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
541 }
542 
543 template <typename T>
544 bool
545 CommandLineHelper::UserItemParse (const std::string value, T & val)
546 {
547  std::istringstream iss;
548  iss.str (value);
549  iss >> val;
550  return !iss.bad () && !iss.fail ();
551 }
552 
572 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
573 
574 } // namespace ns3
575 
576 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:63
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:360
Items m_items
The list of arguments.
Definition: command-line.h:436
std::string m_default
String representation of default value.
Definition: command-line.h:361
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:538
The argument base class.
Definition: command-line.h:318
An argument Item assigning to POD.
Definition: command-line.h:346
virtual bool Parse(const std::string value)=0
Parse from a string.
std::string GetDefault< bool >(const bool &val)
Helper to specialize CommandLine::UserItem::GetDefault() on bool.
std::list< Item * > Items
Argument list container.
Definition: command-line.h:435
tuple cmd
Definition: second.py:35
std::string GetName() const
Get the program name.
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:96
Declaration of the various callback functions.
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:82
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:368
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
std::string m_usage
The Usage string.
Definition: command-line.h:437
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:438
Parse command-line arguments.
Definition: command-line.h:205
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:42
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string GetDefault(const T &val)
Helper to specialize CommandLine::UserItem::GetDefault() on bool.
Definition: command-line.h:528
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:57
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:321
bool UserItemParse< bool >(const std::string value, bool &val)
Helpers to specialize CommandLine::UserItem::Parse() on bool.
Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:378
virtual bool HasDefault() const
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:322
CommandLine()
Constructor.
Definition: command-line.cc:48
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
std::string GetDefault() const
Definition: command-line.h:521
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:69
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)
Helpers to specialize CommandLine::UserItem::Parse() on bool.
Definition: command-line.h:545
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...