coa 0.4.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/lib/arg.js CHANGED
@@ -1,175 +1,58 @@
1
- // Generated by CoffeeScript 1.6.3
2
- var Arg, Cmd, Color, Opt;
1
+ 'use strict';
3
2
 
4
- Color = require('./color').Color;
5
-
6
- Cmd = require('./cmd').Cmd;
7
-
8
- Opt = require('./opt').Opt;
3
+ const
4
+ CoaParam = require('./coaparam'),
5
+ Color = require('./color');
9
6
 
10
7
  /**
11
- Argument
12
-
13
- Unnamed entity. From command line arguments passed as list of unnamed values.
14
- @namespace
15
- @class Presents argument
16
- */
17
-
18
-
19
- exports.Arg = Arg = (function() {
20
- /**
21
- @constructs
22
- @param {COA.Cmd} cmd parent command
23
- */
24
-
25
- function Arg(_cmd) {
26
- this._cmd = _cmd;
27
- this._cmd._args.push(this);
28
- }
29
-
30
- /**
31
- Set a canonical argument identifier to be used anywhere in text messages.
32
- @param {String} _name argument name
33
- @returns {COA.Arg} this instance (for chainability)
34
- */
35
-
36
-
37
- Arg.prototype.name = Opt.prototype.name;
38
-
39
- /**
40
- Set a long description for argument to be used anywhere in text messages.
41
- @param {String} _title argument title
42
- @returns {COA.Arg} this instance (for chainability)
43
- */
44
-
45
-
46
- Arg.prototype.title = Cmd.prototype.title;
47
-
48
- /**
49
- Makes an argument accepts multiple values.
50
- Otherwise, the value will be used by the latter passed.
51
- @returns {COA.Arg} this instance (for chainability)
52
- */
53
-
54
-
55
- Arg.prototype.arr = Opt.prototype.arr;
56
-
57
- /**
58
- Makes an argument required.
59
- @returns {COA.Arg} this instance (for chainability)
60
- */
61
-
62
-
63
- Arg.prototype.req = Opt.prototype.req;
64
-
65
- /**
66
- Set a validation (or value) function for argument.
67
- Value from command line passes through before becoming available from API.
68
- Using for validation and convertion simple types to any values.
69
- @param {Function} _val validating function,
70
- invoked in the context of argument instance
71
- and has one parameter with value from command line
72
- @returns {COA.Arg} this instance (for chainability)
73
- */
74
-
75
-
76
- Arg.prototype.val = Opt.prototype.val;
77
-
78
- /**
79
- Set a default value for argument.
80
- Default value passed through validation function as ordinary value.
81
- @param {Object} _def
82
- @returns {COA.Arg} this instance (for chainability)
83
- */
84
-
85
-
86
- Arg.prototype.def = Opt.prototype.def;
87
-
88
- /**
89
- Set custom additional completion for current argument.
90
- @param {Function} completion generation function,
91
- invoked in the context of argument instance.
92
- Accepts parameters:
93
- - {Object} opts completion options
94
- It can return promise or any other value treated as result.
95
- @returns {COA.Arg} this instance (for chainability)
96
- */
97
-
98
-
99
- Arg.prototype.comp = Cmd.prototype.comp;
100
-
101
- /**
102
- Make argument value inputting stream.
103
- It's add useful validation and shortcut for STDIN.
104
- @returns {COA.Arg} this instance (for chainability)
105
- */
106
-
107
-
108
- Arg.prototype.input = Opt.prototype.input;
109
-
110
- /**
111
- Make argument value outputing stream.
112
- It's add useful validation and shortcut for STDOUT.
113
- @returns {COA.Arg} this instance (for chainability)
114
- */
115
-
116
-
117
- Arg.prototype.output = Opt.prototype.output;
118
-
119
- Arg.prototype._parse = function(arg, args) {
120
- return this._saveVal(args, arg);
121
- };
122
-
123
- Arg.prototype._saveVal = Opt.prototype._saveVal;
124
-
125
- Arg.prototype._checkParsed = function(opts, args) {
126
- return !args.hasOwnProperty(this._name);
127
- };
128
-
129
- Arg.prototype._usage = function() {
130
- var res;
131
- res = [];
132
- res.push(Color('lpurple', this._name.toUpperCase()), ' : ', this._title);
133
- if (this._req) {
134
- res.push(' ', Color('lred', '(required)'));
8
+ * Argument
9
+ *
10
+ * Unnamed entity. From command line arguments passed as list of unnamed values.
11
+ *
12
+ * @class Arg
13
+ * @extends CoaParam
14
+ */
15
+ module.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);
135
24
  }
136
- return res.join('');
137
- };
138
-
139
- Arg.prototype._requiredText = function() {
140
- return 'Missing required argument:\n ' + this._usage();
141
- };
142
-
143
- /**
144
- Return rejected promise with error code.
145
- Use in .val() for return with error.
146
- @param {Object} reject reason
147
- You can customize toString() method and exitCode property
148
- of reason object.
149
- @returns {Q.promise} rejected promise
150
- */
151
25
 
26
+ _saveVal(args, val) {
27
+ this._val && (val = this._val(val));
152
28
 
153
- Arg.prototype.reject = Cmd.prototype.reject;
29
+ const name = this._name;
30
+ this._arr
31
+ ? (args[name] || (args[name] = [])).push(val)
32
+ : (args[name] = val);
154
33
 
155
- /**
156
- Finish chain for current option and return parent command instance.
157
- @returns {COA.Cmd} parent command
158
- */
34
+ return val;
35
+ }
159
36
 
37
+ _parse(arg, args) {
38
+ return this._saveVal(args, arg);
39
+ }
160
40
 
161
- Arg.prototype.end = Cmd.prototype.end;
41
+ _checkParsed(opts, args) {
42
+ return !args.hasOwnProperty(this._name);
43
+ }
162
44
 
163
- /**
164
- Apply function with arguments in context of arg instance.
165
- @param {Function} fn
166
- @param {Array} args
167
- @returns {COA.Arg} this instance (for chainability)
168
- */
45
+ _usage() {
46
+ const res = [];
169
47
 
48
+ res.push(Color('lpurple', this._name.toUpperCase()), ' : ', this._title);
170
49
 
171
- Arg.prototype.apply = Cmd.prototype.apply;
50
+ this._req && res.push(' ', Color('lred', '(required)'));
172
51
 
173
- return Arg;
52
+ return res.join('');
53
+ }
174
54
 
175
- })();
55
+ _requiredText() {
56
+ return `Missing required argument:\n ${this._usage()}`;
57
+ }
58
+ };