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 
197 {
198 public:
200  CommandLine ();
206  CommandLine (const CommandLine &cmd);
213  CommandLine &operator = (const CommandLine &cmd);
215  ~CommandLine ();
216 
222  void Usage (const std::string usage);
223 
233  template <typename T>
234  void AddValue (const std::string &name,
235  const std::string &help,
236  T &value);
237 
249  void AddValue (const std::string &name,
250  const std::string &help,
251  Callback<bool, std::string> callback);
252 
259  void AddValue (const std::string &name,
260  const std::string &attributePath);
261 
276  void Parse (int argc, char *argv[]);
277 
283  std::string GetName () const;
284 
299  void PrintHelp (std::ostream &os) const;
300 
301 private:
302 
307  class Item
308  {
309  public:
310  std::string m_name;
311  std::string m_help;
312  virtual ~Item ();
319  virtual bool Parse (const std::string value) = 0;
323  virtual bool HasDefault () const;
327  virtual std::string GetDefault () const;
328  };
329 
334  template <typename T>
335  class UserItem : public Item
336  {
337  public:
344  virtual bool Parse (const std::string value);
345 
346  bool HasDefault () const;
347  std::string GetDefault () const;
348 
350  std::string m_default;
351  };
352 
357  class CallbackItem : public Item
358  {
359  public:
366  virtual bool Parse (const std::string value);
368  };
369 
377  void HandleArgument (const std::string &name, const std::string &value) const;
385  static bool HandleAttribute (const std::string name, const std::string value);
386 
388  void PrintGlobals (std::ostream &os) const;
395  void PrintAttributes (std::ostream &os, const std::string &type) const;
402  void PrintGroup (std::ostream &os, const std::string &group) const;
404  void PrintTypeIds (std::ostream &os) const;
406  void PrintGroups (std::ostream &os) const;
412  void Copy (const CommandLine &cmd);
414  void Clear (void);
415 
416  typedef std::list<Item *> Items;
418  std::string m_usage;
419  std::string m_name;
420 }; // class CommandLine
421 
422 
430 namespace CommandLineHelper {
431 
441  template <typename T>
442  bool UserItemParse (const std::string value, T & val);
443  template <>
444  bool UserItemParse<bool> (const std::string value, bool & val);
455  template <typename T>
456  std::string GetDefault (const T & val);
457  template <>
458  std::string GetDefault<bool> (const bool & val);
461 } // namespace CommandLineHelper
462 
463 
464 
465 } // namespace ns3
466 
467 namespace ns3 {
468 
469 template <typename T>
470 void
471 CommandLine::AddValue (const std::string &name,
472  const std::string &help,
473  T &value)
474 {
475  UserItem<T> *item = new UserItem<T> ();
476  item->m_name = name;
477  item->m_help = help;
478  item->m_valuePtr = &value;
479 
480  std::stringstream ss;
481  ss << value;
482  ss >> item->m_default;
483 
484  m_items.push_back (item);
485 }
486 
487 
488 template <typename T>
489 bool
491 {
492  return true;
493 }
494 
495 template <typename T>
496 std::string
498 {
499  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
500 }
501 
502 template <typename T>
503 std::string
505 {
506  std::ostringstream oss;
507  oss << val;
508  return oss.str ();
509 }
510 
511 
512 template <typename T>
513 bool
514 CommandLine::UserItem<T>::Parse (const std::string value)
515 {
516  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
517 }
518 
519 template <typename T>
520 bool
521 CommandLineHelper::UserItemParse (const std::string value, T & val)
522 {
523  std::istringstream iss;
524  iss.str (value);
525  iss >> val;
526  return !iss.bad () && !iss.fail ();
527 }
528 
544 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
545 
546 } // namespace ns3
547 
548 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:56
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:349
Items m_items
The list of arguments.
Definition: command-line.h:417
std::string m_default
String representation of default value.
Definition: command-line.h:350
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:514
The argument base class.
Definition: command-line.h:307
An argument Item assigning to POD.
Definition: command-line.h:335
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:416
std::string GetName() const
Get the program name.
Definition: command-line.cc:95
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:89
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:75
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:357
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:418
virtual std::string GetDefault() const
std::string m_name
The program name.
Definition: command-line.h:419
Parse command-line arguments.
Definition: command-line.h:196
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:504
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:50
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:310
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:367
virtual bool HasDefault() const
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:311
CommandLine()
Constructor.
Definition: command-line.cc:41
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:471
std::string GetDefault() const
Definition: command-line.h:497
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:62
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:521
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...