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 
222 {
223 public:
225  CommandLine (void);
236  CommandLine (const std::string filename);
242  CommandLine (const CommandLine &cmd);
251  ~CommandLine ();
252 
258  void Usage (const std::string usage);
259 
269  template <typename T>
270  void AddValue (const std::string &name,
271  const std::string &help,
272  T &value);
273 
280  typedef bool (* Callback) (const std::string value);
281 
294  void AddValue (const std::string &name,
295  const std::string &help,
297  const std::string defaultValue = "");
298 
299 
306  void AddValue (const std::string &name,
307  const std::string &attributePath);
308 
318  template <typename T>
319  void AddNonOption (const std::string name, const std::string help, T & value);
320 
331  std::string GetExtraNonOption (std::size_t i) const;
332 
342  std::size_t GetNExtraNonOptions (void) const;
343 
358  void Parse (int argc, char *argv[]);
359 
369  void Parse (std::vector<std::string> args);
370 
376  std::string GetName () const;
377 
394  void PrintHelp (std::ostream &os) const;
395 
396 private:
397 
402  class Item
403  {
404  public:
405  std::string m_name;
406  std::string m_help;
407  virtual ~Item ();
414  virtual bool Parse (const std::string value) = 0;
418  virtual bool HasDefault () const;
422  virtual std::string GetDefault () const;
423  }; // class Item
424 
429  template <typename T>
430  class UserItem : public Item
431  {
432  public:
433  // Inherited
434  virtual bool Parse (const std::string value);
435  bool HasDefault () const;
436  std::string GetDefault () const;
437 
439  std::string m_default;
440  }; // class UserItem
441 
446  class StringItem : public Item
447  {
448  public:
449  // Inherited
450  bool Parse (const std::string value);
451  bool HasDefault (void) const;
452  std::string GetDefault (void) const;
453 
454  std::string m_value;
455  }; // class StringItem
456 
461  class CallbackItem : public Item
462  {
463  public:
464  // Inherited
465  bool HasDefault (void) const;
466  std::string GetDefault (void) const;
467 
474  virtual bool Parse (const std::string value);
476  std::string m_default;
477  }; // class CallbackItem
478 
479 
486  bool HandleOption (const std::string & param) const;
487 
494  bool HandleNonOption (const std::string &value);
495 
503  void HandleArgument (const std::string &name, const std::string &value) const;
511  static bool HandleAttribute (const std::string name, const std::string value);
512 
517  void PrintGlobals (std::ostream &os) const;
524  void PrintAttributes (std::ostream &os, const std::string &type) const;
531  void PrintGroup (std::ostream &os, const std::string &group) const;
537  void PrintTypeIds (std::ostream &os) const;
543  void PrintGroups (std::ostream &os) const;
549  void Copy (const CommandLine &cmd);
551  void Clear (void);
556  void PrintDoxygenUsage (void) const;
557 
558  typedef std::vector<Item *> Items;
561  std::size_t m_NNonOptions;
562  std::size_t m_nonOptionCount;
563  std::string m_usage;
564  std::string m_shortName;
566 }; // class CommandLine
567 
568 
576 namespace CommandLineHelper {
577 
587 template <typename T>
588 bool UserItemParse (const std::string value, T & val);
596 template <>
597 bool UserItemParse<bool> (const std::string value, bool & val);
606 template <>
607 bool UserItemParse<uint8_t> (const std::string value, uint8_t & val);
608 
617 template <typename T>
618 std::string GetDefault (const T & val);
619 template <>
620 std::string GetDefault<bool> (const bool & val);
623 } // namespace CommandLineHelper
624 
625 
626 } // namespace ns3
627 
628 
629 /********************************************************************
630  * Implementation of the templates declared above.
631  ********************************************************************/
632 
633 namespace ns3 {
634 
635 template <typename T>
636 void
637 CommandLine::AddValue (const std::string &name,
638  const std::string &help,
639  T &value)
640 {
641  UserItem<T> *item = new UserItem<T> ();
642  item->m_name = name;
643  item->m_help = help;
644  item->m_valuePtr = &value;
645 
646  std::stringstream ss;
647  ss << value;
648  ss >> item->m_default;
649 
650  m_options.push_back (item);
651 }
652 
653 template <typename T>
654 void
655 CommandLine::AddNonOption (const std::string name,
656  const std::string help,
657  T & value)
658 {
659  UserItem<T> *item = new UserItem<T> ();
660  item->m_name = name;
661  item->m_help = help;
662  item->m_valuePtr = &value;
663 
664  std::stringstream ss;
665  ss << value;
666  ss >> item->m_default;
667  m_nonOptions.push_back (item);
668  ++m_NNonOptions;
669 
670 }
671 
672 template <typename T>
673 bool
675 {
676  return true;
677 }
678 
679 template <typename T>
680 std::string
682 {
683  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
684 }
685 
686 template <typename T>
687 std::string
689 {
690  std::ostringstream oss;
691  oss << val;
692  return oss.str ();
693 }
694 
695 
696 template <typename T>
697 bool
698 CommandLine::UserItem<T>::Parse (const std::string value)
699 {
700  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
701 }
702 
703 template <typename T>
704 bool
705 CommandLineHelper::UserItemParse (const std::string value, T & val)
706 {
707  std::istringstream iss;
708  iss.str (value);
709  iss >> val;
710  return !iss.bad () && !iss.fail ();
711 }
712 
732 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
733 
734 } // namespace ns3
735 
736 #endif /* COMMAND_LINE_H */
~CommandLine()
Destructor.
Definition: command-line.cc:79
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:438
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:558
std::string m_value
The argument value.
Definition: command-line.h:454
std::string m_default
String representation of default value.
Definition: command-line.h:439
std::string GetName() const
Get the program name.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:560
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:698
The argument abstract base class.
Definition: command-line.h:402
cmd
Definition: second.py:35
An argument Item assigning to POD.
Definition: command-line.h:430
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:98
void AddNonOption(const std::string name, const std::string help, T &value)
Add a non-option argument, assigning to POD.
Definition: command-line.h:655
An argument Item using a Callback to parse the input.
Definition: command-line.h:461
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:559
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:564
std::string m_usage
The Usage string.
Definition: command-line.h:563
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:475
std::string GetDefault() const
Definition: command-line.h:681
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:476
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:221
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:562
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:688
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:73
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:405
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:50
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:406
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:637
void Copy(const CommandLine &cmd)
Copy constructor.
Definition: command-line.cc:85
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:705
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:561
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:446