View | Details | Raw Unified | Return to bug 1653
Collapse All | Expand All

(-)a/src/core/model/command-line.cc (-46 / +46 lines)
 Lines 147-163   CommandLine::Parse (int iargc, char *argv[]) Link Here 
147
}
147
}
148
148
149
void
149
void
150
CommandLine::PrintHelp (void) const
150
CommandLine::PrintHelp (std::ostream &os) const
151
{
151
{
152
  NS_LOG_FUNCTION (this);
152
  NS_LOG_FUNCTION (this);
153
153
154
  std::cout << m_name << " [Program Arguments] [General Arguments]"
154
  os << m_name << " [Program Arguments] [General Arguments]"
155
            << std::endl;
155
     << std::endl;
156
  
156
  
157
  if (m_usage.length ())
157
  if (m_usage.length ())
158
    {
158
    {
159
      std::cout << std::endl;
159
      os << std::endl;
160
      std::cout << m_usage << std::endl;
160
      os << m_usage << std::endl;
161
    }
161
    }
162
  
162
  
163
  if (!m_items.empty ())
163
  if (!m_items.empty ())
 Lines 169-193   CommandLine::PrintHelp (void) const Link Here 
169
        }
169
        }
170
      width += 3;
170
      width += 3;
171
171
172
      std::cout << std::endl;
172
      os << std::endl;
173
      std::cout << "Program Arguments:" << std::endl;
173
      os << "Program Arguments:" << std::endl;
174
      for (Items::const_iterator i = m_items.begin (); i != m_items.end (); ++i)
174
      for (Items::const_iterator i = m_items.begin (); i != m_items.end (); ++i)
175
        {
175
        {
176
          std::cout << "    --"
176
          os << "    --"
177
                    << std::left << std::setw (width) << ( (*i)->m_name + ":")
177
             << std::left << std::setw (width) << ( (*i)->m_name + ":")
178
                    << std::right
178
             << std::right
179
                    << (*i)->m_help;
179
             << (*i)->m_help;
180
180
181
          if ( (*i)->HasDefault ())
181
          if ( (*i)->HasDefault ())
182
            {
182
            {
183
              std::cout << " [" << (*i)->GetDefault () << "]";
183
              os << " [" << (*i)->GetDefault () << "]";
184
            }
184
    }
185
          std::cout << std::endl;
185
          os << std::endl;
186
        }
186
}
187
    }
187
    }
188
188
189
  std::cout << std::endl;
189
  os << std::endl;
190
  std::cout
190
  os
191
    << "General Arguments:\n"
191
    << "General Arguments:\n"
192
    << "    --PrintGlobals:              Print the list of globals.\n"
192
    << "    --PrintGlobals:              Print the list of globals.\n"
193
    << "    --PrintGroups:               Print the list of groups.\n"
193
    << "    --PrintGroups:               Print the list of groups.\n"
 Lines 199-278   CommandLine::PrintHelp (void) const Link Here 
