usage.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var nomnom = require("../nomnom");
  2. function strip(str) {
  3. return str.replace(/\s+/g, '');
  4. };
  5. var opts = {
  6. apple: {
  7. abbr: 'a',
  8. help: 'how many apples'
  9. },
  10. banana: {
  11. full: "b-nana"
  12. },
  13. carrot: {
  14. string: '-c NUM, --carrots=NUM'
  15. },
  16. dill: {
  17. metavar: 'PICKLE'
  18. },
  19. egg: {
  20. position: 0,
  21. help: 'robin'
  22. }
  23. }
  24. var parser = nomnom().options(opts).help("all the best foods").scriptName("test").nocolors();
  25. var expected = "Usage:test[egg][options]eggrobinOptions:-a,--applehowmanyapples--b-nana-cNUM,--carrots=NUM--dillPICKLEallthebestfoods"
  26. exports.testH = function(test) {
  27. test.expect(1);
  28. parser.printer(function(string) {
  29. test.equal(strip(string), expected)
  30. test.done();
  31. })
  32. .nocolors()
  33. .parse(["-h"]);
  34. }
  35. exports.testHelp = function(test) {
  36. test.expect(1);
  37. parser.printer(function(string) {
  38. test.equal(strip(string), expected)
  39. test.done();
  40. })
  41. .nocolors()
  42. .parse(["--help"]);
  43. }
  44. exports.testScriptName = function(test) {
  45. test.expect(1);
  46. nomnom()
  47. .script("test")
  48. .printer(function(string) {
  49. test.equal(strip(string),"Usage:test")
  50. test.done();
  51. })
  52. .nocolors()
  53. .parse(["-h"]);
  54. }
  55. exports.testUsage = function(test) {
  56. test.expect(1);
  57. parser
  58. .usage("test usage")
  59. .printer(function(string) {
  60. test.equal(string, "test usage")
  61. test.done();
  62. })
  63. .nocolors()
  64. .parse(["--help"]);
  65. }
  66. exports.testHidden = function(test) {
  67. test.expect(1);
  68. nomnom().options({
  69. file: {
  70. hidden: true
  71. }
  72. })
  73. .scriptName("test")
  74. .printer(function(string) {
  75. test.equal(strip("Usage:test[options]Options:"), strip(string))
  76. test.done();
  77. })
  78. .nocolors()
  79. .parse(["-h"]);
  80. }
  81. exports.testRequiredOptional = function(test) {
  82. test.expect(1);
  83. nomnom().options({
  84. foo: {
  85. position: 0,
  86. required: true,
  87. help: 'The foo'
  88. },
  89. bar: {
  90. position: 1,
  91. help: 'The bar'
  92. }
  93. })
  94. .scriptName("test")
  95. .printer(function(string) {
  96. test.equal(strip("Usage:test<foo>[bar]fooThefoobarThebar"), strip(string))
  97. test.done();
  98. })
  99. .nocolors()
  100. .parse(["-h"]);
  101. }