react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / examples / testformatters.js
80713 views'use strict';12var a, group, parser, helptext;34var assert = require('assert');567var print = function () {8return console.log.apply(console, arguments);9};10// print = function () {};1112var argparse = require('argparse');1314print("TEST argparse.ArgumentDefaultsHelpFormatter");1516parser = new argparse.ArgumentParser({17debug: true,18formatterClass: argparse.ArgumentDefaultsHelpFormatter,19description: 'description'20});2122parser.addArgument(['--foo'], {23help: 'foo help - oh and by the way, %(defaultValue)s'24});2526parser.addArgument(['--bar'], {27action: 'storeTrue',28help: 'bar help'29});3031parser.addArgument(['spam'], {32help: 'spam help'33});3435parser.addArgument(['badger'], {36nargs: '?',37defaultValue: 'wooden',38help: 'badger help'39});4041group = parser.addArgumentGroup({42title: 'title',43description: 'group description'44});4546group.addArgument(['--baz'], {47type: 'int',48defaultValue: 42,49help: 'baz help'50});5152helptext = parser.formatHelp();53print(helptext);54// test selected clips55assert(helptext.match(/badger help \(default: wooden\)/));56assert(helptext.match(/foo help - oh and by the way, null/));57assert(helptext.match(/bar help \(default: false\)/));58assert(helptext.match(/title:\n {2}group description/)); // test indent59assert(helptext.match(/baz help \(default: 42\)/im));6061/*62usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger]6364description6566positional arguments:67spam spam help68badger badger help (default: wooden)6970optional arguments:71-h, --help show this help message and exit72--foo FOO foo help - oh and by the way, null73--bar bar help (default: false)7475title:76group description7778--baz BAZ baz help (default: 42)79*/8081print("TEST argparse.RawDescriptionHelpFormatter");8283parser = new argparse.ArgumentParser({84debug: true,85prog: 'PROG',86formatterClass: argparse.RawDescriptionHelpFormatter,87description: 'Keep the formatting\n' +88' exactly as it is written\n' +89'\n' +90'here\n'91});9293a = parser.addArgument(['--foo'], {94help: ' foo help should not\n' +95' retain this odd formatting'96});9798parser.addArgument(['spam'], {99'help': 'spam help'100});101102group = parser.addArgumentGroup({103title: 'title',104description: ' This text\n' +105' should be indented\n' +106' exactly like it is here\n'107});108109group.addArgument(['--bar'], {110help: 'bar help'111});112113helptext = parser.formatHelp();114print(helptext);115// test selected clips116assert(helptext.match(parser.description));117assert.equal(helptext.match(a.help), null);118assert(helptext.match(/foo help should not retain this odd formatting/));119120/*121class TestHelpRawDescription(HelpTestCase):122"""Test the RawTextHelpFormatter"""123....124125usage: PROG [-h] [--foo FOO] [--bar BAR] spam126127Keep the formatting128exactly as it is written129130here131132positional arguments:133spam spam help134135optional arguments:136-h, --help show this help message and exit137--foo FOO foo help should not retain this odd formatting138139title:140This text141should be indented142exactly like it is here143144--bar BAR bar help145*/146147148print("TEST argparse.RawTextHelpFormatter");149150parser = new argparse.ArgumentParser({151debug: true,152prog: 'PROG',153formatterClass: argparse.RawTextHelpFormatter,154description: 'Keep the formatting\n' +155' exactly as it is written\n' +156'\n' +157'here\n'158});159160parser.addArgument(['--baz'], {161help: ' baz help should also\n' +162'appear as given here'163});164165a = parser.addArgument(['--foo'], {166help: ' foo help should also\n' +167'appear as given here'168});169170parser.addArgument(['spam'], {171'help': 'spam help'172});173174group = parser.addArgumentGroup({175title: 'title',176description: ' This text\n' +177' should be indented\n' +178' exactly like it is here\n'179});180181group.addArgument(['--bar'], {182help: 'bar help'183});184185helptext = parser.formatHelp();186print(helptext);187// test selected clips188assert(helptext.match(parser.description));189assert(helptext.match(/( {14})appear as given here/gm));190191/*192class TestHelpRawText(HelpTestCase):193"""Test the RawTextHelpFormatter"""194195usage: PROG [-h] [--foo FOO] [--bar BAR] spam196197Keep the formatting198exactly as it is written199200here201202positional arguments:203spam spam help204205optional arguments:206-h, --help show this help message and exit207--foo FOO foo help should also208appear as given here209210title:211This text212should be indented213exactly like it is here214215--bar BAR bar help216*/217218219print("TEST metavar as a tuple");220221parser = new argparse.ArgumentParser({222prog: 'PROG'223});224225parser.addArgument(['-w'], {226help: 'w',227nargs: '+',228metavar: ['W1', 'W2']229});230231parser.addArgument(['-x'], {232help: 'x',233nargs: '*',234metavar: ['X1', 'X2']235});236237parser.addArgument(['-y'], {238help: 'y',239nargs: 3,240metavar: ['Y1', 'Y2', 'Y3']241});242243parser.addArgument(['-z'], {244help: 'z',245nargs: '?',246metavar: ['Z1']247});248249helptext = parser.formatHelp();250print(helptext);251var ustring = 'PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]';252ustring = ustring.replace(/\[/g, '\\[').replace(/\]/g, '\\]');253// print(ustring)254assert(helptext.match(new RegExp(ustring)));255256/*257class TestHelpTupleMetavar(HelpTestCase):258"""Test specifying metavar as a tuple"""259260usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] [-z [Z1]]261262optional arguments:263-h, --help show this help message and exit264-w W1 [W2 ...] w265-x [X1 [X2 ...]] x266-y Y1 Y2 Y3 y267-z [Z1] z268*/269270271272