testformatters.js
5.58 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
'use strict';
var a, group, parser, helptext;
var assert = require('assert');
var _ = require('underscore');
_.str = require('underscore.string');
var print = function () {
return console.log.apply(console, arguments);
};
// print = function () {};
var argparse = require('argparse');
print("TEST argparse.ArgumentDefaultsHelpFormatter");
parser = new argparse.ArgumentParser({
debug: true,
formatterClass: argparse.ArgumentDefaultsHelpFormatter,
description: 'description'
});
parser.addArgument(['--foo'], {
help: 'foo help - oh and by the way, %(defaultValue)s'
});
parser.addArgument(['--bar'], {
action: 'storeTrue',
help: 'bar help'
});
parser.addArgument(['spam'], {
help: 'spam help'
});
parser.addArgument(['badger'], {
nargs: '?',
defaultValue: 'wooden',
help: 'badger help'
});
group = parser.addArgumentGroup({
title: 'title',
description: 'group description'
});
group.addArgument(['--baz'], {
type: 'int',
defaultValue: 42,
help: 'baz help'
});
helptext = parser.formatHelp();
print(helptext);
// test selected clips
assert(helptext.match(/badger help \(default: wooden\)/));
assert(helptext.match(/foo help - oh and by the way, null/));
assert(helptext.match(/bar help \(default: false\)/));
assert(helptext.match(/title:\n {2}group description/)); // test indent
assert(helptext.match(/baz help \(default: 42\)/im));
/*
usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
description
positional arguments:
spam spam help
badger badger help (default: wooden)
optional arguments:
-h, --help show this help message and exit
--foo FOO foo help - oh and by the way, null
--bar bar help (default: false)
title:
group description
--baz BAZ baz help (default: 42)
*/
print("TEST argparse.RawDescriptionHelpFormatter");
parser = new argparse.ArgumentParser({
debug: true,
prog: 'PROG',
formatterClass: argparse.RawDescriptionHelpFormatter,
description: 'Keep the formatting\n' +
' exactly as it is written\n' +
'\n' +
'here\n'
});
a = parser.addArgument(['--foo'], {
help: ' foo help should not\n' +
' retain this odd formatting'
});
parser.addArgument(['spam'], {
'help': 'spam help'
});
group = parser.addArgumentGroup({
title: 'title',
description: ' This text\n' +
' should be indented\n' +
' exactly like it is here\n'
});
group.addArgument(['--bar'], {
help: 'bar help'
});
helptext = parser.formatHelp();
print(helptext);
// test selected clips
assert(helptext.match(parser.description));
assert.equal(helptext.match(a.help), null);
assert(helptext.match(/foo help should not retain this odd formatting/));
/*
class TestHelpRawDescription(HelpTestCase):
"""Test the RawTextHelpFormatter"""
....
usage: PROG [-h] [--foo FOO] [--bar BAR] spam
Keep the formatting
exactly as it is written
here
positional arguments:
spam spam help
optional arguments:
-h, --help show this help message and exit
--foo FOO foo help should not retain this odd formatting
title:
This text
should be indented
exactly like it is here
--bar BAR bar help
*/
print("TEST argparse.RawTextHelpFormatter");
parser = new argparse.ArgumentParser({
debug: true,
prog: 'PROG',
formatterClass: argparse.RawTextHelpFormatter,
description: 'Keep the formatting\n' +
' exactly as it is written\n' +
'\n' +
'here\n'
});
parser.addArgument(['--baz'], {
help: ' baz help should also\n' +
'appear as given here'
});
a = parser.addArgument(['--foo'], {
help: ' foo help should also\n' +
'appear as given here'
});
parser.addArgument(['spam'], {
'help': 'spam help'
});
group = parser.addArgumentGroup({
title: 'title',
description: ' This text\n' +
' should be indented\n' +
' exactly like it is here\n'
});
group.addArgument(['--bar'], {
help: 'bar help'
});
helptext = parser.formatHelp();
print(helptext);
// test selected clips
assert(helptext.match(parser.description));
assert(helptext.match(/( {14})appear as given here/gm));
/*
class TestHelpRawText(HelpTestCase):
"""Test the RawTextHelpFormatter"""
usage: PROG [-h] [--foo FOO] [--bar BAR] spam
Keep the formatting
exactly as it is written
here
positional arguments:
spam spam help
optional arguments:
-h, --help show this help message and exit
--foo FOO foo help should also
appear as given here
title:
This text
should be indented
exactly like it is here
--bar BAR bar help
*/
print("TEST metavar as a tuple");
parser = new argparse.ArgumentParser({
prog: 'PROG'
});
parser.addArgument(['-w'], {
help: 'w',
nargs: '+',
metavar: ['W1', 'W2']
});
parser.addArgument(['-x'], {
help: 'x',
nargs: '*',
metavar: ['X1', 'X2']
});
parser.addArgument(['-y'], {
help: 'y',
nargs: 3,
metavar: ['Y1', 'Y2', 'Y3']
});
parser.addArgument(['-z'], {
help: 'z',
nargs: '?',
metavar: ['Z1']
});
helptext = parser.formatHelp();
print(helptext);
var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';
ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
// print(ustring)
assert(helptext.match(new RegExp(ustring)));
/*
class TestHelpTupleMetavar(HelpTestCase):
"""Test specifying metavar as a tuple"""
usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]
optional arguments:
-h, --help show this help message and exit
-w W1 [W2 ...] w
-x [X1 [X2 ...]] x
-y Y1 Y2 Y3 y
-z [Z1] z
*/