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 
402  void PrintGlobals (std::ostream &os) const;
409  void PrintAttributes (std::ostream &os, const std::string &type) const;
416  void PrintGroup (std::ostream &os, const std::string &group) const;
422  void PrintTypeIds (std::ostream &os) const;
428  void PrintGroups (std::ostream &os) const;
434  void Copy (const CommandLine &cmd);
436  void Clear (void);
437 
438  typedef std::list<Item *> Items;
439  Items m_items;
440  std::string m_usage;
441  std::string m_name;
442 }; // class CommandLine
443 
444 
452 namespace CommandLineHelper {
453 
463  template <typename T>
464  bool UserItemParse (const std::string value, T & val);
465  template <>
466  bool UserItemParse<bool> (const std::string value, bool & val);
477  template <typename T>
478  std::string GetDefault (const T & val);
479  template <>
480  std::string GetDefault<bool> (const bool & val);
483 } // namespace CommandLineHelper
484 
485 
486 
487 } // namespace ns3
488 
489 
490 /********************************************************************
491  * Implementation of the templates declared above.
492  ********************************************************************/
493 
494 namespace ns3 {
495 
496 template <typename T>
497 void
498 CommandLine::AddValue (const std::string &name,
499  const std::string &help,
500  T &value)
501 {
502  UserItem<T> *item = new UserItem<T> ();
503  item->m_name = name;
504  item->m_help = help;
505  item->m_valuePtr = &value;
506 
507  std::stringstream ss;
508  ss << value;
509  ss >> item->m_default;
510 
511  m_items.push_back (item);
512 }
513 
514 
515 template <typename T>
516 bool
518 {
519  return true;
520 }
521 
522 template <typename T>
523 std::string
525 {
526  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
527 }
528 
529 template <typename T>
530 std::string
532 {
533  std::ostringstream oss;
534  oss << val;
535  return oss.str ();
536 }
537 
538 
539 template <typename T>
540 bool
541 CommandLine::UserItem<T>::Parse (const std::string value)
542 {
543  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
544 }
545 
546 template <typename T>
547 bool
548 CommandLineHelper::UserItemParse (const std::string value, T & val)
549 {
550  std::istringstream iss;
551  iss.str (value);
552  iss >> val;
553  return !iss.bad () && !iss.fail ();
554 }
555 
575 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
576 
577 } // namespace ns3
578 
579 #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:439
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:541
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:438
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:440
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:441
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:531
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:498
std::string GetDefault() const
Definition: command-line.h:524
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:548
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...