Нема описа

testformatters.js 5.6KB

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