123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- '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
- */
|