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 */
ns3::CommandLine::AddNonOption
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
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::CommandLine::m_shortName
std::string m_shortName
The source file name (without .cc), as would be given to ns3 --run
Definition: command-line.h:586
ns3::CommandLine::Item::Parse
virtual bool Parse(const std::string value)=0
Parse from a string.
ns3::CommandLine::CallbackItem::Parse
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.cc:681
ns3::Callback
Callback template class.
Definition: callback.h:1279
ns3::CommandLine::GetName
std::string GetName() const
Get the program name.
Definition: command-line.cc:128
ns3::CommandLine::StringItem::HasDefault
bool HasDefault(void) const
Definition: command-line.cc:795
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::CommandLine::CallbackItem::m_callback
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:497
ns3::CommandLine::UserItem
An argument Item assigning to POD.
Definition: command-line.h:453
ns3::CommandLine::AddValue
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:659
ns3::CommandLine::PrintGlobals
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Definition: command-line.cc:444
ns3::CommandLine::UserItem::GetDefault
std::string GetDefault() const
Definition: command-line.h:703
ns3::CommandLineHelper::UserItemParse< bool >
bool UserItemParse< bool >(const std::string value, bool &val)
Specialization of CommandLine::UserItem to bool.
Definition: command-line.cc:817
ns3::CommandLine::operator=
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:77
ns3::CommandLine::PrintGroups
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
Definition: command-line.cc:566
ns3::CommandLine::StringItem::m_value
std::string m_value
The argument value.
Definition: command-line.h:476
ns3::CommandLine::PrintHelp
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
Definition: command-line.cc:258
callback.h
Declaration of the various callback functions.
ns3::CommandLine::m_nonOptionCount
std::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
Definition: command-line.h:584
ns3::CommandLine::PrintVersion
void PrintVersion(std::ostream &os) const
Print ns-3 version to the desired output stream.
Definition: command-line.cc:353
ns3::CommandLine::m_usage
std::string m_usage
The Usage string.
Definition: command-line.h:585
ns3::CommandLineHelper::GetDefault< bool >
std::string GetDefault< bool >(const bool &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
Definition: command-line.cc:808
ns3::CommandLine::Item::GetDefault
virtual std::string GetDefault() const =0
ns3::CommandLine::UserItem::HasDefault
bool HasDefault() const
Definition: command-line.h:696
ns3::CommandLine::m_options
Items m_options
The list of option arguments.
Definition: command-line.h:581
ns3::CommandLine::CallbackItem::m_default
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:498
ns3::CommandLine::UserItem::m_default
std::string m_default
String representation of default value.
Definition: command-line.h:461
ns3::CommandLine::GetNExtraNonOptions
std::size_t GetNExtraNonOptions(void) const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
Definition: command-line.cc:753
ns3::CommandLine::UserItem::Parse
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:719
ns3::CommandLine::Clear
void Clear(void)
Remove all arguments, Usage(), name.
Definition: command-line.cc:102
ns3::CommandLine::HandleNonOption
bool HandleNonOption(const std::string &value)
Handle a non-option.
Definition: command-line.cc:218
ns3::CommandLine::Item
The argument abstract base class.
Definition: command-line.h:425
ns3::CommandLine::CallbackItem::GetDefault
std::string GetDefault(void) const
Definition: command-line.cc:675
ns3::CommandLine::Usage
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:122
ns3::CommandLine::PrintAttributes
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type.
Definition: command-line.cc:476
ns3::CommandLine::UserItem::m_valuePtr
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:460
ns3::CommandLine::Item::m_help
std::string m_help
Argument help string.
Definition: command-line.h:428
ns3::CommandLine::GetExtraNonOption
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
Definition: command-line.cc:737
ns3::CommandLineHelper::UserItemParse< uint8_t >
bool UserItemParse< uint8_t >(const std::string value, uint8_t &val)
Specialization of CommandLine::UserItem to uint8_t to distinguish from char.
Definition: command-line.cc:857
ns3::CommandLine::Item::m_name
std::string m_name
Argument label: --m_name=...
Definition: command-line.h:427
ns3::CommandLine::CallbackItem
An argument Item using a Callback to parse the input.
Definition: command-line.h:484
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::CommandLineHelper::GetDefault
std::string GetDefault(const T &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
Definition: command-line.h:710
ns3::CommandLine::Copy
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:89
ns3::CommandLine::PrintGroup
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
Definition: command-line.cc:512
ns3::CommandLine::CommandLine
CommandLine(void)
Constructor.
Definition: command-line.cc:54
ns3::CommandLine::Items
std::vector< Item * > Items
Argument list container.
Definition: command-line.h:580
second.cmd
cmd
Definition: second.py:35
ns3::CommandLine::Item::~Item
virtual ~Item()
Destructor.
Definition: command-line.cc:133
ns3::CommandLine::PrintTypeIds
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
Definition: command-line.cc:541
ns3::CommandLine::~CommandLine
~CommandLine()
Destructor.
Definition: command-line.cc:83
ns3::CommandLine::HandleAttribute
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
Definition: command-line.cc:768
ns3::CommandLine::CallbackItem::HasDefault
bool HasDefault(void) const
Definition: command-line.cc:669
ns3::CommandLineHelper::GetDefault< Time >
std::string GetDefault< Time >(const Time &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
Definition: command-line.cc:848
ns3::CommandLine::StringItem::GetDefault
std::string GetDefault(void) const
Definition: command-line.cc:801
ns3::CommandLine::Parse
void Parse(int argc, char *argv[])
Parse the program arguments.
Definition: command-line.cc:250
ns3::CommandLine::Item::HasDefault
virtual bool HasDefault() const
Definition: command-line.cc:782
ns3::CommandLine::m_NNonOptions
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:583
ns3::CommandLine::HandleArgument
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.
Definition: command-line.cc:588
ns3::CommandLine::m_nonOptions
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:582
ns3::CommandLine::StringItem::Parse
bool Parse(const std::string value)
Parse from a string.
Definition: command-line.cc:788
ns3::CommandLineHelper::UserItemParse
bool UserItemParse(const std::string value, T &val)
Helpers to specialize CommandLine::UserItem::Parse()
Definition: command-line.h:726
nstime.h
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::CommandLine::GetVersion
std::string GetVersion() const
Get the program version.
Definition: command-line.cc:342
ns3::CommandLine::HandleOption
bool HandleOption(const std::string &param) const
Handle an option in the form param=value.
Definition: command-line.cc:177
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
ns3::CommandLine::StringItem
Extension of Item for strings.
Definition: command-line.h:469
ns3::CommandLine::PrintDoxygenUsage
void PrintDoxygenUsage(void) const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
Definition: command-line.cc:359