arg.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const
  3. CoaParam = require('./coaparam'),
  4. chalk = require('chalk');
  5. /**
  6. * Argument
  7. *
  8. * Unnamed entity. From command line arguments passed as list of unnamed values.
  9. *
  10. * @class Arg
  11. * @extends CoaParam
  12. */
  13. module.exports = class Arg extends CoaParam {
  14. /**
  15. * @constructs
  16. * @param {COA.Cmd} cmd - parent command
  17. */
  18. constructor(cmd) {
  19. super(cmd);
  20. this._cmd._args.push(this);
  21. }
  22. _saveVal(args, val) {
  23. this._val && (val = this._val(val));
  24. const name = this._name;
  25. this._arr
  26. ? (args[name] || (args[name] = [])).push(val)
  27. : (args[name] = val);
  28. return val;
  29. }
  30. _parse(arg, args) {
  31. return this._saveVal(args, arg);
  32. }
  33. _checkParsed(opts, args) {
  34. return !args.hasOwnProperty(this._name);
  35. }
  36. _usage() {
  37. const res = [];
  38. res.push(chalk.magentaBright(this._name.toUpperCase()), ' : ', this._title);
  39. this._req && res.push(' ', chalk.redBright('(required)'));
  40. return res.join('');
  41. }
  42. _requiredText() {
  43. return `Missing required argument:\n ${this._usage()}`;
  44. }
  45. };