forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_description.cpp
242 lines (204 loc) · 6.56 KB
/
options_description.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* MEGAHIT
* Copyright (C) 2014 The University of Hong Kong
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file options_description.cpp
* @brief
* @author Yu Peng ([email protected])
* @version 1.0.0
* @date 2011-08-07
*/
#include "options_description.h"
#include <getopt.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
using namespace std;
ostream &operator <<(ostream &os, const OptionsDescription &desc)
{
for (unsigned i = 0; i < desc.options.size(); ++i)
os << desc.options[i] << endl;
return os;
}
ostream &operator <<(ostream &os, const OptionsDescription::Option &option)
{
return os << string(option);
}
void OptionsDescription::Parse(int &argc, char *argv[])
{
struct option long_options[options.size()+1];
string short_options;
for (unsigned i = 0; i < options.size(); ++i)
{
long_options[i].name = options[i].long_name.c_str();
long_options[i].flag = 0;
long_options[i].val =
(options[i].short_name != "" ? options[i].short_name[0] : 0);
if (options[i].type == kOptionBool)
{
if (options[i].short_name != "")
short_options += options[i].short_name;
long_options[i].has_arg = no_argument;
}
else
{
if (options[i].short_name != "")
short_options += options[i].short_name + ":";
long_options[i].has_arg = required_argument;
}
}
long_options[options.size()].name = 0;
long_options[options.size()].has_arg = 0;
long_options[options.size()].flag = 0;
long_options[options.size()].val = 0;
while (true)
{
int index = -1;
int ch = getopt_long(argc, argv, short_options.c_str(), long_options, &index);
if (ch == -1)
break;
if (ch == '?')
throw logic_error("uknown option");
//throw exception();
if (ch != 0)
{
string s;
s += ch;
for (unsigned i = 0; i < options.size(); ++i)
{
if (options[i].short_name == s)
{
index = i;
break;
}
}
}
if (options[index].type == kOptionBool)
options[index].value = "true";
else
options[index].value = optarg;
}
int index = 1;
for (int i = optind; i < argc; ++i)
argv[index++] = argv[i];
argc = index;
for (unsigned i = 0; i < options.size(); ++i)
options[i].Parse();
}
OptionsDescription::operator string() const
{
stringstream ss;
for (unsigned i = 0; i < options.size(); ++i)
ss << options[i] << endl;
return ss.str();
}
void OptionsDescription::AddOption(const std::string &long_name, const std::string &short_name,
bool &bool_option, const std::string &description)
{
AddOption(long_name, short_name, &bool_option, kOptionBool, description, "");
}
void OptionsDescription::AddOption(const std::string &long_name, const std::string &short_name,
int &int_option, const std::string &description)
{
stringstream ss;
ss << int_option;
AddOption(long_name, short_name, &int_option, kOptionInt, description, ss.str());
}
void OptionsDescription::AddOption(const std::string &long_name, const std::string &short_name,
double &double_option, const std::string &description)
{
stringstream ss;
ss << double_option;
AddOption(long_name, short_name, &double_option, kOptionDouble, description, ss.str());
}
void OptionsDescription::AddOption(const std::string &long_name, const std::string &short_name,
std::string &string_option, const std::string &description)
{
AddOption(long_name, short_name, &string_option, kOptionString, description, string_option);
}
void OptionsDescription::AddOption(const string &long_name, const string &short_name,
void *pointer, OptionType type, const string &description, const string &default_value)
{
for (unsigned i = 0; i < options.size(); ++i)
{
if (options[i].long_name == long_name
|| (short_name != "" && options[i].short_name == short_name))
{
throw logic_error("option already exist: " + long_name + ", " + short_name);
}
}
options.push_back(Option(long_name, short_name, pointer, type, description, default_value));
}
OptionsDescription::Option::Option(const string &long_name, const string &short_name,
void *pointer, OptionType type, const string &description,
const string &default_value)
{
if (short_name.size() > 1 || long_name.size() < 2)
{
throw logic_error("invalid option: " + long_name + ", " + short_name);
}
this->long_name = long_name;
this->short_name = short_name;
this->pointer = pointer;
this->type = type;
this->description = description;
this->default_value = default_value;
}
void OptionsDescription::Option::Parse()
{
if (value == "")
value = default_value;
if (value != "")
{
switch (type)
{
case kOptionBool:
if (value != "" || value == "false")
*(bool *)pointer = true;
break;
case kOptionInt:
*(int *)pointer = atoi(value.c_str());
break;
case kOptionDouble:
*(double *)pointer = atof(value.c_str());
break;
case kOptionString:
*(string *)pointer = value;
}
}
}
OptionsDescription::Option::operator string() const
{
string s;
if (short_name != "")
s += " -" + short_name + ", ";
else
s += " ";
s += "--" + long_name;
if (type != kOptionBool)
s += " arg";
if (default_value != "")
s += " (=" + default_value + ")";
while (s.size() < 40)
s += " ";
s += " " + description;
return s;
}