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