src/core/command-line.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3190 51fe9001a679
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2581
    20
#ifndef COMMAND_LINE_H
mathieu@2581
    21
#define COMMAND_LINE_H
raj@439
    22
mathieu@2575
    23
#include <string>
mathieu@2575
    24
#include <sstream>
raj@439
    25
#include <list>
raj@439
    26
gjc@3929
    27
#include "ns3/callback.h"
gjc@3929
    28
raj@439
    29
namespace ns3 {
raj@439
    30
mathieu@2584
    31
/**
mathieu@2584
    32
 * \brief parse command-line arguments
mathieu@3190
    33
 * \ingroup core
mathieu@2584
    34
 *
mathieu@2584
    35
 * Instances of this class can be used to parse command-line 
mathieu@2584
    36
 * arguments: users can register new arguments with
mathieu@2584
    37
 * CommandLine::AddValue but the most important functionality
mathieu@2584
    38
 * provided by this class is that it can be used to set the
mathieu@2584
    39
 * 'initial value' of every attribute in the system with the
mathieu@2584
    40
 * --TypeIdName::AttributeName=value syntax and it can be used
mathieu@2584
    41
 * to set the value of every GlobalValue in the system with
mathieu@2584
    42
 * the --GlobalValueName=value syntax.
mathieu@2584
    43
 */
raj@439
    44
class CommandLine
raj@439
    45
{
raj@439
    46
public:
mathieu@2575
    47
  ~CommandLine ();
mathieu@2575
    48
mathieu@2584
    49
  /**
mathieu@2584
    50
   * \param name the name of the user-supplied argument
mathieu@2584
    51
   * \param help some help text used by --PrintHelp
mathieu@2584
    52
   * \param value a reference to the variable where the
mathieu@2584
    53
   *        value parsed will be stored (if no value
mathieu@2584
    54
   *        is parsed, this variable is not modified).
mathieu@2584
    55
   */
raj@439
    56
  template <typename T>
mathieu@2575
    57
  void AddValue (const std::string &name,
mathieu@2575
    58
		 const std::string &help,
mathieu@2575
    59
		 T &value);
mathieu@2575
    60
gjc@3929
    61
gjc@3929
    62
  /**
gjc@3929
    63
   * \param name the name of the user-supplied argument
gjc@3929
    64
   * \param help some help text used by --PrintHelp
gjc@3929
    65
   * \param callback a callback function that will be invoked to parse
gjc@3929
    66
   * and collect the value.  This normally used by language bindings.
gjc@3929
    67
   */
gjc@3929
    68
  void AddValue (const std::string &name,
gjc@3929
    69
		 const std::string &help,
gjc@3929
    70
                 Callback<bool, std::string> callback);
gjc@3929
    71
mathieu@2584
    72
  /**
mathieu@2584
    73
   * \param argc the 'argc' variable: number of arguments (including the
mathieu@2584
    74
   *        main program name as first element).
mathieu@2584
    75
   * \param argv the 'argv' variable: a null-terminated array of strings,
mathieu@2584
    76
   *        each of which identifies a command-line argument.
mathieu@2584
    77
   * 
mathieu@2584
    78
   * Obviously, this method will parse the input command-line arguments and
mathieu@2584
    79
   * will attempt to handle them all.
mathieu@2584
    80
   */
mathieu@2915
    81
  void Parse (int argc, char *argv[]) const;
mathieu@2575
    82
private:
mathieu@2575
    83
  class Item 
raj@439
    84
  {
raj@439
    85
  public:
mathieu@2575
    86
    std::string m_name;
mathieu@2575
    87
    std::string m_help;
mathieu@2575
    88
    virtual ~Item ();
mathieu@2575
    89
    virtual bool Parse (std::string value) = 0;
mathieu@2575
    90
  };
mathieu@2575
    91
  template <typename T>
mathieu@2575
    92
  class UserItem : public Item
mathieu@2575
    93
  {
mathieu@2575
    94
  public:
mathieu@2575
    95
    virtual bool Parse (std::string value);
raj@439
    96
    T *m_valuePtr;
raj@439
    97
  };
gjc@3929
    98
  class CallbackItem : public Item
gjc@3929
    99
  {
gjc@3929
   100
  public:
gjc@3929
   101
    virtual bool Parse (std::string value);
gjc@3929
   102
    Callback<bool, std::string> m_callback;
gjc@3929
   103
  };
gjc@3929
   104
mathieu@2575
   105
  void HandleArgument (std::string name, std::string value) const;
mathieu@2575
   106
  void PrintHelp (void) const;
mathieu@2575
   107
  void PrintGlobals (void) const;
mathieu@2575
   108
  void PrintAttributes (std::string type) const;
mathieu@2575
   109
  void PrintGroup (std::string group) const;
mathieu@2575
   110
  void PrintTypeIds (void) const;
mathieu@2575
   111
  void PrintGroups (void) const;
gjc@1703
   112
mathieu@2575
   113
  typedef std::list<Item *> Items;
mathieu@2575
   114
  Items m_items;
raj@439
   115
};
raj@439
   116
mathieu@2575
   117
} // namespace ns3
raj@439
   118
raj@439
   119
namespace ns3 {
raj@439
   120
raj@439
   121
template <typename T>
raj@439
   122
void 
mathieu@2575
   123
CommandLine::AddValue (const std::string &name,
mathieu@2575
   124
		   const std::string &help,
mathieu@2575
   125
		   T &value)
raj@439
   126
{
mathieu@2575
   127
  UserItem<T> *item = new UserItem<T> ();
mathieu@2575
   128
  item->m_name = name;
mathieu@2575
   129
  item->m_help = help;
mathieu@2575
   130
  item->m_valuePtr = &value;
mathieu@2575
   131
  m_items.push_back (item);
raj@439
   132
}
raj@439
   133
raj@439
   134
template <typename T>
mathieu@2575
   135
bool 
mathieu@2575
   136
CommandLine::UserItem<T>::Parse (std::string value)
raj@439
   137
{
raj@441
   138
  std::istringstream iss;
raj@439
   139
  iss.str (value);
mathieu@2575
   140
  iss >> (*m_valuePtr);
mathieu@2575
   141
  return !iss.bad () && !iss.fail ();
raj@439
   142
}
raj@439
   143
mathieu@2575
   144
} // namespace ns3
raj@439
   145
mathieu@2581
   146
#endif /* COMMAND_LINE_H */