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 
202 {
203 public:
205  CommandLine ();
211  CommandLine (const CommandLine &cmd);
218  CommandLine &operator = (const CommandLine &cmd);
220  ~CommandLine ();
221 
227  void Usage (const std::string usage);
228 
238  template <typename T>
239  void AddValue (const std::string &name,
240  const std::string &help,
241  T &value);
242 
254  void AddValue (const std::string &name,
255  const std::string &help,
256  Callback<bool, std::string> callback);
257 
264  void AddValue (const std::string &name,
265  const std::string &attributePath);
266 
281  void Parse (int argc, char *argv[]);
282 
288  std::string GetName () const;
289 
306  void PrintHelp (std::ostream &os) const;
307 
308 private:
309 
314  class Item
315  {
316  public:
317  std::string m_name;
318  std::string m_help;
319  virtual ~Item ();
326  virtual bool Parse (const std::string value) = 0;
330  virtual bool HasDefault () const;
334  virtual std::string GetDefault () const;
335  };
336 
341  template <typename T>
342  class UserItem : public Item
343  {
344  public:
351  virtual bool Parse (const std::string value);
352 
353  bool HasDefault () const;
354  std::string GetDefault () const;
355 
357  std::string m_default;
358  };
359 
364  class CallbackItem : public Item
365  {
366  public:
373  virtual bool Parse (const std::string value);
375  };
376 
384  void HandleArgument (const std::string &name, const std::string &value) const;
392  static bool HandleAttribute (const std::string name, const std::string value);
393 
395  void PrintGlobals (std::ostream &os) const;
402  void PrintAttributes (std::ostream &os, const std::string &type) const;
409  void PrintGroup (std::ostream &os, const std::string &group) const;
415  void PrintTypeIds (std::ostream &os) const;
421  void PrintGroups (std::ostream &os) const;
427  void Copy (const CommandLine &cmd);
429  void Clear (void);
430 
431  typedef std::list<Item *> Items;
432  Items m_items;
433  std::string m_usage;
434  std::string m_name;
435 }; // class CommandLine
436 
437 
445 namespace CommandLineHelper {
446 
456  template <typename T>
457  bool UserItemParse (const std::string value, T & val);
458  template <>
459  bool UserItemParse<bool> (const std::string value, bool & val);
470  template <typename T>
471  std::string GetDefault (const T & val);
472  template <>
473  std::string GetDefault<bool> (const bool & val);
476 } // namespace CommandLineHelper
477 
478 
479 
480 } // namespace ns3
481 
482 
483 /********************************************************************
484  * Implementation of the templates declared above.
485  ********************************************************************/
486 
487 namespace ns3 {
488 
489 template <typename T>
490 void
491 CommandLine::AddValue (const std::string &name,
492  const std::string &help,
493  T &value)
494 {
495  UserItem<T> *item = new UserItem<T> ();
496  item->m_name = name;
497  item->m_help = help;
498  item->m_valuePtr = &value;
499 
500  std::stringstream ss;
501  ss << value;
502  ss >> item->m_default;
503 
504  m_items.push_back (item);
505 }
506 
507 
508 template <typename T>
509 bool
511 {
512  return true;
513 }
514 
515 template <typename T>
516 std::string
518 {
519  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
520 }
521 
522 template <typename T>
523 std::string
525 {
526  std::ostringstream oss;
527  oss << val;
528  return oss.str ();
529 }
530 
531 
532 template <typename T>
533 bool
534 CommandLine::UserItem<T>::Parse (const std::string value)
535 {
536  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
537 }
538 
539 template <typename T>
540 bool
541 CommandLineHelper::UserItemParse (const std::string value, T & val)
542 {
543  std::istringstream iss;
544  iss.str (value);
545  iss >> val;
546  return !iss.bad () && !iss.fail ();
547 }
548 
568 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
569 
570 } // namespace ns3
571 
572 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:62
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:356
Items m_items
The list of arguments.
Definition: command-line.h:432
std::string m_default
String representation of default value.
Definition: command-line.h:357
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:534
The argument base class.
Definition: command-line.h:314
An argument Item assigning to POD.
Definition: command-line.h:342
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:431
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:95
Declaration of the various callback functions.
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:81
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:364
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:433
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:434
Parse command-line arguments.
Definition: command-line.h:201
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:524
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:56
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:317
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:374
virtual bool HasDefault() const
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:318
CommandLine()
Constructor.
Definition: command-line.cc:47
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
std::string GetDefault() const
Definition: command-line.h:517
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:68
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:541
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...