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 |
|
raj@439
|
27 |
namespace ns3 {
|
raj@439
|
28 |
|
mathieu@2584
|
29 |
/**
|
mathieu@2584
|
30 |
* \brief parse command-line arguments
|
mathieu@3190
|
31 |
* \ingroup core
|
mathieu@2584
|
32 |
*
|
mathieu@2584
|
33 |
* Instances of this class can be used to parse command-line
|
mathieu@2584
|
34 |
* arguments: users can register new arguments with
|
mathieu@2584
|
35 |
* CommandLine::AddValue but the most important functionality
|
mathieu@2584
|
36 |
* provided by this class is that it can be used to set the
|
mathieu@2584
|
37 |
* 'initial value' of every attribute in the system with the
|
mathieu@2584
|
38 |
* --TypeIdName::AttributeName=value syntax and it can be used
|
mathieu@2584
|
39 |
* to set the value of every GlobalValue in the system with
|
mathieu@2584
|
40 |
* the --GlobalValueName=value syntax.
|
mathieu@2584
|
41 |
*/
|
raj@439
|
42 |
class CommandLine
|
raj@439
|
43 |
{
|
raj@439
|
44 |
public:
|
mathieu@2575
|
45 |
~CommandLine ();
|
mathieu@2575
|
46 |
|
mathieu@2584
|
47 |
/**
|
mathieu@2584
|
48 |
* \param name the name of the user-supplied argument
|
mathieu@2584
|
49 |
* \param help some help text used by --PrintHelp
|
mathieu@2584
|
50 |
* \param value a reference to the variable where the
|
mathieu@2584
|
51 |
* value parsed will be stored (if no value
|
mathieu@2584
|
52 |
* is parsed, this variable is not modified).
|
mathieu@2584
|
53 |
*/
|
raj@439
|
54 |
template <typename T>
|
mathieu@2575
|
55 |
void AddValue (const std::string &name,
|
mathieu@2575
|
56 |
const std::string &help,
|
mathieu@2575
|
57 |
T &value);
|
mathieu@2575
|
58 |
|
mathieu@2584
|
59 |
/**
|
mathieu@2584
|
60 |
* \param argc the 'argc' variable: number of arguments (including the
|
mathieu@2584
|
61 |
* main program name as first element).
|
mathieu@2584
|
62 |
* \param argv the 'argv' variable: a null-terminated array of strings,
|
mathieu@2584
|
63 |
* each of which identifies a command-line argument.
|
mathieu@2584
|
64 |
*
|
mathieu@2584
|
65 |
* Obviously, this method will parse the input command-line arguments and
|
mathieu@2584
|
66 |
* will attempt to handle them all.
|
mathieu@2584
|
67 |
*/
|
mathieu@2915
|
68 |
void Parse (int argc, char *argv[]) const;
|
mathieu@2575
|
69 |
private:
|
mathieu@2575
|
70 |
class Item
|
raj@439
|
71 |
{
|
raj@439
|
72 |
public:
|
mathieu@2575
|
73 |
std::string m_name;
|
mathieu@2575
|
74 |
std::string m_help;
|
mathieu@2575
|
75 |
virtual ~Item ();
|
mathieu@2575
|
76 |
virtual bool Parse (std::string value) = 0;
|
mathieu@2575
|
77 |
};
|
mathieu@2575
|
78 |
template <typename T>
|
mathieu@2575
|
79 |
class UserItem : public Item
|
mathieu@2575
|
80 |
{
|
mathieu@2575
|
81 |
public:
|
mathieu@2575
|
82 |
virtual bool Parse (std::string value);
|
raj@439
|
83 |
T *m_valuePtr;
|
raj@439
|
84 |
};
|
mathieu@2575
|
85 |
void HandleArgument (std::string name, std::string value) const;
|
mathieu@2575
|
86 |
void PrintHelp (void) const;
|
mathieu@2575
|
87 |
void PrintGlobals (void) const;
|
mathieu@2575
|
88 |
void PrintAttributes (std::string type) const;
|
mathieu@2575
|
89 |
void PrintGroup (std::string group) const;
|
mathieu@2575
|
90 |
void PrintTypeIds (void) const;
|
mathieu@2575
|
91 |
void PrintGroups (void) const;
|
gjc@1703
|
92 |
|
mathieu@2575
|
93 |
typedef std::list<Item *> Items;
|
mathieu@2575
|
94 |
Items m_items;
|
raj@439
|
95 |
};
|
raj@439
|
96 |
|
mathieu@2575
|
97 |
} // namespace ns3
|
raj@439
|
98 |
|
raj@439
|
99 |
namespace ns3 {
|
raj@439
|
100 |
|
raj@439
|
101 |
template <typename T>
|
raj@439
|
102 |
void
|
mathieu@2575
|
103 |
CommandLine::AddValue (const std::string &name,
|
mathieu@2575
|
104 |
const std::string &help,
|
mathieu@2575
|
105 |
T &value)
|
raj@439
|
106 |
{
|
mathieu@2575
|
107 |
UserItem<T> *item = new UserItem<T> ();
|
mathieu@2575
|
108 |
item->m_name = name;
|
mathieu@2575
|
109 |
item->m_help = help;
|
mathieu@2575
|
110 |
item->m_valuePtr = &value;
|
mathieu@2575
|
111 |
m_items.push_back (item);
|
raj@439
|
112 |
}
|
raj@439
|
113 |
|
raj@439
|
114 |
template <typename T>
|
mathieu@2575
|
115 |
bool
|
mathieu@2575
|
116 |
CommandLine::UserItem<T>::Parse (std::string value)
|
raj@439
|
117 |
{
|
raj@441
|
118 |
std::istringstream iss;
|
raj@439
|
119 |
iss.str (value);
|
mathieu@2575
|
120 |
iss >> (*m_valuePtr);
|
mathieu@2575
|
121 |
return !iss.bad () && !iss.fail ();
|
raj@439
|
122 |
}
|
raj@439
|
123 |
|
mathieu@2575
|
124 |
} // namespace ns3
|
raj@439
|
125 |
|
mathieu@2581
|
126 |
#endif /* COMMAND_LINE_H */
|