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 <vector>
26 
27 #include "callback.h"
28 
35 namespace ns3 {
36 
213 {
214 public:
216  CommandLine ();
222  CommandLine (const CommandLine &cmd);
231  ~CommandLine ();
232 
238  void Usage (const std::string usage);
239 
249  template <typename T>
250  void AddValue (const std::string &name,
251  const std::string &help,
252  T &value);
253 
260  typedef bool (* Callback) (const std::string value);
261 
273  void AddValue (const std::string &name,
274  const std::string &help,
276 
283  void AddValue (const std::string &name,
284  const std::string &attributePath);
285 
295  template <typename T>
296  void AddNonOption (const std::string name, const std::string help, T & value);
297 
308  std::string GetExtraNonOption (std::size_t i) const;
309 
319  std::size_t GetNExtraNonOptions (void) const;
320 
335  void Parse (int argc, char *argv[]);
336 
346  void Parse (std::vector<std::string> args);
347 
353  std::string GetName () const;
354 
371  void PrintHelp (std::ostream &os) const;
372 
373 private:
374 
379  class Item
380  {
381  public:
382  std::string m_name;
383  std::string m_help;
384  virtual ~Item ();
391  virtual bool Parse (const std::string value) = 0;
395  virtual bool HasDefault () const;
399  virtual std::string GetDefault () const;
400  }; // class Item
401 
406  template <typename T>
407  class UserItem : public Item
408  {
409  public:
410  // Inherited
411  virtual bool Parse (const std::string value);
412  bool HasDefault () const;
413  std::string GetDefault () const;
414 
416  std::string m_default;
417  }; // class UserItem
418 
419  class StringItem : public Item
420  {
421  public:
422  // Inherited
423  bool Parse (const std::string value);
424  bool HasDefault (void) const;
425  std::string GetDefault (void) const;
426 
427  std::string m_value;
428  }; // class StringItem
429 
434  class CallbackItem : public Item
435  {
436  public:
443  virtual bool Parse (const std::string value);
445  }; // class CallbackItem
446 
447 
454  bool HandleOption (const std::string & param) const;
455 
462  bool HandleNonOption (const std::string &value);
463 
471  void HandleArgument (const std::string &name, const std::string &value) const;
479  static bool HandleAttribute (const std::string name, const std::string value);
480 
485  void PrintGlobals (std::ostream &os) const;
492  void PrintAttributes (std::ostream &os, const std::string &type) const;
499  void PrintGroup (std::ostream &os, const std::string &group) const;
505  void PrintTypeIds (std::ostream &os) const;
511  void PrintGroups (std::ostream &os) const;
517  void Copy (const CommandLine &cmd);
519  void Clear (void);
520 
521  typedef std::vector<Item *> Items;
524  std::size_t m_NNonOptions;
525  std::size_t m_nonOptionCount;
526  std::string m_usage;
527  std::string m_name;
529 }; // class CommandLine
530 
531 
539 namespace CommandLineHelper {
540 
550 template <typename T>
551 bool UserItemParse (const std::string value, T & val);
552 template <>
553 bool UserItemParse<bool> (const std::string value, bool & val);
564 template <typename T>
565 std::string GetDefault (const T & val);
566 template <>
567 std::string GetDefault<bool> (const bool & val);
570 } // namespace CommandLineHelper
571 
572 
573 
574 } // namespace ns3
575 
576 
577 /********************************************************************
578  * Implementation of the templates declared above.
579  ********************************************************************/
580 
581 namespace ns3 {
582 
583 template <typename T>
584 void
585 CommandLine::AddValue (const std::string &name,
586  const std::string &help,
587  T &value)
588 {
589  UserItem<T> *item = new UserItem<T> ();
590  item->m_name = name;
591  item->m_help = help;
592  item->m_valuePtr = &value;
593 
594  std::stringstream ss;
595  ss << value;
596  ss >> item->m_default;
597 
598  m_options.push_back (item);
599 }
600 
601 template <typename T>
602 void
603 CommandLine::AddNonOption (const std::string name,
604  const std::string help,
605  T & value)
606 {
607  UserItem<T> *item = new UserItem<T> ();
608  item->m_name = name;
609  item->m_help = help;
610  item->m_valuePtr = &value;
611 
612  std::stringstream ss;
613  ss << value;
614  ss >> item->m_default;
615  m_nonOptions.push_back (item);
616  ++m_NNonOptions;
617 
618 }
619 
620 template <typename T>
621 bool
623 {
624  return true;
625 }
626 
627 template <typename T>
628 std::string
630 {
631  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
632 }
633 
634 template <typename T>
635 std::string
637 {
638  std::ostringstream oss;
639  oss << val;
640  return oss.str ();
641 }
642 
643 
644 template <typename T>
645 bool
646 CommandLine::UserItem<T>::Parse (const std::string value)
647 {
648  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
649 }
650 
651 template <typename T>
652 bool
653 CommandLineHelper::UserItemParse (const std::string value, T & val)
654 {
655  std::istringstream iss;
656  iss.str (value);
657  iss >> val;
658  return !iss.bad () && !iss.fail ();
659 }
660 
680 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
681 
682 } // namespace ns3
683 
684 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:65
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:415
Callback template class.
Definition: callback.h:1273
std::vector< Item * > Items
Argument list container.
Definition: command-line.h:521
std::string m_value
The argument value.
Definition: command-line.h:427
std::string m_default
String representation of default value.
Definition: command-line.h:416
std::string GetName() const
Get the program name.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:523
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:646
The argument abstract base class.
Definition: command-line.h:379
cmd
Definition: second.py:35
An argument Item assigning to POD.
Definition: command-line.h:407
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.
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type.
void Usage(const std::string usage)
Supply the program usage and documentation.
bool HandleNonOption(const std::string &value)
Handle a non-option.
Declaration of the various callback functions.
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:83
void AddNonOption(const std::string name, const std::string help, T &value)
Add a non-option argument, assigning to POD.
Definition: command-line.h:603
An argument Item using a Callback to parse the input.
Definition: command-line.h:434
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
virtual std::string GetDefault() const
Items m_options
The list of option arguments.
Definition: command-line.h:522
bool HandleOption(const std::string &param) const
Handle an option in the form param=value.
std::string m_usage
The Usage string.
Definition: command-line.h:526
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:444
std::string GetDefault() const
Definition: command-line.h:629
std::size_t GetNExtraNonOptions(void) const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
bool Parse(const std::string value)
Parse from a string.
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
std::string m_name
The program name.
Definition: command-line.h:527
Parse command-line arguments.
Definition: command-line.h:212
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...
std::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
Definition: command-line.h:525
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
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:636
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:59
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:382
bool UserItemParse< bool >(const std::string value, bool &val)
Helpers to specialize CommandLine::UserItem::Parse() on bool.
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:383
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:585
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:71
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:653
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:524
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
virtual bool HasDefault() const