arg.coffee 3.6 KB

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