-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationUsage.cpp
More file actions
293 lines (245 loc) · 9.34 KB
/
Copy pathApplicationUsage.cpp
File metadata and controls
293 lines (245 loc) · 9.34 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// ApplicationUsage.cpp — Singleton that collects and renders CLI usage/help information.
//
// Part of Sourcy — CNS -> Lua compiler for MUGEN-style characters.
// Licensed under the MIT License. See LICENSE in the project root.
//
// Author: Miguel Angel Exposito Sanchez (radexx), 2012.
#include "ApplicationUsage.h"
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <string>
#include <iostream>
#include <ostream>
ApplicationUsage::ApplicationUsage(const std::string& commandLineUsage):
_commandLineUsage(commandLineUsage)
{
}
ApplicationUsage* ApplicationUsage::instance()
{
static ApplicationUsage *s_applicationUsage = new ApplicationUsage;
return s_applicationUsage;
}
void ApplicationUsage::addUsageExplanation(Type type,const std::string& option,const std::string& explanation)
{
switch(type)
{
case(COMMAND_LINE_OPTION):
addCommandLineOption(option,explanation);
break;
case(ENVIRONMENTAL_VARIABLE):
addEnvironmentalVariable(option,explanation);
break;
case(KEYBOARD_MOUSE_BINDING):
addKeyboardMouseBinding(option,explanation);
break;
default:
break;
}
}
void ApplicationUsage::addCommandLineOption(const std::string& option,const std::string& explanation,const std::string& defaultValue)
{
_commandLineOptions[option]=explanation;
_commandLineOptionsDefaults[option]=defaultValue;
}
void ApplicationUsage::addEnvironmentalVariable(const std::string& option,const std::string& explanation, const std::string& defaultValue)
{
_environmentalVariables[option]=explanation;
_environmentalVariablesDefaults[option]=defaultValue;
}
void ApplicationUsage::addKeyboardMouseBinding(const std::string& option,const std::string& explanation)
{
_keyboardMouse[option]=explanation;
}
void ApplicationUsage::getFormattedString(std::string& str, const UsageMap& um,unsigned int widthOfOutput,bool showDefaults,const UsageMap& ud)
{
unsigned int maxNumCharsInOptions = 0;
ApplicationUsage::UsageMap::const_iterator citr;
for(citr=um.begin();
citr!=um.end();
++citr)
{
maxNumCharsInOptions = std::max(maxNumCharsInOptions,(unsigned int)citr->first.length());
}
unsigned int fullWidth = widthOfOutput;
unsigned int optionPos = 2;
unsigned int explanationPos = optionPos+maxNumCharsInOptions+2;
double ratioOfExplanationToOutputWidth = float(explanationPos)/float(widthOfOutput);
double maxRatioOfExplanationToOutputWidth = 0.25f;
if (ratioOfExplanationToOutputWidth > maxRatioOfExplanationToOutputWidth)
{
explanationPos = static_cast<unsigned int>(maxRatioOfExplanationToOutputWidth*float(widthOfOutput));
}
unsigned int defaultPos = 0;
if (showDefaults)
{
defaultPos = explanationPos;
explanationPos = optionPos+8;
}
unsigned int explanationWidth = fullWidth-explanationPos;
std::string line;
for(citr=um.begin();
citr!=um.end();
++citr)
{
line.assign(fullWidth,' ');
line.replace(optionPos,citr->first.length(),citr->first);
unsigned int currentEndPos = optionPos + citr->first.length();
if (showDefaults)
{
UsageMap::const_iterator ditr = ud.find(citr->first);
if (ditr != ud.end())
{
if (currentEndPos+1>=defaultPos)
{
str += line; str += "\n";
line.assign(fullWidth,' ');
}
line.replace(defaultPos, std::string::npos, "");
if (ditr->second != "")
{
line += "[";
line += ditr->second;
line += "]";
}
str += line;
str += "\n";
line.assign(fullWidth,' ');
currentEndPos = 0;
}
}
const std::string& explanation = citr->second;
std::string::size_type pos = 0;
std::string::size_type offset = 0;
bool firstInLine = true;
if (!explanation.empty())
{
if (currentEndPos+1>explanationPos)
{
str += line; str += "\n";
line.assign(fullWidth,' ');
}
while (pos<explanation.length())
{
if (firstInLine) offset = 0;
// skip any leading white space.
while (pos<explanation.length() && explanation[pos]==' ')
{
if (firstInLine) ++offset;
++pos;
}
firstInLine = false;
std::string::size_type width = std::min((std::string::size_type)(explanation.length()-pos),(std::string::size_type)(explanationWidth-offset));
std::string::size_type slashn_pos = explanation.find('\n',pos);
unsigned int extraSkip = 0;
bool concatinated = false;
if (slashn_pos!=std::string::npos)
{
if (slashn_pos<pos+width)
{
width = slashn_pos-pos;
++extraSkip;
firstInLine = true;
}
else if (slashn_pos==pos+width)
{
++extraSkip;
firstInLine = true;
}
}
if (pos+width<explanation.length())
{
// now reduce width until we get a space or a return
// so that we ensure that whole words are printed.
while (width>0 &&
explanation[pos+width]!=' ' &&
explanation[pos+width]!='\n') --width;
if (width==0)
{
// word must be longer than a whole line so will need
// to concatenate it.
width = explanationWidth-1;
concatinated = true;
}
}
line.replace(explanationPos+offset,explanationWidth, explanation, pos, width);
if (concatinated) { str += line; str += "-\n"; }
else { str += line; str += "\n"; }
// move to the next line of output.
line.assign(fullWidth,' ');
pos += width+extraSkip;
}
}
else
{
str += line; str += "\n";
}
}
}
void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::UsageMap& um,unsigned int widthOfOutput,bool showDefaults,const ApplicationUsage::UsageMap& ud)
{
std::string str;
getFormattedString(str, um, widthOfOutput, showDefaults, ud);
output << str << std::endl;
}
void ApplicationUsage::write(std::ostream& output, unsigned int type, unsigned int widthOfOutput, bool showDefaults)
{
output << "Usage: "<<getCommandLineUsage()<<std::endl;
bool needspace = false;
if ((type&COMMAND_LINE_OPTION) && !getCommandLineOptions().empty())
{
output << "Options";
if (showDefaults) output << " [and default value]";
output << ":"<<std::endl;
write(output,getCommandLineOptions(),widthOfOutput,showDefaults,getCommandLineOptionsDefaults());
needspace = true;
}
if ((type&ENVIRONMENTAL_VARIABLE) && !getEnvironmentalVariables().empty())
{
if (needspace) output << std::endl;
output << "Environmental Variables";
if (showDefaults) output << " [and default value]";
output << ":"<<std::endl;
write(output,getEnvironmentalVariables(),widthOfOutput,showDefaults,getEnvironmentalVariablesDefaults());
needspace = true;
}
if ((type&KEYBOARD_MOUSE_BINDING) && !getKeyboardMouseBindings().empty())
{
if (needspace) output << std::endl;
output << "Keyboard and Mouse Bindings:"<<std::endl;
write(output,getKeyboardMouseBindings(),widthOfOutput);
needspace = true;
}
}
void ApplicationUsage::writeEnvironmentSettings(std::ostream& output)
{
output << "Current Environment Settings:"<<std::endl;
unsigned int maxNumCharsInOptions = 0;
ApplicationUsage::UsageMap::const_iterator citr;
for(citr=getEnvironmentalVariables().begin();
citr!=getEnvironmentalVariables().end();
++citr)
{
std::string::size_type len = citr->first.find_first_of("\n\r\t ");
if (len == std::string::npos) len = citr->first.size();
maxNumCharsInOptions = std::max( maxNumCharsInOptions,static_cast<unsigned int>(len));
}
unsigned int optionPos = 2;
std::string line;
for(citr=getEnvironmentalVariables().begin();
citr!=getEnvironmentalVariables().end();
++citr)
{
line.assign(optionPos+maxNumCharsInOptions+2,' ');
std::string::size_type len = citr->first.find_first_of("\n\r\t ");
if (len == std::string::npos) len = citr->first.size();
line.replace(optionPos,len,citr->first.substr(0,len));
const char *cp = getenv(citr->first.substr(0, len).c_str());
if (!cp) cp = "[not set]";
else if (!*cp) cp = "[set]";
line += std::string(cp) + "\n";
output << line;
}
output << std::endl;
}