src/core/command-line.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 10 Oct 2007 11:14:16 +0100
changeset 1703 3f808ee49c2f
parent 1702 0fbe74581141
child 1804 9c11e776f501
permissions -rw-r--r--
Fix CommandLine::AddArgCommand memory leak.
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; -*- */
599
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     2
/*
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     3
 * Copyright (c) 2007 Georgia Tech University, INRIA
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     4
 *
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     7
 * published by the Free Software Foundation;
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     8
 *
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    12
 * GNU General Public License for more details.
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    13
 *
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    15
 * along with this program; if not, write to the Free Software
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    17
 *
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    18
 * Authors: Raj Bhattacharjea <raj.b@gatech.edu>,
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    19
 *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7b7012218291 add license header
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 439
diff changeset
    20
 */
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    21
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    22
#include "command-line.h"
1702
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
    23
#include "ns3/debug.h"
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    24
#include <unistd.h>
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    25
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    26
namespace ns3 {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    27
1703
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    28
CommandLine::List::~List ()
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    29
{
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    30
  for (iterator iter = begin (); iter != end (); iter++)
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    31
    {
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    32
      delete *iter;
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    33
    }
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    34
}
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    35
3f808ee49c2f Fix CommandLine::AddArgCommand memory leak.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1702
diff changeset
    36
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    37
CommandDefaultValue CommandLine::g_help ("help",
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    38
                                         "Print Help text for all commands",
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    39
                                         MakeCallback (&CommandLine::PrintHelp));
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    40
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    41
void 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    42
CommandLine::AddArgCommand (const std::string &name,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    43
                            const std::string &help,
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    44
                            Callback<void> cb)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    45
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    46
  DefaultValueBase *base = 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    47
    new CommandDefaultValue (name, help, cb);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    48
  GetUserList ()->push_back (base);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    49
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    50
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    51
CommandLine::List *
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    52
CommandLine::GetUserList (void)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    53
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    54
  static List list;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    55
  return &list;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    56
}
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
void 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    59
CommandLine::PrintHelp (void)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    60
{
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    61
  for (List::iterator i = GetUserList ()->begin ();
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    62
       i != GetUserList ()->end (); i++)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    63
    {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    64
      DefaultValueBase *item = *i;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    65
      if (item->GetType () == "" &&
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    66
          item->GetDefaultValue () == "")
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
          std::cout << "--" << item->GetName () << "\t" << item->GetHelp () << std::endl;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    69
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    70
      else
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    71
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    72
          std::cout << "--" << item->GetName () << "=[" << item->GetType () << ":"
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    73
                    << item->GetDefaultValue () << "]\t" << item->GetHelp () << std::endl;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    74
        }
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
  for (List::iterator i = DefaultValueList::Begin ();
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    77
       i != DefaultValueList::End (); i++)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    78
    {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    79
      DefaultValueBase *item = *i;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    80
      if (item->GetType () == "" &&
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    81
          item->GetDefaultValue () == "")
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    82
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    83
          std::cout << "--" << item->GetName () << "\t" << item->GetHelp () << std::endl;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    84
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    85
      else
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    86
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    87
          std::cout << "--" << item->GetName () << "=[" << item->GetType () << ":"
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    88
                    << item->GetDefaultValue () << "]\t" << item->GetHelp () << std::endl;
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
    }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    91
  // XXX on win32, do the right thing here.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    92
  exit (0);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    93
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    94
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    95
void 
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    96
CommandLine::Parse (int argc, char *argv[])
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    97
{
1533
1e8249c58fda Fix infinite loop in CommandLine::Parse when trying to ignore a badly formed parameter.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 599
diff changeset
    98
  for (argc--, argv++; argc > 0; argc--, argv++)
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    99
    {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   100
      // remove "--" or "-" heading.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   101
      std::string param = *argv;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   102
      std::string::size_type cur = param.find ("--");
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   103
      if (cur == std::string::npos)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   104
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   105
          cur = param.find ("-");
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   106
          if (cur == std::string::npos)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   107
            {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   108
              // invalid argument. ignore it.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   109
              continue;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   110
            }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   111
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   112
      if (cur != 0)
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
          // invalid argument. ignore it.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   115
          continue;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   116
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   117
      param = std::string (param, 2, param.size ());
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   118
      cur = param.find ("=");
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   119
      std::string name, value;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   120
      if (cur == std::string::npos)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   121
        {
1702
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   122
          if (argc == 1)
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   123
            {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   124
              // invalid argument. ignore it.
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   125
              continue;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   126
            }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   127
          argv++;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   128
          argc--;
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   129
          name = param;
1702
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   130
          value = *argv;
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   131
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   132
      else
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   133
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   134
          name = std::string (param, 0, cur);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   135
          value = std::string (param, cur + 1, std::string::npos);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   136
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   137
      // try to find this argument in the user args.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   138
      for (List::iterator i = GetUserList ()->begin ();
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   139
           i != GetUserList ()->end (); i++)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   140
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   141
          DefaultValueBase *item = *i;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   142
          if (item->GetName () == name)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   143
            {
1702
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   144
              if (!item->ParseValue (value))
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   145
                {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   146
                  std::cerr << "Warning: failed to parse command line argument `"
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   147
                            << name << "' of type '" << item->GetType ()
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   148
                            << "' with value `" << value << "'." << std::endl;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   149
                }
439
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   150
              continue;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   151
            }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   152
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   153
           
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   154
      // try to find this argument in the default args.
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   155
      for (List::iterator i = DefaultValueList::Begin ();
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   156
           i != DefaultValueList::End (); i++)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   157
        {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   158
          DefaultValueBase *item = *i;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   159
          if (item->GetName () == name)
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   160
            {
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   161
              item->ParseValue (value);
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   162
              continue;
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   163
            }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   164
        }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   165
    }
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   166
}
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   167
fed13fb45eef Incorporated defaults and command-line arguments
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   168
}//namespace ns3
1702
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   169
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   170
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   171
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   172
#ifdef RUN_SELF_TESTS
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   173
#include "test.h"
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   174
#include <iostream>
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   175
#include <sstream>
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   176
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   177
namespace ns3 {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   178
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   179
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   180
class CommandLineTest : public Test
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   181
{
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   182
public:
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   183
  CommandLineTest () : Test ("CommandLine") {}
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   184
  virtual bool RunTests (void)
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   185
  {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   186
    bool result = true;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   187
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   188
    // redirect stderr temporarily (else warnings appear during unit testing, which is not nice)
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   189
    std::ostringstream nullout;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   190
    std::streambuf *origcerr = std::cerr.rdbuf (nullout.rdbuf ());
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   191
    {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   192
      char *argv[] = {"run-tests", "--loops", "bad-value", NULL};
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   193
      int argc = sizeof (argv) / sizeof (argv[0]) - 1;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   194
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   195
      uint32_t loops = 123;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   196
      CommandLine::AddArgValue ("loops","a test of the command line", loops);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   197
      CommandLine::Parse (argc, argv);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   198
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   199
      NS_TEST_ASSERT_EQUAL (loops, 123);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   200
    }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   201
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   202
    {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   203
      char *argv[] = {"run-tests", "--loops=bad-value", NULL};
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   204
      int argc = sizeof (argv) / sizeof (argv[0]) - 1;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   205
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   206
      uint32_t loops = 123;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   207
      CommandLine::AddArgValue ("loops","a test of the command line", loops);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   208
      CommandLine::Parse (argc, argv);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   209
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   210
      NS_TEST_ASSERT_EQUAL (loops, 123);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   211
    }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   212
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   213
    {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   214
      char *argv[] = {"run-tests", "--loops", "456", NULL};
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   215
      int argc = sizeof (argv) / sizeof (argv[0]) - 1;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   216
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   217
      uint32_t loops = 123;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   218
      CommandLine::AddArgValue ("loops","a test of the command line", loops);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   219
      CommandLine::Parse (argc, argv);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   220
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   221
      NS_TEST_ASSERT_EQUAL (loops, 456);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   222
    }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   223
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   224
    {
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   225
      char *argv[] = {"run-tests", "--loops=456", NULL};
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   226
      int argc = sizeof (argv) / sizeof (argv[0]) - 1;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   227
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   228
      uint32_t loops = 123;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   229
      CommandLine::AddArgValue ("loops","a test of the command line", loops);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   230
      CommandLine::Parse (argc, argv);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   231
      
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   232
      NS_TEST_ASSERT_EQUAL (loops, 456);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   233
    }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   234
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   235
    // unredirect cerr
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   236
    std::cerr.rdbuf (origcerr);
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   237
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   238
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   239
    return result;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   240
  }
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   241
};
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   242
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   243
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   244
static CommandLineTest g_commandLineTests;
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   245
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   246
}//namespace ns3
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   247
0fbe74581141 Bug 81 (CommandLine::AddArgValue not working correctly)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1533
diff changeset
   248
#endif /* RUN_SELF_TESTS */