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#include "nstime.h"
29#include "type-id.h"
30
37namespace ns3 {
38
229{
230public:
232 CommandLine (void);
243 CommandLine (const std::string filename);
249 CommandLine (const CommandLine &cmd);
258 ~CommandLine ();
259
265 void Usage (const std::string usage);
266
276 template <typename T>
277 void AddValue (const std::string &name,
278 const std::string &help,
279 T &value);
280
287 typedef bool (* Callback) (const std::string value);
288
301 void AddValue (const std::string &name,
302 const std::string &help,
304 const std::string defaultValue = "");
305
306
313 void AddValue (const std::string &name,
314 const std::string &attributePath);
315
325 template <typename T>
326 void AddNonOption (const std::string name, const std::string help, T & value);
327
338 std::string GetExtraNonOption (std::size_t i) const;
339
349 std::size_t GetNExtraNonOptions (void) const;
350
365 void Parse (int argc, char *argv[]);
366
376 void Parse (std::vector<std::string> args);
377
383 std::string GetName () const;
384
401 void PrintHelp (std::ostream &os) const;
402
408 std::string GetVersion () const;
409
417 void PrintVersion (std::ostream &os) const;
418
419private:
420
425 class Item
426 {
427 public:
428 std::string m_name;
429 std::string m_help;
430 virtual ~Item ();
437 virtual bool Parse (const std::string value) = 0;
441 virtual bool HasDefault () const;
445 virtual std::string GetDefault () const = 0;
446 }; // class Item
447
452 template <typename T>
453 class UserItem : public Item
454 {
455 public:
456 // Inherited
457 virtual bool Parse (const std::string value);
458 bool HasDefault () const;
459 std::string GetDefault () const;
460
462 std::string m_default;
463 }; // class UserItem
464
469 class StringItem : public Item
470 {
471 public:
472 // Inherited
473 bool Parse (const std::string value);
474 bool HasDefault (void) const;
475 std::string GetDefault (void) const;
476
477 std::string m_value;
478 }; // class StringItem
479
484 class CallbackItem : public Item
485 {
486 public:
487 // Inherited
488 bool HasDefault (void) const;
489 std::string GetDefault (void) const;
490
497 virtual bool Parse (const std::string value);
499 std::string m_default;
500 }; // class CallbackItem
501
502
509 bool HandleOption (const std::string & param) const;
510
517 bool HandleNonOption (const std::string &value);
518
526 void HandleArgument (const std::string &name, const std::string &value) const;
534 static bool HandleAttribute (const std::string name, const std::string value);
535
540 void PrintGlobals (std::ostream &os) const;
548 void PrintAttributes (std::ostream &os, const std::string &type) const;
556 void PrintAttributeList (std::ostream &os, const TypeId tid, std::stringstream & header) const;
563 void PrintGroup (std::ostream &os, const std::string &group) const;
569 void PrintTypeIds (std::ostream &os) const;
575 void PrintGroups (std::ostream &os) const;
581 void Copy (const CommandLine &cmd);
583 void Clear (void);
589 void PrintDoxygenUsage (void) const;
590
591 typedef std::vector<Item *> Items;
594 std::size_t m_NNonOptions;
595 std::size_t m_nonOptionCount;
596 std::string m_usage;
597 std::string m_shortName;
599}; // class CommandLine
600
601
609namespace CommandLineHelper {
610
620template <typename T>
621bool UserItemParse (const std::string value, T & val);
629template <>
630bool UserItemParse<bool> (const std::string value, bool & val);
639template <>
640bool UserItemParse<uint8_t> (const std::string value, uint8_t & val);
641
651template <typename T> std::string GetDefault (const T & val);
652template <> std::string GetDefault<bool> (const bool & val);
653template <> std::string GetDefault<Time> (const Time & val);
656} // namespace CommandLineHelper
657
658
659} // namespace ns3
660
661
662/********************************************************************
663 * Implementation of the templates declared above.
664 ********************************************************************/
665
666namespace ns3 {
667
668template <typename T>
669void
670CommandLine::AddValue (const std::string &name,
671 const std::string &help,
672 T &value)
673{
674 UserItem<T> *item = new UserItem<T> ();
675 item->m_name = name;
676 item->m_help = help;
677 item->m_valuePtr = &value;
678
679 std::stringstream ss;
680 ss << value;
681 ss >> item->m_default;
682
683 m_options.push_back (item);
684}
685
686template <typename T>
687void
688CommandLine::AddNonOption (const std::string name,
689 const std::string help,
690 T & value)
691{
692 UserItem<T> *item = new UserItem<T> ();
693 item->m_name = name;
694 item->m_help = help;
695 item->m_valuePtr = &value;
696
697 std::stringstream ss;
698 ss << value;
699 ss >> item->m_default;
700 m_nonOptions.push_back (item);
702
703}
704
705template <typename T>
706bool
708{
709 return (m_default.size () > 0);
710}
711
712template <typename T>
713std::string
715{
716 return CommandLineHelper::GetDefault<T> (*m_valuePtr);
717}
718
719template <typename T>
720std::string
722{
723 std::ostringstream oss;
724 oss << val;
725 return oss.str ();
726}
727
728template <typename T>
729bool
730CommandLine::UserItem<T>::Parse (const std::string value)
731{
732 return CommandLineHelper::UserItemParse<T> (value, *m_valuePtr);
733}
734
735template <typename T>
736bool
737CommandLineHelper::UserItemParse (const std::string value, T & val)
738{
739 std::istringstream iss;
740 iss.str (value);
741 iss >> val;
742 return !iss.bad () && !iss.fail ();
743}
744
764std::ostream & operator << (std::ostream & os, const CommandLine & cmd);
765
766} // namespace ns3
767
768#endif /* COMMAND_LINE_H */
Declaration of the various callback functions.
Callback template class.
Definition: callback.h:1279
An argument Item using a Callback to parse the input.
Definition: command-line.h:485
std::string GetDefault(void) const
ns3::Callback< bool, std::string > m_callback
The Callback.
Definition: command-line.h:498
virtual bool Parse(const std::string value)
Parse from a string.
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:499
The argument abstract base class.
Definition: command-line.h:426
virtual ~Item()
Destructor.
virtual bool Parse(const std::string value)=0
Parse from a string.
virtual bool HasDefault() const
virtual std::string GetDefault() const =0
std::string m_name
Argument label: --m_name=...
Definition: command-line.h:428
std::string m_help
Argument help string.
Definition: command-line.h:429
Extension of Item for strings.
Definition: command-line.h:470
bool Parse(const std::string value)
Parse from a string.
std::string m_value
The argument value.
Definition: command-line.h:477
std::string GetDefault(void) const
An argument Item assigning to POD.
Definition: command-line.h:454
std::string m_default
String representation of default value.
Definition: command-line.h:462
virtual bool Parse(const std::string value)
Parse from a string.
Definition: command-line.h:730
std::string GetDefault() const
Definition: command-line.h:714
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:461
Parse command-line arguments.
Definition: command-line.h:229
void PrintAttributeList(std::ostream &os, const TypeId tid, std::stringstream &header) const
Print the Attributes for a single type.
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
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:595
~CommandLine()
Destructor.
std::string GetName() const
Get the program name.
Items m_options
The list of option arguments.
Definition: command-line.h:592
bool HandleNonOption(const std::string &value)
Handle a non-option.
void AddNonOption(const std::string name, const std::string help, T &value)
Add a non-option argument, assigning to POD.
Definition: command-line.h:688
std::vector< Item * > Items
Argument list container.
Definition: command-line.h:591
void Parse(int argc, char *argv[])
Parse the program arguments.
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
void Copy(const CommandLine &cmd)
Copy constructor.
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:594
void Clear(void)
Remove all arguments, Usage(), name.
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:593
void PrintVersion(std::ostream &os) const
Print ns-3 version to the desired output stream.
std::string m_shortName
The source file name (without .cc), as would be given to ns3 run
Definition: command-line.h:597
void Usage(const std::string usage)
Supply the program usage and documentation.
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:596
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type as well as its parents.
CommandLine(void)
Constructor.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:670
std::size_t GetNExtraNonOptions(void) const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
std::string GetVersion() const
Get the program version.
CommandLine & operator=(const CommandLine &cmd)
Assignment.
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
void PrintDoxygenUsage(void) const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
a unique identifier for an interface.
Definition: type-id.h:59
std::string GetDefault< Time >(const Time &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
bool UserItemParse(const std::string value, T &val)
Helpers to specialize CommandLine::UserItem::Parse()
Definition: command-line.h:737
std::string GetDefault(const T &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
Definition: command-line.h:721
std::string GetDefault< bool >(const bool &val)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
bool UserItemParse< bool >(const std::string value, bool &val)
Specialization of CommandLine::UserItem to bool.
bool UserItemParse< uint8_t >(const std::string value, uint8_t &val)
Specialization of CommandLine::UserItem to uint8_t to distinguish from char.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
cmd
Definition: second.py:43
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TypeId declaration; inline and template implementations.