-
Notifications
You must be signed in to change notification settings - Fork 3
/
commandline.h
71 lines (58 loc) · 1.4 KB
/
commandline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Refer to CommandLine.h/cpp in NS3:
// ns3::CommandLine Class Reference
// Basic version
#include <list>
#include <string>
#include <algorithm>
#include <sstream>
#include <ostream>
namespace std
{
class CommandLine
{
public:
CommandLine (){}
~CommandLine ();
template <typename T>
void AddValue (const std::string &name, const std::string &help, T &value);
void Parse (int argc, char *argv[]);
void PrintHelp (std::ostream &os) const;
private:
class Item
{
public:
std::string m_name;
std::string m_help;
virtual ~Item (){}
virtual bool Parse (const std::string value) = 0;
};
template <typename T>
class UserItem : public Item
{
public:
T *m_valuePtr;
virtual bool Parse (const std::string value);
};
typedef std::list<Item *> Items;
Items m_items;
};
template <typename T>
void CommandLine::AddValue (const std::string &name, const std::string &help, T &value)
{
UserItem<T> *item = new UserItem<T> ();
item->m_name = name;
item->m_help = help;
item->m_valuePtr = &value;
m_items.push_back (item);
}
template <typename T>
bool CommandLine::UserItem<T>::Parse (const std::string value)
{
std::string src = value;
std::transform(src.begin(), src.end(), src.begin(), ::tolower);
std::istringstream iss;
iss.str (src);
iss >> (*m_valuePtr);
return !iss.bad () && !iss.fail ();
}
}