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 
227 {
228 public:
230  CommandLine (void);
241  CommandLine (const std::string filename);
247  CommandLine (const CommandLine &cmd);
256  ~CommandLine ();
257 
263  void Usage (const std::string usage);
264 
274  template <typename T>
275  void AddValue (const std::string &name,
276  const std::string &help,
277  T &value);
278 
285  typedef bool (* Callback) (const std::string value);
286 
299  void AddValue (const std::string &name,
300  const std::string &help,
302  const std::string defaultValue = "");
303 
304 
311  void AddValue (const std::string &name,
312  const std::string &attributePath);
313 
323  template <typename T>
324  void AddNonOption (const std::string name, const std::string help, T & value);
325 
336  std::string GetExtraNonOption (std::size_t i) const;
337 
347  std::size_t GetNExtraNonOptions (void) const;
348 
363  void Parse (int argc, char *argv[]);
364 
374  void Parse (std::vector<std::string> args);
375 
381  std::string GetName () const;
382 
399  void PrintHelp (std::ostream &os) const;
400 
406  std::string GetVersion () const;
407 
415  void PrintVersion (std::ostream &os) const;
416 
417 private:
418 
423  class Item
424  {
425  public:
426  std::string m_name;
427  std::string m_help;
428  virtual ~Item ();
435  virtual bool Parse (const std::string value) = 0;
439  virtual bool HasDefault () const;
443  virtual std::string GetDefault () const;
444  }; // class Item
445 
450  template <typename T>
451  class UserItem : public Item
452  {
453  public:
454  // Inherited
455  virtual bool Parse (const std::string value);
456  bool HasDefault () const;
457  std::string GetDefault () const;
458 
460  std::string m_default;
461  }; // class UserItem
462 
467  class StringItem : public Item
468  {
469  public:
470  // Inherited
471  bool Parse (const std::string value);
472  bool HasDefault (void) const;
473  std::string GetDefault (void) const;
474 
475  std::string m_value;
476  }; // class StringItem
477 
482  class CallbackItem : public Item
483  {
484  public:
485  // Inherited
486  bool HasDefault (void) const;
487  std::string GetDefault (void) const;
488 
495  virtual bool Parse (const std::string value);
497  std::string m_default;
498  }; // class CallbackItem
499 
500 
507  bool HandleOption (const std::string & param) const;
508 
515  bool HandleNonOption (const std::string &value);
516 
524  void HandleArgument (const std::string &name, const std::string &value) const;
532  static bool HandleAttribute (const std::string name, const std::string value);
533 
538  void PrintGlobals (std::ostream &os) const;
545  void PrintAttributes (std::ostream &os, const std::string &type) const;
552  void PrintGroup (std::ostream &os, const std::string &group) const;
558  void PrintTypeIds (std::ostream &os) const;
564  void PrintGroups (std::ostream &os) const;
570  void Copy (const CommandLine &cmd);
572  void Clear (void);
577  void PrintDoxygenUsage (void) const;
578 
579  typedef std::vector<Item *> Items;
582  std::size_t m_NNonOptions;
583  std::size_t m_nonOptionCount;
584  std::string m_usage;
585  std::string m_shortName;
587 }; // class CommandLine
588 
589 
597 namespace CommandLineHelper {
598 
608 template <typename T>
609 bool UserItemParse (const std::string value, T & val);
617 template <>
618 bool UserItemParse<bool> (const std::string value, bool & val);
627 template <>
628 bool UserItemParse<uint8_t> (const std::string value, uint8_t & val);
629 
638 template <typename T>
639 std::string GetDefault (const T & val);
640 template <>
641 std::string GetDefault<bool> (const bool & val);
644 } // namespace CommandLineHelper
645 
646 
647 } // namespace ns3
648 
649 
650 /********************************************************************
651  * Implementation of the templates declared above.
652  ********************************************************************/
653 
654 namespace ns3 {
655 
656 template <typename T>
657 void
658 CommandLine::AddValue (const std::string &name,
659  const std::string &help,
660  T &value)
661 {
662  UserItem<T> *item = new UserItem<T> ();
663  item->m_name = name;
664  item->m_help = help;
665  item->m_valuePtr = &value;
666 
667  std::stringstream ss;
668  ss << value;
669  ss >> item->m_default;
670 
671  m_options.push_back (item);
672 }
673 
674 template <typename T>
675 void
676 CommandLine::AddNonOption (const std::string name,
677  const std::string help,
678  T & value)
679 {
680  UserItem<T> *item = new UserItem<T> ();
681  item->m_name = name;
682  item->m_help = help;
683  item->m_valuePtr = &value;
684 
685  std::stringstream ss;
686  ss << value;
687  ss >> item->m_default;
688  m_nonOptions.push_back (item);
689  ++m_NNonOptions;
690 
691 }
692 
693 template <typename T>
694 bool
696 {
697  return true;
698 }
699 
700 template <typename T>
701 std::string
703 {
704  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
705 }
706 
707 template <typename T>
708 std::string
710 {
711  std::ostringstream oss;
712  oss << val;
713  return oss.str ();
714 }
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.
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:459
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:579
std::string m_value
The argument value.
Definition: command-line.h:475
std::string m_default
String representation of default value.
Definition: command-line.h:460
std::string GetName() const
Get the program name.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:581
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:423
cmd
Definition: second.py:35
An argument Item assigning to POD.
Definition: command-line.h:451
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 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.
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:676
An argument Item using a Callback to parse the input.
Definition: command-line.h:482
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.
virtual std::string GetDefault() const
Items m_options
The list of option arguments.
Definition: command-line.h:580
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:585
std::string m_usage
The Usage string.
Definition: command-line.h:584
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:496
std::string GetDefault() const
Definition: command-line.h:702
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:497
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 GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
Parse command-line arguments.
Definition: command-line.h:226
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:583
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:709
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:77
std::string m_name
Argument label: --m_name=...
Definition: command-line.h:426
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:427
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:658
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:582
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:467