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 
214 {
215 public:
217  CommandLine ();
223  CommandLine (const CommandLine &cmd);
232  ~CommandLine ();
233 
239  void Usage (const std::string usage);
240 
250  template <typename T>
251  void AddValue (const std::string &name,
252  const std::string &help,
253  T &value);
254 
266  void AddValue (const std::string &name,
267  const std::string &help,
268  Callback<bool, std::string> callback);
269 
276  void AddValue (const std::string &name,
277  const std::string &attributePath);
278 
288  template <typename T>
289  void AddNonOption (const std::string name, const std::string help, T & value);
290 
301  std::string GetExtraNonOption (std::size_t i) const;
302 
312  std::size_t GetNExtraNonOptions (void) const;
313 
328  void Parse (int argc, char *argv[]);
329 
339  void Parse (std::vector<std::string> args);
340 
346  std::string GetName () const;
347 
364  void PrintHelp (std::ostream &os) const;
365 
366 private:
367 
372  class Item
373  {
374  public:
375  std::string m_name;
376  std::string m_help;
377  virtual ~Item ();
384  virtual bool Parse (const std::string value) = 0;
388  virtual bool HasDefault () const;
392  virtual std::string GetDefault () const;
393  }; // class Item
394 
399  template <typename T>
400  class UserItem : public Item
401  {
402  public:
403  // Inherited
404  virtual bool Parse (const std::string value);
405  bool HasDefault () const;
406  std::string GetDefault () const;
407 
409  std::string m_default;
410  }; // class UserItem
411 
412  class StringItem : public Item
413  {
414  public:
415  // Inherited
416  bool Parse (const std::string value);
417  bool HasDefault (void) const;
418  std::string GetDefault (void) const;
419 
420  std::string m_value;
421  }; // class StringItem
422 
427  class CallbackItem : public Item
428  {
429  public:
436  virtual bool Parse (const std::string value);
438  }; // class CallbackItem
439 
440 
447  bool HandleOption (const std::string & param) const;
448 
455  bool HandleNonOption (const std::string &value);
456 
464  void HandleArgument (const std::string &name, const std::string &value) const;
472  static bool HandleAttribute (const std::string name, const std::string value);
473 
478  void PrintGlobals (std::ostream &os) const;
485  void PrintAttributes (std::ostream &os, const std::string &type) const;
492  void PrintGroup (std::ostream &os, const std::string &group) const;
498  void PrintTypeIds (std::ostream &os) const;
504  void PrintGroups (std::ostream &os) const;
510  void Copy (const CommandLine &cmd);
512  void Clear (void);
513 
514  typedef std::vector<Item *> Items;
517  std::size_t m_NNonOptions;
518  std::size_t m_nonOptionCount;
519  std::string m_usage;
520  std::string m_name;
522 }; // class CommandLine
523 
524 
532 namespace CommandLineHelper {
533 
543  template <typename T>
544  bool UserItemParse (const std::string value, T & val);
545  template <>
546  bool UserItemParse<bool> (const std::string value, bool & val);
557  template <typename T>
558  std::string GetDefault (const T & val);
559  template <>
560  std::string GetDefault<bool> (const bool & val);
563 } // namespace CommandLineHelper
564 
565 
566 
567 } // namespace ns3
568 
569 
570 /********************************************************************
571  * Implementation of the templates declared above.
572  ********************************************************************/
573 
574 namespace ns3 {
575 
576 template <typename T>
577 void
578 CommandLine::AddValue (const std::string &name,
579  const std::string &help,
580  T &value)
581 {
582  UserItem<T> *item = new UserItem<T> ();
583  item->m_name = name;
584  item->m_help = help;
585  item->m_valuePtr = &value;
586 
587  std::stringstream ss;
588  ss << value;
589  ss >> item->m_default;
590 
591  m_options.push_back (item);
592 }
593 
594 template <typename T>
595 void
596 CommandLine::AddNonOption (const std::string name,
597  const std::string help,
598  T & value)
599 {
600  UserItem<T> *item = new UserItem<T> ();
601  item->m_name = name;
602  item->m_help = help;
603  item->m_valuePtr = &value;
604 
605  std::stringstream ss;
606  ss << value;
607  ss >> item->m_default;
608  m_nonOptions.push_back (item);
609  ++m_NNonOptions;
610 
611 }
612 
613 template <typename T>
614 bool
616 {
617  return true;
618 }
619 
620 template <typename T>
621 std::string
623 {
624  return CommandLineHelper::GetDefault<T> (*m_valuePtr);
625 }
626 
627 template <typename T>
628 std::string
630 {
631  std::ostringstream oss;
632  oss << val;
633  return oss.str ();
634 }
635 
636 
637 template <typename T>
638 bool
639 CommandLine::UserItem<T>::Parse (const std::string value)
640 {
641  return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
642 }
643 
644 template <typename T>
645 bool
646 CommandLineHelper::UserItemParse (const std::string value, T & val)
647 {
648  std::istringstream iss;
649  iss.str (value);
650  iss >> val;
651  return !iss.bad () && !iss.fail ();
652 }
653 
673 std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
674 
675 } // namespace ns3
676 
677 #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:408
std::vector< Item * > Items
Argument list container.
Definition: command-line.h:514
std::string m_value
The argument value.
Definition: command-line.h:420
std::string m_default
String representation of default value.
Definition: command-line.h:409
std::string GetName() const
Get the program name.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:516
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:639
The argument abstract base class.
Definition: command-line.h:372
cmd
Definition: second.py:35
An argument Item assigning to POD.
Definition: command-line.h:400
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:596
An argument Item using a Callback to parse the input.
Definition: command-line.h:427
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:515
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:519
std::string GetDefault(void) const
std::string GetDefault() const
Definition: command-line.h:622
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:520
Parse command-line arguments.
Definition: command-line.h:213
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:518
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:629
CommandLine & operator=(const CommandLine &cmd)
Assignment.
Definition: command-line.cc:59
std::string m_name
Argument label: ---m_name=...
Definition: command-line.h:375
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.
Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:437
virtual ~Item()
Destructor.
std::string m_help
Argument help string.
Definition: command-line.h:376
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:578
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:646
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:517
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
virtual bool HasDefault() const