Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
'use strict';
2
3
var a, group, parser, helptext;
4
5
var assert = require('assert');
6
7
8
var print = function () {
9
return console.log.apply(console, arguments);
10
};
11
// print = function () {};
12
13
var argparse = require('argparse');
14
15
print("TEST argparse.ArgumentDefaultsHelpFormatter");
16
17
parser = new argparse.ArgumentParser({
18
debug: true,
19
formatterClass: argparse.ArgumentDefaultsHelpFormatter,
20
description: 'description'
21
});
22
23
parser.addArgument(['--foo'], {
24
help: 'foo help - oh and by the way, %(defaultValue)s'
25
});
26
27
parser.addArgument(['--bar'], {
28
action: 'storeTrue',
29
help: 'bar help'
30
});
31
32
parser.addArgument(['spam'], {
33
help: 'spam help'
34
});
35
36
parser.addArgument(['badger'], {
37
nargs: '?',
38
defaultValue: 'wooden',
39
help: 'badger help'
40
});
41
42
group = parser.addArgumentGroup({
43
title: 'title',
44
description: 'group description'
45
});
46
47
group.addArgument(['--baz'], {
48
type: 'int',
49
defaultValue: 42,
50
help: 'baz help'
51
});
52
53
helptext = parser.formatHelp();
54
print(helptext);
55
// test selected clips
56
assert(helptext.match(/badger help \(default: wooden\)/));
57
assert(helptext.match(/foo help - oh and by the way, null/));
58
assert(helptext.match(/bar help \(default: false\)/));
59
assert(helptext.match(/title:\n {2}group description/)); // test indent
60
assert(helptext.match(/baz help \(default: 42\)/im));
61
62
/*
63
usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
64
65
description
66
67
positional arguments:
68
spam spam help
69
badger badger help (default: wooden)
70
71
optional arguments:
72
-h, --help show this help message and exit
73
--foo FOO foo help - oh and by the way, null
74
--bar bar help (default: false)
75
76
title:
77
group description
78
79
--baz BAZ baz help (default: 42)
80
*/
81
82
print("TEST argparse.RawDescriptionHelpFormatter");
83
84
parser = new argparse.ArgumentParser({
85
debug: true,
86
prog: 'PROG',
87
formatterClass: argparse.RawDescriptionHelpFormatter,
88
description: 'Keep the formatting\n' +
89
' exactly as it is written\n' +
90
'\n' +
91
'here\n'
92
});
93
94
a = parser.addArgument(['--foo'], {
95
help: ' foo help should not\n' +
96
' retain this odd formatting'
97
});
98
99
parser.addArgument(['spam'], {
100
'help': 'spam help'
101
});
102
103
group = parser.addArgumentGroup({
104
title: 'title',
105
description: ' This text\n' +
106
' should be indented\n' +
107
' exactly like it is here\n'
108
});
109
110
group.addArgument(['--bar'], {
111
help: 'bar help'
112
});
113
114
helptext = parser.formatHelp();
115
print(helptext);
116
// test selected clips
117
assert(helptext.match(parser.description));
118
assert.equal(helptext.match(a.help), null);
119
assert(helptext.match(/foo help should not retain this odd formatting/));
120
121
/*
122
class TestHelpRawDescription(HelpTestCase):
123
"""Test the RawTextHelpFormatter"""
124
....
125
126
usage: PROG [-h] [--foo FOO] [--bar BAR] spam
127
128
Keep the formatting
129
exactly as it is written
130
131
here
132
133
positional arguments:
134
spam spam help
135
136
optional arguments:
137
-h, --help show this help message and exit
138
--foo FOO foo help should not retain this odd formatting
139
140
title:
141
This text
142
should be indented
143
exactly like it is here
144
145
--bar BAR bar help
146
*/
147
148
149
print("TEST argparse.RawTextHelpFormatter");
150
151
parser = new argparse.ArgumentParser({
152
debug: true,
153
prog: 'PROG',
154
formatterClass: argparse.RawTextHelpFormatter,
155
description: 'Keep the formatting\n' +
156
' exactly as it is written\n' +
157
'\n' +
158
'here\n'
159
});
160
161
parser.addArgument(['--baz'], {
162
help: ' baz help should also\n' +
163
'appear as given here'
164
});
165
166
a = parser.addArgument(['--foo'], {
167
help: ' foo help should also\n' +
168
'appear as given here'
169
});
170
171
parser.addArgument(['spam'], {
172
'help': 'spam help'
173
});
174
175
group = parser.addArgumentGroup({
176
title: 'title',
177
description: ' This text\n' +
178
' should be indented\n' +
179
' exactly like it is here\n'
180
});
181
182
group.addArgument(['--bar'], {
183
help: 'bar help'
184
});
185
186
helptext = parser.formatHelp();
187
print(helptext);
188
// test selected clips
189
assert(helptext.match(parser.description));
190
assert(helptext.match(/( {14})appear as given here/gm));
191
192
/*
193
class TestHelpRawText(HelpTestCase):
194
"""Test the RawTextHelpFormatter"""
195
196
usage: PROG [-h] [--foo FOO] [--bar BAR] spam
197
198
Keep the formatting
199
exactly as it is written
200
201
here
202
203
positional arguments:
204
spam spam help
205
206
optional arguments:
207
-h, --help show this help message and exit
208
--foo FOO foo help should also
209
appear as given here
210
211
title:
212
This text
213
should be indented
214
exactly like it is here
215
216
--bar BAR bar help
217
*/
218
219
220
print("TEST metavar as a tuple");
221
222
parser = new argparse.ArgumentParser({
223
prog: 'PROG'
224
});
225
226
parser.addArgument(['-w'], {
227
help: 'w',
228
nargs: '+',
229
metavar: ['W1', 'W2']
230
});
231
232
parser.addArgument(['-x'], {
233
help: 'x',
234
nargs: '*',
235
metavar: ['X1', 'X2']
236
});
237
238
parser.addArgument(['-y'], {
239
help: 'y',
240
nargs: 3,
241
metavar: ['Y1', 'Y2', 'Y3']
242
});
243
244
parser.addArgument(['-z'], {
245
help: 'z',
246
nargs: '?',
247
metavar: ['Z1']
248
});
249
250
helptext = parser.formatHelp();
251
print(helptext);
252
var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';
253
ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
254
// print(ustring)
255
assert(helptext.match(new RegExp(ustring)));
256
257
/*
258
class TestHelpTupleMetavar(HelpTestCase):
259
"""Test specifying metavar as a tuple"""
260
261
usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]
262
263
optional arguments:
264
-h, --help show this help message and exit
265
-w W1 [W2 ...] w
266
-x [X1 [X2 ...]] x
267
-y Y1 Y2 Y3 y
268
-z [Z1] z
269
*/
270
271
272