commands.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var nomnom = require("../nomnom");
  2. function strip(str) {
  3. return str.replace(/\s+/g, '');
  4. }
  5. exports.testCallback = function(test) {
  6. test.expect(1);
  7. var parser = nomnom();
  8. parser.command('run').callback(function(options) {
  9. test.equal(options.v, 3);
  10. });
  11. parser.command('other').callback(function() {
  12. test.ok(false, "callback for other command shouldn't be called");
  13. });
  14. parser.parse(["run","-v", "3"]);
  15. test.done();
  16. }
  17. exports.testMissingCommand = function(test) {
  18. test.expect(1);
  19. var parser = nomnom().scriptName("test");
  20. parser.command('run');
  21. parser.printer(function(string) {
  22. test.equal(string, "test: no such command 'other'");
  23. test.done();
  24. });
  25. parser.parse(["other"]);
  26. }
  27. exports.testNoCommand = function(test) {
  28. test.expect(2);
  29. var parser = nomnom();
  30. parser.nocommand()
  31. .options({
  32. version: {
  33. flag: true
  34. }
  35. })
  36. .callback(function(options) {
  37. test.strictEqual(options.version, true);
  38. })
  39. .usage("fallback usage");
  40. parser.command('run');
  41. var options = parser.parse(["--version"]);
  42. test.strictEqual(options.version, true);
  43. test.done();
  44. }
  45. function createParser() {
  46. var parser = nomnom().scriptName("test")
  47. .options({
  48. debug: {
  49. flag: true
  50. }
  51. });
  52. parser.command('run')
  53. .options({
  54. file: {
  55. help: 'file to run'
  56. }
  57. })
  58. .help("run all");
  59. parser.command('test').usage("test usage");
  60. parser.nocommand()
  61. .options({
  62. verbose: {
  63. flag: true
  64. }
  65. })
  66. .help("nocommand");
  67. return parser;
  68. }
  69. exports.testUsage = function(test) {
  70. test.expect(4);
  71. var parser = createParser();
  72. parser.printer(function(string) {
  73. test.equal(strip(string), "testusage");
  74. });
  75. parser.parse(["test", "-h"]);
  76. parser = createParser().nocolors();
  77. parser.printer(function(string) {
  78. test.equal(strip(string), "Usage:testrun[options]Options:--debug--filefiletorunrunall");
  79. });
  80. parser.parse(["run", "-h"]);
  81. parser = createParser().nocolors();
  82. parser.printer(function(string) {
  83. test.equal(strip(string), "Usage:test[command][options]commandoneof:run,testOptions:--debug--verbosenocommand");
  84. });
  85. parser.parse(["-h"]);
  86. parser = createParser().nocolors();
  87. parser.nocommand()
  88. .usage("fallback");
  89. parser.printer(function(string) {
  90. test.equal(strip(string), "fallback");
  91. });
  92. parser.parse(["-h"]);
  93. test.done();
  94. }