src/core/command-line.h
author Raj Bhattacharjea <raj.b@gatech.edu>
Fri, 11 May 2007 10:37:01 -0400
changeset 538 3cc417842b5f
parent 441 d824620eac38
child 599 7b7012218291
permissions -rw-r--r--
Added/fixed some doxygen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     2
#ifndef COMMAND_LINE_H
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     3
#define COMMAND_LINE_H
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     4
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     5
#include <list>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     6
#include <string>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     7
#include "default-value.h"
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     8
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     9
namespace ns3 {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    10
538
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    11
/**
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    12
 * \brief Command line argument processing class
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    13
 * \ingroup config
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    14
 *
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    15
 * Often times in simulations, the user will want the ability to change and
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    16
 * tweak simulation parameters without having to recompile the entire 
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    17
 * simulation.  This can be done by passing arguments in from the command
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    18
 * line invocation of a simulation.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    19
 * This class is a collection of static functions to aid in this facility.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    20
 * By using it, users get the automatic things like support for --help command
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    21
 * line arguments.  See samples/main-default-value.cc for more info.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    22
 */
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    23
class CommandLine
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    24
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    25
public:
538
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    26
  /**
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    27
   * \brief Add a command line argument to the system
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    28
   * \param name Name of the string to expect on the command line
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    29
   * \param help A help string for this param, displayed --help is used.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    30
   * \param value The desination to store the value read from the command line
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    31
   */
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    32
  template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    33
  static void AddArgValue (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    34
			   const std::string &help, 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    35
			   T &value);
538
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    36
  /**
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    37
   * \brief Add a command line triggered function call to the system
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    38
   * \param name Name of the string to expect on the command line
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    39
   * \param help A help string for this param, displayed --help is used.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    40
   * \param value An ns3::Callback that gets called if name is present as a
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    41
   * commandline argument.
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    42
   */
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    43
  static void AddArgCommand (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    44
			     const std::string &help,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    45
			     Callback<void> cb);
538
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    46
  /**
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    47
   * \brief Parse the command line for arguments thus far added
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    48
   * \param argc Number of strings on the command line; pass this directly from
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    49
   * the first argument of your main(int,char**) function 
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    50
   * \param argv Array of strings passed in as arguments; pass this directly from
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    51
   * the second argument of your main(int,char**) function
3cc417842b5f Added/fixed some doxygen
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 441
diff changeset
    52
   */
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    53
  static void Parse (int argc, char *argv[]);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    54
 private:
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    55
  template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    56
  class UserDefaultValue : public DefaultValueBase 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    57
  {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    58
  public:
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    59
    UserDefaultValue (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    60
                      const std::string &help,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    61
		      T &value);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    62
  private:
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    63
    virtual bool DoParseValue (const std::string &value);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    64
    virtual std::string DoGetType (void) const;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    65
    virtual std::string DoGetDefaultValue (void) const;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    66
    T *m_valuePtr;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    67
  };
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    68
  static void PrintHelp (void);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    69
  typedef std::list<DefaultValueBase *> List;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    70
  static List *GetUserList (void);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    71
  static CommandDefaultValue g_help;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    72
};
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    73
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    74
}//namespace ns3
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    75
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    76
namespace ns3 {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    77
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    78
template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    79
void 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    80
CommandLine::AddArgValue (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    81
			  const std::string &help, 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    82
			  T &value)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    83
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    84
  DefaultValueBase *base = 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    85
    new UserDefaultValue<T> (name, help, value);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    86
  GetUserList ()->push_back (base);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    87
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    88
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    89
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    90
template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    91
CommandLine::UserDefaultValue<T>::UserDefaultValue (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    92
						    const std::string &help,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    93
						    T &value)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    94
  : DefaultValueBase (name, help),
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    95
    m_valuePtr (&value)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    96
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    97
  // we do not register in the DefaultValueList class on purpose.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    98
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    99
template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   100
bool
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   101
CommandLine::UserDefaultValue<T>::DoParseValue (const std::string &value)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   102
{
441
d824620eac38 Added default value sample
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 439
diff changeset
   103
  std::istringstream iss;
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   104
  iss.str (value);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   105
  T v;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   106
  iss >> v;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   107
  *m_valuePtr = v;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   108
  return !iss.bad () && !iss.fail ();
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   109
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   110
template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   111
std::string
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   112
CommandLine::UserDefaultValue<T>::DoGetType (void) const
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   113
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   114
  return "";
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   115
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   116
template <typename T>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   117
std::string
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   118
CommandLine::UserDefaultValue<T>::DoGetDefaultValue (void) const
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   119
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   120
  return "";
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   121
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   122
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   123
}//namespace ns3
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   124
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   125
#endif /* COMMAND_LINE_H */