yargs 3.7.1 → 3.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +23 -7
- package/index.js +461 -456
- package/lib/completion.js +29 -29
- package/lib/parser.js +349 -354
- package/lib/usage.js +289 -381
- package/lib/validation.js +186 -187
- package/package.json +19 -20
package/index.js
CHANGED
|
@@ -3,493 +3,498 @@ var assert = require('assert'),
|
|
|
3
3
|
Completion = require('./lib/completion'),
|
|
4
4
|
Parser = require('./lib/parser'),
|
|
5
5
|
Usage = require('./lib/usage'),
|
|
6
|
-
Validation = require('./lib/validation')
|
|
6
|
+
Validation = require('./lib/validation')
|
|
7
7
|
|
|
8
|
-
Argv(process.argv.slice(2))
|
|
8
|
+
Argv(process.argv.slice(2))
|
|
9
9
|
|
|
10
|
-
var exports = module.exports = Argv
|
|
10
|
+
var exports = module.exports = Argv
|
|
11
11
|
function Argv (processArgs, cwd) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
12
|
+
processArgs = processArgs || [] // handle calling yargs().
|
|
13
|
+
|
|
14
|
+
var self = {}
|
|
15
|
+
var completion = null
|
|
16
|
+
var usage = null
|
|
17
|
+
var validation = null
|
|
18
|
+
|
|
19
|
+
if (!cwd) cwd = process.cwd()
|
|
20
|
+
|
|
21
|
+
self.$0 = process.argv
|
|
22
|
+
.slice(0, 2)
|
|
23
|
+
.map(function (x, i) {
|
|
24
|
+
// ignore the node bin, specify this in your
|
|
25
|
+
// bin file with #!/usr/bin/env node
|
|
26
|
+
if (i === 0 && /\b(node|iojs)$/.test(x)) return
|
|
27
|
+
var b = rebase(cwd, x)
|
|
28
|
+
return x.match(/^\//) && b.length < x.length
|
|
29
|
+
? b : x
|
|
30
|
+
})
|
|
31
|
+
.join(' ').trim()
|
|
32
|
+
|
|
33
|
+
if (process.env._ !== undefined && process.argv[1] === process.env._) {
|
|
34
|
+
self.$0 = process.env._.replace(
|
|
35
|
+
path.dirname(process.execPath) + '/', ''
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var options
|
|
40
|
+
self.resetOptions = self.reset = function () {
|
|
41
|
+
// put yargs back into its initial
|
|
42
|
+
// state, this is useful for creating a
|
|
43
|
+
// nested CLI.
|
|
44
|
+
options = {
|
|
45
|
+
array: [],
|
|
46
|
+
boolean: [],
|
|
47
|
+
string: [],
|
|
48
|
+
narg: {},
|
|
49
|
+
key: {},
|
|
50
|
+
alias: {},
|
|
51
|
+
default: {},
|
|
52
|
+
defaultDescription: {},
|
|
53
|
+
requiresArg: [],
|
|
54
|
+
count: [],
|
|
55
|
+
normalize: [],
|
|
56
|
+
config: []
|
|
38
57
|
}
|
|
39
58
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
self.boolean = function (bools) {
|
|
77
|
-
options.boolean.push.apply(options.boolean, [].concat(bools));
|
|
78
|
-
return self;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
self.array = function (arrays) {
|
|
82
|
-
options.array.push.apply(options.array, [].concat(arrays));
|
|
83
|
-
return self;
|
|
59
|
+
usage = Usage(self) // handle usage output.
|
|
60
|
+
validation = Validation(self, usage) // handle arg validation.
|
|
61
|
+
completion = Completion(self, usage)
|
|
62
|
+
|
|
63
|
+
demanded = {}
|
|
64
|
+
|
|
65
|
+
exitProcess = true
|
|
66
|
+
strict = false
|
|
67
|
+
helpOpt = null
|
|
68
|
+
versionOpt = null
|
|
69
|
+
completionOpt = null
|
|
70
|
+
commandHandlers = {}
|
|
71
|
+
self.parsed = false
|
|
72
|
+
|
|
73
|
+
return self
|
|
74
|
+
}
|
|
75
|
+
self.resetOptions()
|
|
76
|
+
|
|
77
|
+
self.boolean = function (bools) {
|
|
78
|
+
options.boolean.push.apply(options.boolean, [].concat(bools))
|
|
79
|
+
return self
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
self.array = function (arrays) {
|
|
83
|
+
options.array.push.apply(options.array, [].concat(arrays))
|
|
84
|
+
return self
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
self.nargs = function (key, n) {
|
|
88
|
+
if (typeof key === 'object') {
|
|
89
|
+
Object.keys(key).forEach(function (k) {
|
|
90
|
+
self.nargs(k, key[k])
|
|
91
|
+
})
|
|
92
|
+
} else {
|
|
93
|
+
options.narg[key] = n
|
|
84
94
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
+
return self
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
self.normalize = function (strings) {
|
|
99
|
+
options.normalize.push.apply(options.normalize, [].concat(strings))
|
|
100
|
+
return self
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
self.config = function (configs) {
|
|
104
|
+
options.config.push.apply(options.config, [].concat(configs))
|
|
105
|
+
return self
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
self.example = function (cmd, description) {
|
|
109
|
+
usage.example(cmd, description)
|
|
110
|
+
return self
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
self.command = function (cmd, description, fn) {
|
|
114
|
+
usage.command(cmd, description)
|
|
115
|
+
if (fn) commandHandlers[cmd] = fn
|
|
116
|
+
return self
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
var commandHandlers = {}
|
|
120
|
+
self.getCommandHandlers = function () {
|
|
121
|
+
return commandHandlers
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
self.string = function (strings) {
|
|
125
|
+
options.string.push.apply(options.string, [].concat(strings))
|
|
126
|
+
return self
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
self.default = function (key, value, defaultDescription) {
|
|
130
|
+
if (typeof key === 'object') {
|
|
131
|
+
Object.keys(key).forEach(function (k) {
|
|
132
|
+
self.default(k, key[k])
|
|
133
|
+
})
|
|
134
|
+
} else {
|
|
135
|
+
if (typeof value === 'function') {
|
|
136
|
+
defaultDescription = usage.functionDescription(value, defaultDescription)
|
|
137
|
+
value = value.call()
|
|
138
|
+
}
|
|
139
|
+
options.defaultDescription[key] = defaultDescription
|
|
140
|
+
options.default[key] = value
|
|
95
141
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
self.example = function (cmd, description) {
|
|
108
|
-
usage.example(cmd, description);
|
|
109
|
-
return self;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
self.command = function (cmd, description) {
|
|
113
|
-
usage.command(cmd, description);
|
|
114
|
-
return self;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
self.string = function (strings) {
|
|
118
|
-
options.string.push.apply(options.string, [].concat(strings));
|
|
119
|
-
return self;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
self.default = function (key, value, defaultDescription) {
|
|
123
|
-
if (typeof key === 'object') {
|
|
124
|
-
Object.keys(key).forEach(function (k) {
|
|
125
|
-
self.default(k, key[k]);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
if (typeof value === 'function') {
|
|
130
|
-
defaultDescription = usage.functionDescription(value, defaultDescription);
|
|
131
|
-
value = value.call();
|
|
132
|
-
}
|
|
133
|
-
options.defaultDescription[key] = defaultDescription;
|
|
134
|
-
options.default[key] = value;
|
|
135
|
-
}
|
|
136
|
-
return self;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
self.alias = function (x, y) {
|
|
140
|
-
if (typeof x === 'object') {
|
|
141
|
-
Object.keys(x).forEach(function (key) {
|
|
142
|
-
self.alias(key, x[key]);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
options.alias[x] = (options.alias[x] || []).concat(y);
|
|
147
|
-
}
|
|
148
|
-
return self;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
self.count = function(counts) {
|
|
152
|
-
options.count.push.apply(options.count, [].concat(counts));
|
|
153
|
-
return self;
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
var demanded = {};
|
|
157
|
-
self.demand = self.required = self.require = function (keys, msg) {
|
|
158
|
-
if (typeof keys == 'number') {
|
|
159
|
-
if (!demanded._) demanded._ = { count: 0, msg: null };
|
|
160
|
-
demanded._.count += keys;
|
|
161
|
-
demanded._.msg = msg;
|
|
162
|
-
}
|
|
163
|
-
else if (Array.isArray(keys)) {
|
|
164
|
-
keys.forEach(function (key) {
|
|
165
|
-
self.demand(key, msg);
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
if (typeof msg === 'string') {
|
|
170
|
-
demanded[keys] = { msg: msg };
|
|
171
|
-
}
|
|
172
|
-
else if (msg === true || typeof msg === 'undefined') {
|
|
173
|
-
demanded[keys] = { msg: undefined };
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return self;
|
|
178
|
-
};
|
|
179
|
-
self.getDemanded = function() {
|
|
180
|
-
return demanded;
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
self.requiresArg = function (requiresArgs) {
|
|
184
|
-
options.requiresArg.push.apply(options.requiresArg, [].concat(requiresArgs));
|
|
185
|
-
return self;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
self.implies = function (key, value) {
|
|
189
|
-
validation.implies(key, value);
|
|
190
|
-
return self;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
self.usage = function (msg, opts) {
|
|
194
|
-
if (!opts && typeof msg === 'object') {
|
|
195
|
-
opts = msg;
|
|
196
|
-
msg = null;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
usage.usage(msg);
|
|
200
|
-
|
|
201
|
-
if (opts) self.options(opts);
|
|
202
|
-
|
|
203
|
-
return self;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
self.epilogue = self.epilog = function (msg) {
|
|
207
|
-
usage.epilog(msg);
|
|
208
|
-
return self;
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
self.fail = function (f) {
|
|
212
|
-
usage.failFn(f);
|
|
213
|
-
return self;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
self.check = function (f) {
|
|
217
|
-
validation.check(f);
|
|
218
|
-
return self;
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
self.defaults = self.default;
|
|
222
|
-
|
|
223
|
-
self.describe = function (key, desc) {
|
|
224
|
-
options.key[key] = true;
|
|
225
|
-
usage.describe(key, desc);
|
|
226
|
-
return self;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
self.parse = function (args) {
|
|
230
|
-
return parseArgs(args);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
self.option = self.options = function (key, opt) {
|
|
234
|
-
if (typeof key === 'object') {
|
|
235
|
-
Object.keys(key).forEach(function (k) {
|
|
236
|
-
self.options(k, key[k]);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
assert(typeof opt === 'object', 'second argument to option must be an object');
|
|
241
|
-
|
|
242
|
-
options.key[key] = true; // track manually set keys.
|
|
243
|
-
|
|
244
|
-
if (opt.alias) self.alias(key, opt.alias);
|
|
245
|
-
|
|
246
|
-
var demand = opt.demand || opt.required || opt.require;
|
|
247
|
-
|
|
248
|
-
if (demand) {
|
|
249
|
-
self.demand(key, demand);
|
|
250
|
-
}
|
|
251
|
-
if ('default' in opt) {
|
|
252
|
-
self.default(key, opt.default);
|
|
253
|
-
}
|
|
254
|
-
if ('nargs' in opt) {
|
|
255
|
-
self.nargs(key, opt.nargs);
|
|
256
|
-
}
|
|
257
|
-
if (opt.boolean || opt.type === 'boolean') {
|
|
258
|
-
self.boolean(key);
|
|
259
|
-
if (opt.alias) self.boolean(opt.alias);
|
|
260
|
-
}
|
|
261
|
-
if (opt.array || opt.type === 'array') {
|
|
262
|
-
self.array(key);
|
|
263
|
-
if (opt.alias) self.array(opt.alias);
|
|
264
|
-
}
|
|
265
|
-
if (opt.string || opt.type === 'string') {
|
|
266
|
-
self.string(key);
|
|
267
|
-
if (opt.alias) self.string(opt.alias);
|
|
268
|
-
}
|
|
269
|
-
if (opt.count || opt.type === 'count') {
|
|
270
|
-
self.count(key);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
var desc = opt.describe || opt.description || opt.desc;
|
|
274
|
-
if (desc) {
|
|
275
|
-
self.describe(key, desc);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (opt.requiresArg) {
|
|
279
|
-
self.requiresArg(key);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
return self;
|
|
284
|
-
};
|
|
285
|
-
self.getOptions = function() {
|
|
286
|
-
return options;
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
self.wrap = function (cols) {
|
|
290
|
-
usage.wrap(cols);
|
|
291
|
-
return self;
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
var strict = false;
|
|
295
|
-
self.strict = function () {
|
|
296
|
-
strict = true;
|
|
297
|
-
return self;
|
|
298
|
-
};
|
|
299
|
-
self.getStrict = function () {
|
|
300
|
-
return strict;
|
|
142
|
+
return self
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
self.alias = function (x, y) {
|
|
146
|
+
if (typeof x === 'object') {
|
|
147
|
+
Object.keys(x).forEach(function (key) {
|
|
148
|
+
self.alias(key, x[key])
|
|
149
|
+
})
|
|
150
|
+
} else {
|
|
151
|
+
options.alias[x] = (options.alias[x] || []).concat(y)
|
|
301
152
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return self;
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
var exitProcess = true;
|
|
330
|
-
self.exitProcess = function (enabled) {
|
|
331
|
-
if (typeof enabled !== 'boolean') {
|
|
332
|
-
enabled = true;
|
|
333
|
-
}
|
|
334
|
-
exitProcess = enabled;
|
|
335
|
-
return self;
|
|
336
|
-
};
|
|
337
|
-
self.getExitProcess = function () {
|
|
338
|
-
return exitProcess;
|
|
153
|
+
return self
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
self.count = function (counts) {
|
|
157
|
+
options.count.push.apply(options.count, [].concat(counts))
|
|
158
|
+
return self
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
var demanded = {}
|
|
162
|
+
self.demand = self.required = self.require = function (keys, msg) {
|
|
163
|
+
if (typeof keys === 'number') {
|
|
164
|
+
if (!demanded._) demanded._ = { count: 0, msg: null }
|
|
165
|
+
demanded._.count += keys
|
|
166
|
+
demanded._.msg = msg
|
|
167
|
+
} else if (Array.isArray(keys)) {
|
|
168
|
+
keys.forEach(function (key) {
|
|
169
|
+
self.demand(key, msg)
|
|
170
|
+
})
|
|
171
|
+
} else {
|
|
172
|
+
if (typeof msg === 'string') {
|
|
173
|
+
demanded[keys] = { msg: msg }
|
|
174
|
+
} else if (msg === true || typeof msg === 'undefined') {
|
|
175
|
+
demanded[keys] = { msg: undefined }
|
|
176
|
+
}
|
|
339
177
|
}
|
|
340
178
|
|
|
341
|
-
self
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
completionCommand = cmd;
|
|
362
|
-
completionOpt = completion.completionKey;
|
|
363
|
-
self.command(completionCommand, desc || 'generate bash completion script');
|
|
364
|
-
|
|
365
|
-
// a function can be provided
|
|
366
|
-
if (fn) completion.registerFunction(fn);
|
|
367
|
-
|
|
368
|
-
return self;
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
self.showCompletionScript = function($0) {
|
|
372
|
-
$0 = $0 || self.$0;
|
|
373
|
-
console.log(completion.generateCompletionScript($0));
|
|
374
|
-
return self;
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
self.getUsageInstance = function () {
|
|
378
|
-
return usage;
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
self.getValidationInstance = function () {
|
|
382
|
-
return validation;
|
|
179
|
+
return self
|
|
180
|
+
}
|
|
181
|
+
self.getDemanded = function () {
|
|
182
|
+
return demanded
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
self.requiresArg = function (requiresArgs) {
|
|
186
|
+
options.requiresArg.push.apply(options.requiresArg, [].concat(requiresArgs))
|
|
187
|
+
return self
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
self.implies = function (key, value) {
|
|
191
|
+
validation.implies(key, value)
|
|
192
|
+
return self
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
self.usage = function (msg, opts) {
|
|
196
|
+
if (!opts && typeof msg === 'object') {
|
|
197
|
+
opts = msg
|
|
198
|
+
msg = null
|
|
383
199
|
}
|
|
384
200
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
201
|
+
usage.usage(msg)
|
|
202
|
+
|
|
203
|
+
if (opts) self.options(opts)
|
|
204
|
+
|
|
205
|
+
return self
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
self.epilogue = self.epilog = function (msg) {
|
|
209
|
+
usage.epilog(msg)
|
|
210
|
+
return self
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
self.fail = function (f) {
|
|
214
|
+
usage.failFn(f)
|
|
215
|
+
return self
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
self.check = function (f) {
|
|
219
|
+
validation.check(f)
|
|
220
|
+
return self
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
self.defaults = self.default
|
|
224
|
+
|
|
225
|
+
self.describe = function (key, desc) {
|
|
226
|
+
options.key[key] = true
|
|
227
|
+
usage.describe(key, desc)
|
|
228
|
+
return self
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
self.parse = function (args) {
|
|
232
|
+
return parseArgs(args)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
self.option = self.options = function (key, opt) {
|
|
236
|
+
if (typeof key === 'object') {
|
|
237
|
+
Object.keys(key).forEach(function (k) {
|
|
238
|
+
self.options(k, key[k])
|
|
239
|
+
})
|
|
240
|
+
} else {
|
|
241
|
+
assert(typeof opt === 'object', 'second argument to option must be an object')
|
|
242
|
+
|
|
243
|
+
options.key[key] = true // track manually set keys.
|
|
244
|
+
|
|
245
|
+
if (opt.alias) self.alias(key, opt.alias)
|
|
246
|
+
|
|
247
|
+
var demand = opt.demand || opt.required || opt.require
|
|
248
|
+
|
|
249
|
+
if (demand) {
|
|
250
|
+
self.demand(key, demand)
|
|
251
|
+
} if ('default' in opt) {
|
|
252
|
+
self.default(key, opt.default)
|
|
253
|
+
} if ('nargs' in opt) {
|
|
254
|
+
self.nargs(key, opt.nargs)
|
|
255
|
+
} if (opt.boolean || opt.type === 'boolean') {
|
|
256
|
+
self.boolean(key)
|
|
257
|
+
if (opt.alias) self.boolean(opt.alias)
|
|
258
|
+
} if (opt.array || opt.type === 'array') {
|
|
259
|
+
self.array(key)
|
|
260
|
+
if (opt.alias) self.array(opt.alias)
|
|
261
|
+
} if (opt.string || opt.type === 'string') {
|
|
262
|
+
self.string(key)
|
|
263
|
+
if (opt.alias) self.string(opt.alias)
|
|
264
|
+
} if (opt.count || opt.type === 'count') {
|
|
265
|
+
self.count(key)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
var desc = opt.describe || opt.description || opt.desc
|
|
269
|
+
if (desc) {
|
|
270
|
+
self.describe(key, desc)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (opt.requiresArg) {
|
|
274
|
+
self.requiresArg(key)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
399
277
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
278
|
+
return self
|
|
279
|
+
}
|
|
280
|
+
self.getOptions = function () {
|
|
281
|
+
return options
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
self.wrap = function (cols) {
|
|
285
|
+
usage.wrap(cols)
|
|
286
|
+
return self
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var strict = false
|
|
290
|
+
self.strict = function () {
|
|
291
|
+
strict = true
|
|
292
|
+
return self
|
|
293
|
+
}
|
|
294
|
+
self.getStrict = function () {
|
|
295
|
+
return strict
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
self.showHelp = function (level) {
|
|
299
|
+
if (!self.parsed) parseArgs(processArgs) // run parser, if it has not already been executed.
|
|
300
|
+
usage.showHelp(level)
|
|
301
|
+
return self
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
var versionOpt = null
|
|
305
|
+
self.version = function (ver, opt, msg) {
|
|
306
|
+
versionOpt = opt || 'version'
|
|
307
|
+
usage.version(ver)
|
|
308
|
+
self.boolean(versionOpt)
|
|
309
|
+
self.describe(versionOpt, msg || 'Show version number')
|
|
310
|
+
return self
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
var helpOpt = null
|
|
314
|
+
self.addHelpOpt = function (opt, msg) {
|
|
315
|
+
helpOpt = opt
|
|
316
|
+
self.boolean(opt)
|
|
317
|
+
self.describe(opt, msg || 'Show help')
|
|
318
|
+
return self
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
self.showHelpOnFail = function (enabled, message) {
|
|
322
|
+
usage.showHelpOnFail(enabled, message)
|
|
323
|
+
return self
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
var exitProcess = true
|
|
327
|
+
self.exitProcess = function (enabled) {
|
|
328
|
+
if (typeof enabled !== 'boolean') {
|
|
329
|
+
enabled = true
|
|
330
|
+
}
|
|
331
|
+
exitProcess = enabled
|
|
332
|
+
return self
|
|
333
|
+
}
|
|
334
|
+
self.getExitProcess = function () {
|
|
335
|
+
return exitProcess
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
self.help = function () {
|
|
339
|
+
if (arguments.length > 0) return self.addHelpOpt.apply(self, arguments)
|
|
340
|
+
|
|
341
|
+
if (!self.parsed) parseArgs(processArgs) // run parser, if it has not already been executed.
|
|
342
|
+
|
|
343
|
+
return usage.help()
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
var completionOpt = null,
|
|
347
|
+
completionCommand = null
|
|
348
|
+
self.completion = function (cmd, desc, fn) {
|
|
349
|
+
// a function to execute when generating
|
|
350
|
+
// completions can be provided as the second
|
|
351
|
+
// or third argument to completion.
|
|
352
|
+
if (typeof desc === 'function') {
|
|
353
|
+
fn = desc
|
|
354
|
+
desc = null
|
|
355
|
+
}
|
|
404
356
|
|
|
405
|
-
|
|
357
|
+
// register the completion command.
|
|
358
|
+
completionCommand = cmd
|
|
359
|
+
completionOpt = completion.completionKey
|
|
360
|
+
self.command(completionCommand, desc || 'generate bash completion script')
|
|
361
|
+
|
|
362
|
+
// a function can be provided
|
|
363
|
+
if (fn) completion.registerFunction(fn)
|
|
364
|
+
|
|
365
|
+
return self
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
self.showCompletionScript = function ($0) {
|
|
369
|
+
$0 = $0 || self.$0
|
|
370
|
+
console.log(completion.generateCompletionScript($0))
|
|
371
|
+
return self
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
self.getUsageInstance = function () {
|
|
375
|
+
return usage
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
self.getValidationInstance = function () {
|
|
379
|
+
return validation
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
Object.defineProperty(self, 'argv', {
|
|
383
|
+
get: function () {
|
|
384
|
+
var args = null
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
args = parseArgs(processArgs)
|
|
388
|
+
} catch (err) {
|
|
389
|
+
usage.fail(err.message)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return args
|
|
393
|
+
},
|
|
394
|
+
enumerable: true
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
function parseArgs (args) {
|
|
398
|
+
var parsed = Parser(args, options),
|
|
399
|
+
argv = parsed.argv,
|
|
400
|
+
aliases = parsed.aliases
|
|
401
|
+
|
|
402
|
+
argv.$0 = self.$0
|
|
403
|
+
|
|
404
|
+
self.parsed = parsed
|
|
405
|
+
|
|
406
|
+
// generate a completion script for adding to ~/.bashrc.
|
|
407
|
+
if (completionCommand && ~argv._.indexOf(completionCommand)) {
|
|
408
|
+
self.showCompletionScript()
|
|
409
|
+
if (exitProcess) {
|
|
410
|
+
process.exit(0)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
406
413
|
|
|
407
|
-
|
|
414
|
+
// if there's a handler associated with a
|
|
415
|
+
// command defer processing to it.
|
|
416
|
+
var handlerKeys = Object.keys(self.getCommandHandlers())
|
|
417
|
+
for (var i = 0, command; (command = handlerKeys[i]) !== undefined; i++) {
|
|
418
|
+
if (~argv._.indexOf(command)) {
|
|
419
|
+
self.getCommandHandlers()[command](self.reset())
|
|
420
|
+
return self.argv
|
|
421
|
+
}
|
|
422
|
+
}
|
|
408
423
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
}
|
|
424
|
+
Object.keys(argv).forEach(function (key) {
|
|
425
|
+
if (key === helpOpt && argv[key]) {
|
|
426
|
+
self.showHelp('log')
|
|
427
|
+
if (exitProcess) {
|
|
428
|
+
process.exit(0)
|
|
415
429
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
if (exitProcess){
|
|
421
|
-
process.exit(0);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
else if (key === versionOpt) {
|
|
425
|
-
usage.showVersion();
|
|
426
|
-
if (exitProcess){
|
|
427
|
-
process.exit(0);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
else if (key === completionOpt) {
|
|
431
|
-
// we allow for asynchronous completions,
|
|
432
|
-
// e.g., loading in a list of commands from an API.
|
|
433
|
-
completion.getCompletion(function(completions) {
|
|
434
|
-
(completions || []).forEach(function(completion) {
|
|
435
|
-
console.log(completion);
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
if (exitProcess){
|
|
439
|
-
process.exit(0);
|
|
440
|
-
}
|
|
441
|
-
});
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
validation.nonOptionCount(argv);
|
|
447
|
-
validation.missingArgumentValue(argv);
|
|
448
|
-
validation.requiredArguments(argv);
|
|
449
|
-
|
|
450
|
-
if (strict) {
|
|
451
|
-
validation.unknownArguments(argv, aliases);
|
|
430
|
+
} else if (key === versionOpt && argv[key]) {
|
|
431
|
+
usage.showVersion()
|
|
432
|
+
if (exitProcess) {
|
|
433
|
+
process.exit(0)
|
|
452
434
|
}
|
|
435
|
+
} else if (key === completionOpt) {
|
|
436
|
+
// we allow for asynchronous completions,
|
|
437
|
+
// e.g., loading in a list of commands from an API.
|
|
438
|
+
completion.getCompletion(function (completions) {
|
|
439
|
+
;(completions || []).forEach(function (completion) {
|
|
440
|
+
console.log(completion)
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
if (exitProcess) {
|
|
444
|
+
process.exit(0)
|
|
445
|
+
}
|
|
446
|
+
})
|
|
447
|
+
return
|
|
448
|
+
}
|
|
449
|
+
})
|
|
453
450
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
451
|
+
validation.nonOptionCount(argv)
|
|
452
|
+
validation.missingArgumentValue(argv)
|
|
453
|
+
validation.requiredArguments(argv)
|
|
457
454
|
|
|
458
|
-
|
|
455
|
+
if (strict) {
|
|
456
|
+
validation.unknownArguments(argv, aliases)
|
|
459
457
|
}
|
|
460
458
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
459
|
+
validation.customChecks(argv, aliases)
|
|
460
|
+
validation.implications(argv)
|
|
461
|
+
setPlaceholderKeys(argv)
|
|
462
|
+
|
|
463
|
+
return argv
|
|
464
|
+
}
|
|
466
465
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
466
|
+
function setPlaceholderKeys (argv) {
|
|
467
|
+
Object.keys(options.key).forEach(function (key) {
|
|
468
|
+
if (typeof argv[key] === 'undefined') argv[key] = undefined
|
|
469
|
+
})
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
sigletonify(self)
|
|
473
|
+
return self
|
|
474
|
+
}
|
|
470
475
|
|
|
471
476
|
// rebase an absolute path to a relative one with respect to a base directory
|
|
472
477
|
// exported for tests
|
|
473
|
-
exports.rebase = rebase
|
|
478
|
+
exports.rebase = rebase
|
|
474
479
|
function rebase (base, dir) {
|
|
475
|
-
return path.relative(base, dir)
|
|
476
|
-
}
|
|
480
|
+
return path.relative(base, dir)
|
|
481
|
+
}
|
|
477
482
|
|
|
478
483
|
/* Hack an instance of Argv with process.argv into Argv
|
|
479
484
|
so people can do
|
|
480
|
-
|
|
485
|
+
require('yargs')(['--beeble=1','-z','zizzle']).argv
|
|
481
486
|
to parse a list of args and
|
|
482
|
-
|
|
487
|
+
require('yargs').argv
|
|
483
488
|
to get a parsed version of process.argv.
|
|
484
489
|
*/
|
|
485
|
-
function sigletonify(inst) {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
490
|
+
function sigletonify (inst) {
|
|
491
|
+
Object.keys(inst).forEach(function (key) {
|
|
492
|
+
if (key === 'argv') {
|
|
493
|
+
Argv.__defineGetter__(key, inst.__lookupGetter__(key))
|
|
494
|
+
} else {
|
|
495
|
+
Argv[key] = typeof inst[key] === 'function'
|
|
496
|
+
? inst[key].bind(inst)
|
|
497
|
+
: inst[key]
|
|
498
|
+
}
|
|
499
|
+
})
|
|
495
500
|
}
|