arg.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Generated by CoffeeScript 1.6.3
  2. var Arg, Cmd, Color, Opt;
  3. Color = require('./color').Color;
  4. Cmd = require('./cmd').Cmd;
  5. Opt = require('./opt').Opt;
  6. /**
  7. Argument
  8. Unnamed entity. From command line arguments passed as list of unnamed values.
  9. @namespace
  10. @class Presents argument
  11. */
  12. exports.Arg = Arg = (function() {
  13. /**
  14. @constructs
  15. @param {COA.Cmd} cmd parent command
  16. */
  17. function Arg(_cmd) {
  18. this._cmd = _cmd;
  19. this._cmd._args.push(this);
  20. }
  21. /**
  22. Set a canonical argument identifier to be used anywhere in text messages.
  23. @param {String} _name argument name
  24. @returns {COA.Arg} this instance (for chainability)
  25. */
  26. Arg.prototype.name = Opt.prototype.name;
  27. /**
  28. Set a long description for argument to be used anywhere in text messages.
  29. @param {String} _title argument title
  30. @returns {COA.Arg} this instance (for chainability)
  31. */
  32. Arg.prototype.title = Cmd.prototype.title;
  33. /**
  34. Makes an argument accepts multiple values.
  35. Otherwise, the value will be used by the latter passed.
  36. @returns {COA.Arg} this instance (for chainability)
  37. */
  38. Arg.prototype.arr = Opt.prototype.arr;
  39. /**
  40. Makes an argument required.
  41. @returns {COA.Arg} this instance (for chainability)
  42. */
  43. Arg.prototype.req = Opt.prototype.req;
  44. /**
  45. Set a validation (or value) function for argument.
  46. Value from command line passes through before becoming available from API.
  47. Using for validation and convertion simple types to any values.
  48. @param {Function} _val validating function,
  49. invoked in the context of argument instance
  50. and has one parameter with value from command line
  51. @returns {COA.Arg} this instance (for chainability)
  52. */
  53. Arg.prototype.val = Opt.prototype.val;
  54. /**
  55. Set a default value for argument.
  56. Default value passed through validation function as ordinary value.
  57. @param {Object} _def
  58. @returns {COA.Arg} this instance (for chainability)
  59. */
  60. Arg.prototype.def = Opt.prototype.def;
  61. /**
  62. Set custom additional completion for current argument.
  63. @param {Function} completion generation function,
  64. invoked in the context of argument instance.
  65. Accepts parameters:
  66. - {Object} opts completion options
  67. It can return promise or any other value treated as result.
  68. @returns {COA.Arg} this instance (for chainability)
  69. */
  70. Arg.prototype.comp = Cmd.prototype.comp;
  71. /**
  72. Make argument value inputting stream.
  73. It's add useful validation and shortcut for STDIN.
  74. @returns {COA.Arg} this instance (for chainability)
  75. */
  76. Arg.prototype.input = Opt.prototype.input;
  77. /**
  78. Make argument value outputing stream.
  79. It's add useful validation and shortcut for STDOUT.
  80. @returns {COA.Arg} this instance (for chainability)
  81. */
  82. Arg.prototype.output = Opt.prototype.output;
  83. Arg.prototype._parse = function(arg, args) {
  84. return this._saveVal(args, arg);
  85. };
  86. Arg.prototype._saveVal = Opt.prototype._saveVal;
  87. Arg.prototype._checkParsed = function(opts, args) {
  88. return !args.hasOwnProperty(this._name);
  89. };
  90. Arg.prototype._usage = function() {
  91. var res;
  92. res = [];
  93. res.push(Color('lpurple', this._name.toUpperCase()), ' : ', this._title);
  94. if (this._req) {
  95. res.push(' ', Color('lred', '(required)'));
  96. }
  97. return res.join('');
  98. };
  99. Arg.prototype._requiredText = function() {
  100. return 'Missing required argument:\n ' + this._usage();
  101. };
  102. /**
  103. Return rejected promise with error code.
  104. Use in .val() for return with error.
  105. @param {Object} reject reason
  106. You can customize toString() method and exitCode property
  107. of reason object.
  108. @returns {Q.promise} rejected promise
  109. */
  110. Arg.prototype.reject = Cmd.prototype.reject;
  111. /**
  112. Finish chain for current option and return parent command instance.
  113. @returns {COA.Cmd} parent command
  114. */
  115. Arg.prototype.end = Cmd.prototype.end;
  116. /**
  117. Apply function with arguments in context of arg instance.
  118. @param {Function} fn
  119. @param {Array} args
  120. @returns {COA.Arg} this instance (for chainability)
  121. */
  122. Arg.prototype.apply = Cmd.prototype.apply;
  123. return Arg;
  124. })();