199
}
199
}
200
200
201
void
201
void
202
CommandLine::PrintGlobals (void) const
202
CommandLine::PrintGlobals (std::ostream &os) const
203
{
203
{
204
  NS_LOG_FUNCTION (this);
204
  NS_LOG_FUNCTION (this);
205
205
206
  std::cout << "Global values:" << std::endl;
206
  os << "Global values:" << std::endl;
207
  
207
  
208
  for (GlobalValue::Iterator i = GlobalValue::Begin ();
208
  for (GlobalValue::Iterator i = GlobalValue::Begin ();
209
       i != GlobalValue::End ();
209
       i != GlobalValue::End ();
210
       ++i)
210
       ++i)
211
    {
211
    {
212
      std::cout << "    --" << (*i)->GetName () << "=[";
212
      os << "    --" << (*i)->GetName () << "=[";
213
      Ptr<const AttributeChecker> checker = (*i)->GetChecker ();
213
      Ptr<const AttributeChecker> checker = (*i)->GetChecker ();
214
      StringValue v;
214
      StringValue v;
215
      (*i)->GetValue (v);
215
      (*i)->GetValue (v);
216
      std::cout << v.Get () << "]" << std::endl;
216
      os << v.Get () << "]" << std::endl;
217
      std::cout << "        " << (*i)->GetHelp () << std::endl;
217
      os << "        " << (*i)->GetHelp () << std::endl;
218
    }
218
    }
219
}
219
}
220
220
221
void
221
void
222
CommandLine::PrintAttributes (std::string type) const
222
CommandLine::PrintAttributes (std::ostream &os, const std::string &type) const
223
{
223
{
224
  NS_LOG_FUNCTION (this);
224
  NS_LOG_FUNCTION (this);
225
225
226
  TypeId tid;
226
  TypeId tid;
227
  if (!TypeId::LookupByNameFailSafe (type, &tid))
227
  if (!TypeId::LookupByNameFailSafe (type, &tid))
228
    {
228
    {
229
      NS_FATAL_ERROR ("Unknown type=" << type << " in --PrintAttributes");
229
      NS_FATAL_ERROR ("Unknown type="<<type<<" in --PrintAttributes");
230
    }
230
    }
231
231
232
  std::cout << "Attributes for TypeId " << tid.GetName () << std::endl;
232
  os << "Attributes for TypeId " << tid.GetName () << std::endl;
233
  
233
  
234
  for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
234
  for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
235
    {
235
    {
236
      std::cout << "    --" << tid.GetAttributeFullName (i) << "=[";
236
      os << "    --" << tid.GetAttributeFullName (i) << "=[";
237
      struct TypeId::AttributeInformation info = tid.GetAttribute (i);
237
      struct TypeId::AttributeInformation info = tid.GetAttribute (i);
238
      std::cout << info.initialValue->SerializeToString (info.checker) << "]"
238
      os << info.initialValue->SerializeToString (info.checker) << "]"
239
                << std::endl;
239
                << std::endl;
240
      std::cout << "        " << info.help << std::endl;
240
      os << "        " << info.help << std::endl;
241
    }
241
    }
242
}
242
}
243
243
244
244
245
void
245
void
246
CommandLine::PrintGroup (std::string group) const
246
CommandLine::PrintGroup (std::ostream &os, const std::string &group) const
247
{
247
{
248
  NS_LOG_FUNCTION (this);
248
  NS_LOG_FUNCTION (this);
249
249
250
  std::cout << "TypeIds in group " << group << ":" << std::endl;
250
  os << "TypeIds in group " << group << ":" << std::endl;
251
  
251
  
252
  for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
252
  for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
253
    {
253
    {
254
      TypeId tid = TypeId::GetRegistered (i);
254
      TypeId tid = TypeId::GetRegistered (i);
255
      if (tid.GetGroupName () == group)
255
      if (tid.GetGroupName () == group)
256
        {
256
        {
257
          std::cout << "    " <<tid.GetName () << std::endl;
257
          os << "    " <<tid.GetName () << std::endl;
258
        }
258
        }
259
    }
259
    }
260
}
260
}
261
261
262
void
262
void
263
CommandLine::PrintTypeIds (void) const
263
CommandLine::PrintTypeIds (std::ostream &os) const
264
{
264
{
265
  NS_LOG_FUNCTION (this);
265
  NS_LOG_FUNCTION (this);
266
  std::cout << "Registered TypeIds:" << std::endl;
266
  os << "Registered TypeIds:" << std::endl;
267
  for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
267
  for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
268
    {
268
    {
269
      TypeId tid = TypeId::GetRegistered (i);
269
      TypeId tid = TypeId::GetRegistered (i);
270
      std::cout << "    " << tid.GetName () << std::endl;
270
      os << "    " << tid.GetName () << std::endl;
271
    }
271
    }
272
}
272
}
273
273
274
void
274
void
275
CommandLine::PrintGroups (void) const
275
CommandLine::PrintGroups (std::ostream &os) const
276
{
276
{
277
  NS_LOG_FUNCTION (this);
277
  NS_LOG_FUNCTION (this);
278
278
 Lines 302-357   CommandLine::PrintGroups (void) const Link Here 
302
        }
302
        }
