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 #include "nstime.h"
29 
36 namespace ns3 {
37 
228 {
229 public:
231  CommandLine (void);
242  CommandLine (const std::string filename);
248  CommandLine (const CommandLine &cmd);
257  ~CommandLine ();
258 
264  void Usage (const std::string usage);
265 
275  template <typename T>
276  void AddValue (const std::string &name,
277  const std::string &help,
278  T &value);
279 
286  typedef bool (* Callback) (const std::string value);
287 
300  void AddValue (const std::string &name,
301  const std::string &help,
303  const std::string defaultValue = "");
304 
305 
312  void AddValue (const std::string &name,
313  const std::string &attributePath);
314 
324  template <typename T>
325  void AddNonOption (const std::string name, const std::string help, T & value);
326 
337  std::string GetExtraNonOption (std::size_t i) const;
338 
348  std::size_t GetNExtraNonOptions (void) const;
349 
364  void Parse (int argc, char *argv[]);
365 
375  void Parse (std::vector<std::string> args);
376 
382  std::string GetName () const;
383 
400  void PrintHelp (std::ostream &os) const;
401 
407  std::string GetVersion () const;
408 
416  void PrintVersion (std::ostream &os) const;
417 
418 private:
419 
424  class Item
425  {
426  public:
427  std::string m_name;
428  std::string m_help;
429  virtual ~Item ();
436  virtual bool Parse (const std::string value) = 0;
440  virtual bool HasDefault () const;
444  virtual std::string GetDefault () const = 0;
445  }; // class Item
446 
451  template <typename T>
452  class UserItem : public Item
453  {
454  public:
455  // Inherited
456  virtual bool Parse (const std::string value);
457  bool HasDefault () const;
458  std::string GetDefault () const;
459 
461  std::string m_default;
462  }; // class UserItem
463 
468  class StringItem : public Item
469  {
470  public:
471  // Inherited
472  bool Parse (const std::string value);
473  bool HasDefault (void) const;
474  std::string GetDefault (void) const;
475 
476  std::string m_value;
477  }; // class StringItem
478 
483  class CallbackItem : public Item
484  {
485  public:
486  // Inherited
487  bool HasDefault (void) const;
488  std::string GetDefault (void) const;
489 
496  virtual bool Parse (const std::string value);
498  std::string m_default;
499  }; // class CallbackItem
500 
501 
508  bool HandleOption (const std::string & param) const;
509 
516  bool HandleNonOption (const std::string &value);
517 
525  void HandleArgument (const std::string &name, const std::string &value) const;
533  static bool HandleAttribute (const std::string name, const std::string value);
534 
539  void PrintGlobals (std::ostream &os) const;
546  void PrintAttributes (std::ostream &os, const std::string &type) const;
553  void PrintGroup (std::ostream &os, const std::string &group) const;
559  void PrintTypeIds (std::ostream &os) const;
565  void PrintGroups (std::ostream &os) const;
571  void Copy (const CommandLine &cmd);
573  void Clear (void);
578  void PrintDoxygenUsage (void) const;
579 
580  typedef std::vector<Item *> Items;
583  std::size_t m_NNonOptions;
584  std::size_t m_nonOptionCount;
585  std::string m_usage;
586  std::string m_shortName;
588 }; // class CommandLine
589 
590 
598 namespace CommandLineHelper {
599 
609 template <typename T>
610 bool UserItemParse (const std::string value, T & val);
618 template <>
619 bool UserItemParse<bool> (const std::string value, bool & val);
628 template <>
629 bool UserItemParse<uint8_t> (const std::string value, uint8_t & val);
630 
640 template <typename T> std::string GetDefault (const T & val);
641 template <> std::string GetDefault<bool> (const bool & val);
642 template <> std::string GetDefault<Time> (const Time & val);
645 } // namespace CommandLineHelper
646 
647 
648 } // namespace ns3
649 
650 
651 /********************************************************************
652  * Implementation of the templates declared above.
653  ********************************************************************/
654 
655 namespace ns3 {
656 
657 template <typename T>
658 void
659 CommandLine::AddValue (const std::string &name,
660  const std::string &help,
661  T &value)
662 {
663  UserItem<T> *item = new UserItem<T> ();
664  item->m_name = name;
665  item->m_help = help;
666  item->m_valuePtr = &value;
667 
668  std::stringstream ss;
669  ss << value;
670  ss >> item->m_default;
671 
672  m_options.push_back (item);
673 }
674 
675 template <typename T>
676 void
677 CommandLine::AddNonOption (const std::string name,
678  const std::string help,
679  T & value)
680 {
681  UserItem<T> *item = new UserItem<T> ();
682  item->m_name = name;
683  item->m_help = help;
684  item->m_valuePtr = &value;
685 
686  std::stringstream ss;
687  ss << value;
688  ss >> item->m_default;
689  m_nonOptions.push_back (item);
690  ++m_NNonOptions;
691 
692 }
693 
694 template <typename T>
695 bool
697 {
698  return true;
699 }
700 
701 template <typename T>
702 std::string
704 {
705  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
706 }
707 
708 template <typename T>
709 std::string
711 {
712  std::ostringstream oss;
713  oss << val;
714  return oss.str ();
715 }
716 
717 template <typename T>
718 bool
719 CommandLine::UserItem<T>::Parse (const std::string value)
720 {
721  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
722 }
723 
724 template <typename T>
725 bool
726 CommandLineHelper::UserItemParse (const std::string value, T & val)
727 {
728  std::istringstream iss;
729  iss.str (value);
730  iss >> val;
731  return !iss.bad () && !iss.fail ();
732 }
733 
753 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
754 
755 } // namespace ns3
756 
757 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:83
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
bool UserItemParse< bool >(const std::string value, bool &val)
Specialization of CommandLine::UserItem to bool.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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:460
Callback template class.
Definition: callback.h:1278
void PrintDoxygenUsage(void) const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
std::vector< Item * > Items
Argument list container.
Definition: command-line.h:580
std::string m_value
The argument value.
Definition: command-line.h:476
std::string m_default
String representation of default value.
Definition: command-line.h:461
std::string GetName() const
Get the program name.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:582
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:719
The argument abstract base class.
Definition: command-line.h:424
cmd
Definition: second.py:35
An argument Item assigning to POD.
Definition: command-line.h:452
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 types needing special handling...
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
void PrintVersion(std::ostream &os) const
Print ns-3 version to the desired output stream.
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.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
virtual std::string GetDefault() const =0
void Clear(void)
Remove all arguments, Usage(), name.
void AddNonOption(const std::string name, const std::string help, T &value)
Add a non-option argument, assigning to POD.
Definition: command-line.h:677
An argument Item using a Callback to parse the input.
Definition: command-line.h:483
bool UserItemParse< uint8_t >(const std::string value, uint8_t &val)
Specialization of CommandLine::UserItem to uint8_t to distinguish from char.
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
Items m_options
The list of option arguments.
Definition: command-line.h:581
bool HandleOption(const std::string &param) const
Handle an option in the form param=value.
std::string m_shortName
The source file name (without .cc), as would be given to waf --run
Definition: command-line.h:586
std::string m_usage
The Usage string.
Definition: command-line.h:585
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:497
std::string GetDefault() const
Definition: command-line.h:703
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:498
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 GetDefault(void) const
std::string GetDefault< Time >(const Time &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling...
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes...
Parse command-line arguments.
Definition: command-line.h:227
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:584
virtual bool Parse(const std::string value)
Parse from a string.
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 types needing special handling...
Definition: command-line.h:710
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:77
std::string m_name
Argument label: --m_name=...
Definition: command-line.h:427
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
CommandLine(void)
Constructor.
Definition: command-line.cc:54
virtual ~Item()
Destructor.
std::string GetVersion() const
Get the program version.
std::string m_help
Argument help string.
Definition: command-line.h:428
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:659
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:89
void Parse(int argc, char *argv[])
Parse the program arguments.
bool UserItemParse(const std::string value, T &val)
Helpers to specialize CommandLine::UserItem::Parse()
Definition: command-line.h:726
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:583
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
virtual bool HasDefault() const
Extension of Item for strings.
Definition: command-line.h:468