-
Notifications
You must be signed in to change notification settings - Fork 2
/
Formatter.d
249 lines (200 loc) · 5.22 KB
/
Formatter.d
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
/**
* Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
* Authors: Jacob Carlborg
* Version: Initial created: Aug 1, 2012
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*/
module mambo.arguments.Formatter;
import mambo.arguments.Arguments;
import mambo.arguments.Options;
import mambo.core._;
import mambo.text.Inflections;
abstract class Formatter
{
protected
{
string appName_;
string appVersion_;
}
@property static Formatter instance (Arguments arguments)
{
return new DefaultFormatter(arguments);
}
@property string appName ()
{
return appName_;
}
@property string appName (string value)
{
return appName_ = value;
}
@property string appVersion ()
{
return appVersion_;
}
@property string appVersion (string value)
{
return appVersion_ = value;
}
abstract @property string helpText ();
abstract string errors (char[] delegate (char[] buffer, const(char)[] format, ...) dg);
}
class DefaultFormatter : Formatter
{
private
{
Arguments arguments;
ArgumentBase[] positionalArguments_;
Option!(int)[] options_;
enum indentation = " ";
string[] errorMessages_ = defaultErrorMessages;
enum defaultErrorMessages = [
"argument '{0}' expects {2} parameter(s) but has {1}\n",
"argument '{0}' expects {3} parameter(s) but has {1}\n",
"argument '{0}' is missing\n",
"argument '{0}' requires '{4}'\n",
"argument '{0}' conflicts with '{4}'\n",
"unexpected argument '{0}'\n",
"argument '{0}' expects one of {5}\n",
"invalid parameter for argument '{0}': {4}\n",
];
}
this (Arguments arguments)
{
this.arguments = arguments;
}
override string errors (char[] delegate (char[] buffer, const(char)[] format, ...) dg)
{
auto result = arguments.errors(dg).assumeUnique;
char[256] buffer;
auto msg = errorMessages;
auto posArgs = arguments.positionalArguments;
posArgs.sort!((a, b) => a.position < b.position)();
foreach (arg ; posArgs)
{
if (arg.error)
result ~= dg(buffer, msg[arg.error - 1], arg.name, arg.rawValues.length,
arg.min, arg.max);
}
return result;
}
@property string[] errorMessages ()
{
return errorMessages_;
}
@property string[] errorMessages (string[] errors)
in
{
assert(errors.length == defaultErrorMessages.length);
}
body
{
return errorMessages_ = errors;
}
override @property string helpText ()
{
string help = header;
if (positionalArguments.any)
help ~= "\n\n" ~ positionalArgumentsText;
if (options.any)
help ~= "\n\n" ~ optionsText;
help ~= '\n' ~ footer;
return help;
}
private:
@property string optionsText ()
{
string help = "Option:\n";
const optionNames = generateOptionNames();
immutable len = lengthOfLongestOption(optionNames);
enum numberOfIndentations = 1;
assert(options.length == optionNames.length);
foreach (i, option ; options)
{
auto text = option.helpText ~ '.';
auto name = optionNames[i];
if (option.name.count == 0 && shortOption(option) == char.init)
help ~= format("{}\n", text);
else if (shortOption(option) == char.init)
help ~= format("{}--{}{}{}{}\n",
indentation ~ indentation,
name,
" ".repeat(len - name.count),
indentation.repeat(numberOfIndentations),
text);
else
help ~= format("{}-{}, --{}{}{}{}\n",
indentation,
shortOption(option),
name,
" ".repeat(len - name.count),
indentation.repeat(numberOfIndentations),
text);
}
return help;
}
@property Option!(int)[] options ()
{
return options_ = options_.any ? options_ : arguments.options;
}
@property char shortOption (Option!(int) option)
{
return option.aliases.any ? option.aliases[0] : char.init;
}
@property size_t lengthOfLongestOption (const string[] names)
{
return names.reduce!((a, b) => a.count > b.count ? a : b).count;
}
@property ArgumentBase[] positionalArguments ()
{
return positionalArguments_.any ? positionalArguments_ : (positionalArguments_ = arguments.positionalArguments);
}
@property string header ()
{
string str = "Usage: " ~ appName;
if (options.any)
str ~= " [options]";
if (positionalArguments.any)
str ~= format(" <{}>", "arg".pluralize(positionalArguments.length));
str ~= "\nVersion " ~ appVersion;
return str;
}
@property string footer ()
{
return "Use the `-h' flag for help.";
}
@property string positionalArgumentsText ()
{
immutable len = lengthOfLongestPositionalArgument;
auto str = "Positional Arguments:\n";
enum numberOfIndentations = 1;
foreach (arg ; positionalArguments)
{
immutable text = arg.helpText ~ '.';
str ~= format("{}{}{}{}{}\n",
indentation,
arg.name,
" ".repeat(len - arg.name.count),
indentation.repeat(numberOfIndentations),
text);
}
return str;
}
@property size_t lengthOfLongestPositionalArgument ()
{
return positionalArguments.reduce!((a, b) => a.name.count > b.name.count ? a : b).name.count;
}
string[] generateOptionNames ()
{
return options.map!((option) {
string name = option.name;
if (option.min == 1)
name ~= " <arg>";
else if (option.min > 1)
name ~= " <arg0>";
if (option.max > 1)
name ~= " .. <arg" ~ option.max.toString ~ '>';
return name;
}).toArray;
}
}