303
    }
303
    }
304
  
304
  
305
  std::cout << "Registered TypeId groups:" << std::endl;
305
  os << "Registered TypeId groups:" << std::endl;
306
  
306
  
307
  for (std::list<std::string>::const_iterator k = groups.begin ();
307
  for (std::list<std::string>::const_iterator k = groups.begin ();
308
       k != groups.end ();
308
       k != groups.end ();
309
       ++k)
309
       ++k)
310
    {
310
    {
311
      std::cout << "    " << *k << std::endl;
311
      os << "    " << *k << std::endl;
312
    }
312
    }
313
}
313
}
314
314
315
void
315
void
316
CommandLine::HandleArgument (std::string name, std::string value) const
316
CommandLine::HandleArgument (const std::string &name, const std::string &value) const
317
{
317
{
318
  NS_LOG_FUNCTION (this << name << value);
318
  NS_LOG_FUNCTION (this << name << value);
319
319
320
  NS_LOG_DEBUG ("Handle arg name=" << name << " value=" << value);
320
  NS_LOG_DEBUG ("Handle arg name="<<name<<" value="<<value);
321
  if (name == "PrintHelp" || name == "help")
321
  if (name == "PrintHelp" || name == "help")
322
    {
322
    {
323
      // method below never returns.
323
      // method below never returns.
324
      PrintHelp ();
324
      PrintHelp (std::cout);
325
      std::exit (0);
325
      std::exit (0);
326
    } 
326
    } 
327
  else if (name == "PrintGroups")
327
  else if (name == "PrintGroups")
328
    {
328
    {
329
      // method below never returns.
329
      // method below never returns.
330
      PrintGroups ();
330
      PrintGroups (std::cout);
331
      std::exit (0);
331
      std::exit (0);
332
    }
332
    }
333
  else if (name == "PrintTypeIds")
333
  else if (name == "PrintTypeIds")
334
    {
334
    {
335
      // method below never returns.
335
      // method below never returns.
336
      PrintTypeIds ();
336
      PrintTypeIds (std::cout);
337
      std::exit (0);
337
      std::exit (0);
338
    }
338
    }
339
  else if (name == "PrintGlobals")
339
  else if (name == "PrintGlobals")
340
    {
340
    {
341
      // method below never returns.
341
      // method below never returns.
342
      PrintGlobals ();
342
      PrintGlobals (std::cout);
343
      std::exit (0);
343
      std::exit (0);
344
    }
344
    }
345
  else if (name == "PrintGroup")
345
  else if (name == "PrintGroup")
346
    {
346
    {
347
      // method below never returns.
347
      // method below never returns.
348
      PrintGroup (value);
348
      PrintGroup (std::cout, value);
349
      std::exit (0);
349
      std::exit (0);
350
    }
350
    }
351
  else if (name == "PrintAttributes")
351
  else if (name == "PrintAttributes")
352
    {
352
    {
353
      // method below never returns.
353
      // method below never returns.
354
      PrintAttributes (value);
354
      PrintAttributes (std::cout, value);
355
      std::exit (0);
355
      std::exit (0);
356
    }
356
    }
357
  else
357
  else
 Lines 378-384   CommandLine::HandleArgument (std::string name, std::string value) const Link Here 
378
    {
378
    {
379
      std::cerr << "Invalid command-line arguments: --"
379
      std::cerr << "Invalid command-line arguments: --"
380
                << name << "=" << value << std::endl;
380
                << name << "=" << value << std::endl;
381
      PrintHelp ();
381
      PrintHelp (std::cerr);
382
      std::exit (1);
382
      std::exit (1);
383
    }
383
    }
384
}
384
}
(-)a/src/core/model/command-line.h (-11 / +42 lines)
 Lines 203-208   public: Link Here 
203
   */
203
   */
204
  std::string GetName () const;
204
  std::string GetName () const;
