3 var a, group, parser, helptext;
5 var assert = require('assert');
8 var print = function () {
9 return console.log.apply(console, arguments);
11 // print = function () {};
13 var argparse = require('argparse');
15 print("TEST argparse.ArgumentDefaultsHelpFormatter");
17 parser = new argparse.ArgumentParser({
19 formatterClass: argparse.ArgumentDefaultsHelpFormatter,
20 description: 'description'
23 parser.addArgument(['--foo'], {
24 help: 'foo help - oh and by the way, %(defaultValue)s'
27 parser.addArgument(['--bar'], {
32 parser.addArgument(['spam'], {
36 parser.addArgument(['badger'], {
38 defaultValue: 'wooden',
42 group = parser.addArgumentGroup({
44 description: 'group description'
47 group.addArgument(['--baz'], {
53 helptext = parser.formatHelp();
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));
63 usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]
69 badger badger help (default: wooden)
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)
79 --baz BAZ baz help (default: 42)
82 print("TEST argparse.RawDescriptionHelpFormatter");
84 parser = new argparse.ArgumentParser({
87 formatterClass: argparse.RawDescriptionHelpFormatter,
88 description: 'Keep the formatting\n' +
89 ' exactly as it is written\n' +
94 a = parser.addArgument(['--foo'], {
95 help: ' foo help should not\n' +
96 ' retain this odd formatting'
99 parser.addArgument(['spam'], {
103 group = parser.addArgumentGroup({
105 description: ' This text\n' +
106 ' should be indented\n' +
107 ' exactly like it is here\n'
110 group.addArgument(['--bar'], {
114 helptext = parser.formatHelp();
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/));
122 class TestHelpRawDescription(HelpTestCase):
123 """Test the RawTextHelpFormatter"""
126 usage: PROG [-h] [--foo FOO] [--bar BAR] spam
129 exactly as it is written
133 positional arguments:
137 -h, --help show this help message and exit
138 --foo FOO foo help should not retain this odd formatting
143 exactly like it is here
149 print("TEST argparse.RawTextHelpFormatter");
151 parser = new argparse.ArgumentParser({
154 formatterClass: argparse.RawTextHelpFormatter,
155 description: 'Keep the formatting\n' +
156 ' exactly as it is written\n' +
161 parser.addArgument(['--baz'], {
162 help: ' baz help should also\n' +
163 'appear as given here'
166 a = parser.addArgument(['--foo'], {
167 help: ' foo help should also\n' +
168 'appear as given here'
171 parser.addArgument(['spam'], {
175 group = parser.addArgumentGroup({
177 description: ' This text\n' +
178 ' should be indented\n' +
179 ' exactly like it is here\n'
182 group.addArgument(['--bar'], {
186 helptext = parser.formatHelp();
188 // test selected clips
189 assert(helptext.match(parser.description));
190 assert(helptext.match(/( {14})appear as given here/gm));
193 class TestHelpRawText(HelpTestCase):
194 """Test the RawTextHelpFormatter"""
196 usage: PROG [-h] [--foo FOO] [--bar BAR] spam
199 exactly as it is written
203 positional arguments:
207 -h, --help show this help message and exit
208 --foo FOO foo help should also
214 exactly like it is here
220 print("TEST metavar as a tuple");
222 parser = new argparse.ArgumentParser({
226 parser.addArgument(['-w'], {
229 metavar: ['W1', 'W2']
232 parser.addArgument(['-x'], {
235 metavar: ['X1', 'X2']
238 parser.addArgument(['-y'], {
241 metavar: ['Y1', 'Y2', 'Y3']
244 parser.addArgument(['-z'], {
250 helptext = parser.formatHelp();
252 var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';
253 ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
255 assert(helptext.match(new RegExp(ustring)));
258 class TestHelpTupleMetavar(HelpTestCase):
259 """Test specifying metavar as a tuple"""
261 usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]
264 -h, --help show this help message and exit