205
205
206
  /**
207
   * \brief Print program usage to the desired output stream
208
   *
209
   * Handler for \c \-\-PrintHelp and \c \-\-help:  print Usage(), argument names, and help strings
210
   *
211
   * Alternatively, an overloaded operator << can be used:
212
   * @code
213
   *       CommandLine cmd;
214
   *       cmd.Parse (argc, argv);
215
   *     ...
216
   *
217
   *       std::cerr << cmd;
218
   * @endcode
219
   */
220
  void PrintHelp (std::ostream &os) const;
221
206
private:
222
private:
207
223
208
  /**
224
  /**
 Lines 279-307   private: Link Here 
279
   * \param name the argument name
295
   * \param name the argument name
280
   * \param value the command line value
296
   * \param value the command line value
281
   */
297
   */
282
  void HandleArgument (std::string name, std::string value) const;
298
  void HandleArgument (const std::string &name, const std::string &value) const;
283
  /**
284
   * Handler for \c \-\-PrintHelp and \c \-\-help:  print Usage(), argument names, and help strings
285
   */
286
  void PrintHelp (void) const;
287
  /** Handler for \c \-\-PrintGlobals:  print all global variables and values */
299
  /** Handler for \c \-\-PrintGlobals:  print all global variables and values */
288
  void PrintGlobals (void) const;
300
  void PrintGlobals (std::ostream &os) const;
289
  /**
301
  /**
290
   * Handler for \c \-\-PrintAttributes:  print the attributes for a given type.
302
   * Handler for \c \-\-PrintAttributes:  print the attributes for a given type.
291
   *
303
   *
292
   * \param type the TypeId whose Attributes should be displayed
304
   * \param type the TypeId whose Attributes should be displayed
293
   */
305
   */
294
  void PrintAttributes (std::string type) const;
306
  void PrintAttributes (std::ostream &os, const std::string &type) const;
295
  /**
307
  /**
296
   * Handler for \c \-\-PrintGroup:  print all types belonging to a given group.
308
   * Handler for \c \-\-PrintGroup:  print all types belonging to a given group.
297
   *
309
   *
298
   * \param group the name of the TypeId group to display
310
   * \param group the name of the TypeId group to display
299
   */
311
   */
300
  void PrintGroup (std::string group) const;
312
  void PrintGroup (std::ostream &os, const std::string &group) const;
301
  /** Handler for \c \-\-PrintTypeIds:  print all TypeId names. */
313
  /** Handler for \c \-\-PrintTypeIds:  print all TypeId names. */
302
  void PrintTypeIds (void) const;
314
  void PrintTypeIds (std::ostream &os) const;
303
  /** Handler for \c \-\-PrintGroups:  print all TypeId group names */
315
  /** Handler for \c \-\-PrintGroups:  print all TypeId group names */
304
  void PrintGroups (void) const;
316
  void PrintGroups (std::ostream &os) const;
305
  /**
317
  /**
306
   * Copy constructor
318
   * Copy constructor
307
   *
319
   *
 Lines 399-404   CommandLineHelper::UserItemParse (const std::string value, T & val) Link Here 
399
  return !iss.bad () && !iss.fail ();
411
  return !iss.bad () && !iss.fail ();
400
}
412
}
401
413
414
/**
415
 * \brief Overloaded operator << to print program usage (shortcut for CommandLine::PrintHelper)
416
 * \see CommandLine::PrintHelper
417
 *
418
 * Example usage:
419
 * @code
420
 *       CommandLine cmd;
421
 *       cmd.Parse (argc, argv);
422
 *       ...
423
 *
424
 *       std::cerr << cmd;
425
 * @endcode
426
 */
427
inline std::ostream &
428
operator << (std::ostream &os, const CommandLine &cmd)
429
{
430
  cmd.PrintHelp (os);
431
  return os;
432
}
433
402
} // namespace ns3
434
} // namespace ns3
403
435
404
#endif /* COMMAND_LINE_H */
436
#endif /* COMMAND_LINE_H */
405
- 

Return to bug 1653