This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
850 lines (755 loc) · 22.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
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
38
39
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
var events = require('events')
, path = require('path')
, basename = path.basename
, util = require('util')
, utils = require('cli-util')
, camelcase = utils.camelcase
, define = utils.define
, EventProxy = require('./lib/event-proxy')
, Description = require('cli-description')
, mutators = require('./lib/mutators')
, helpers = require('./lib/finder');
var k, keys;
var re = {
delimiter: function(){return /[ ,|]+/;},
required: function(){return /^=?</;},
multiple: function(){return /\.\.\./;},
extra: function(){return /^([^=\[<]*)((=|\[|<).*)/;},
key: function(){return /^([^:\s]+)(\s+)?:(\s+)?(.*)/;},
no: function(){return /(\[no-?\]-?)/;}
}
function initialize(options, properties) {
for(var z in options) {
if(~properties.indexOf(z) && options[z]) {
this[z](options[z]);
}
}
if(options.description) this.description(options.description);
if(options.detail) this.detail(options.detail);
if(options.action && typeof this.action === 'function') {
this.action(options.action);
}
}
function description(description) {
if(!arguments.length) return this._description;
if(description && typeof description === 'string') {
description = new Description(description);
}
//console.log('set description %j', description);
this._description = description;
return this;
}
function detail(detail) {
if(!arguments.length) return this._detail;
if(detail && typeof detail === 'string') {
detail = new Description(detail);
}
//console.log('set detail %j', detail);
this._detail = detail;
return this;
}
/**
* Retrieve the key for an option.
*
* Scope is the option. Alternatively you may pass a
* string of the raw name passed when instantiating the options.
*
* @param names Array of names or raw string name (optional).
*/
function getKey(names, name) {
var k, prefixed = false;
names = names || this._names;
name = name || this._name;
if(typeof names == 'string') {
name = names;
names = names.split(re.delimiter());
}
// handle key prefixes, in the form *name: -n, --file-name*
if(re.key().test(name)) {
k = name.replace(re.key(), "$1");
name = this._name = name.replace(re.key(), "$4");
names = this._names = name.split(re.delimiter());
//console.log('got new key names %j', names);
getExtra.call(this);
prefixed = true;
//console.log('got key prefix "%s"', k);
//console.log('got new key name "%s"', name);
//console.log('got new extra %s', this._extra);
//console.log('got new key names %j', this._names);
}
names = names.slice(0);
if(this._extra) {
var j = names.length -1;
var nm = names[j];
while(re.extra().test(nm) || re.multiple().test(nm)) {
// allow for flush extra values
if(/^-+/.test(nm)) {
names[j] = nm.replace(re.extra(), "$1");
}else{
names.pop();
}
nm = names[--j];
}
this._names = sortNames(names);
}
if(!k) {
k = names.reduce(
function (a, b) { return a.length > b.length ? a : b; });
k = k.replace(/^-+/, '');
}
return prefixed ? k : camelcase(k.toLowerCase());
}
function sortNames(names, cmd) {
return names.sort(function(a, b) {
if(cmd) return b.length > a.length;
// sort short options before long options
var re = /^-[^-]/;
return re.test(a) ? -1 : re.test(b) ? 1 : 0;
});
}
/**
* Retrieve the extra portion of the option name.
*
* Note this also updates the names array to not include
* the extra portion.
*/
function getExtra() {
if(re.no().test(this._name)) {
this._extra = false;
return;
}
if(!re.extra().test(this._name)) return;
this._extra = this._name.replace(re.extra(), "$2");
//console.log(this._extra);
var name = this._name.replace(re.extra(), "$1");
//console.log('name: %s', name);
this._names = name.split(re.delimiter());
if(!this._names[this._names.length - 1]) this._names.pop();
this._names = sortNames(this._names);
}
//var enumerable = process.env.CLI_TOOLKIT_DEBUG ? true : false;
//function define(obj, name, value, writable, enumerate) {
//writable = writable !== undefined ? writable : false;
//enumerate = enumerate !== undefined ? enumerate : enumerable;
//Object.defineProperty(obj, name,
//{
//enumerable: enumerate,
//configurable: false,
//writable: writable,
//value: value
//}
//);
//}
/**
* Abstract super class.
*
* @param name The argument name.
* @param description The argument description.
* @param options The argument options or conversion function.
*/
function Argument(name, description, options) {
if(typeof name == 'object') options = name;
define(this, '_name', name, true);
define(this, '_description', '', true);
define(this, '_detail', undefined, true);
define(this, '_key', '', true);
define(this, '_optional', true, true);
define(this, '_multiple', false, true);
define(this, '_value', undefined, true);
define(this, '_converter', undefined, true);
define(this, '_action', undefined, true);
define(this, '_extra', undefined, true);
define(this, '_names', undefined, true);
// event emitter
define(this, '_emitter', new events.EventEmitter(), false);
define(this, '_events', undefined, true);
define(this, '_maxListeners', undefined, true);
define(this, 'domain', undefined, true);
// must set description this way to get the plain
// text and markdown descriptions
this.description(description || '');
if(options === JSON) {
this._converter = JSON;
}else if(options && (typeof options == 'object') && !Array.isArray(options)) {
initialize.call(this, options, Object.keys(mutators.arg));
}else if((typeof options == 'function') || this.isFunctionArray(options)){
this._converter = options;
if(arguments.length > 3 && this._value === undefined) {
this._value = arguments[3];
}
}else {
this._value = options;
if(arguments.length > 3
&& (typeof(arguments[3]) == 'function'
|| this.isFunctionArray(options))) {
this._converter = arguments[3];
}
}
if(!this._name || typeof this._name !== 'string') {
throw new TypeError('Invalid argument name \'' + this._name + '\'');
}
// strip no prefixes
var no = re.no();
var name = '' + this._name;
if((this instanceof Flag) && no.test(this._name)) {
name = this._name.replace(no, '');
}
this._names = this._name.split(re.delimiter());
//console.dir(this._names);
getExtra.call(this);
if(re.required().test(this._extra)) {
this._optional = false;
}
if(re.multiple().test(this._extra)) {
this._multiple = true;
}
this._key = getKey.call(this, name);
//console.log('key', this._key);
}
define(Argument.prototype, 'description', description, false);
define(Argument.prototype, 'detail', detail, false);
for(k in EventProxy) {
define(Argument.prototype, k, EventProxy[k], false);
}
// declare instanceof tests here so there is no
// chance the instances are different between dependencies
// - eg: module version conflict
function isArgument() {
return (this instanceof Argument);
}
function isOption() {
return (this instanceof Option);
}
function isFlag() {
return (this instanceof Flag);
}
function isCommand() {
return (this instanceof Command);
}
function isProgram() {
return (this instanceof Program);
}
var is = {
isArgument: isArgument,
isOption: isOption,
isFlag: isFlag,
isCommand: isCommand,
isProgram: isProgram
}
for(k in is) {
define(Argument.prototype, k, is[k], false);
}
function isFunction(f) {
return typeof f === 'function';
}
function toObject(opts, depth) {
opts = opts || {};
if(depth === undefined) depth = 0;
var o = {};
o.constructor = this.constructor;
o.key = this.key();
if(opts.name !== false && isFunction(this.name)) {
o.name = this.name();
}
if(opts.version !== false && isFunction(this.version)) {
// NOTE: do not call version(), CommandProgram will use middleware
o.version = this._version;
}
if(opts.description !== false && isFunction(this.description)) {
o.description = this.description();
}
if(opts.detail !== false && isFunction(this.detail)) {
o.detail = this.detail();
}
if((opts.all || opts.sections) && isFunction(this.sections)) {
o.sections = this.sections();
}
if((opts.all || opts.names) && isFunction(this.names)) {
o.names = this.names();
}
if((opts.all || opts.extra) && isFunction(this.extra)) {
o.extra = this.extra();
}
if((opts.all || opts.value) && isFunction(this.value)) {
o.value = this.value();
}
if((opts.all || opts.optional) && isFunction(this.optional)) {
o.optional = this.optional();
}
if((opts.all || opts.multiple) && isFunction(this.multiple)) {
o.multiple = this.multiple();
}
if(opts.all || opts.methods) {
if(isFunction(this.converter)) {
o.converter = this.converter();
}
if(isFunction(this.action)) {
o.action = this.action();
}
}
function walk(args, target) {
var k, v;
for(k in args) {
v = args[k];
target[k] = v.toObject(opts, depth);
}
}
if(typeof opts.item === 'function') {
o = opts.item.call(this, o, depth);
}
if(opts.recurse !== false
&& isFunction(this.options) && isFunction(this.commands)) {
if(Object.keys(this.options()).length) {
o.options = {};
walk(this.options(), o.options);
}
if(Object.keys(this.commands()).length) {
o.commands = {};
++depth;
walk(this.commands(), o.commands);
--depth;
}
}
return o;
}
define(Argument.prototype, 'toObject', toObject, false);
function toString(delimiter, names) {
if(!arguments.length) return Object.prototype.toString.call(this);
names = names || this.names();
var opt = typeof(this.extra) === 'function';
delimiter = delimiter || ' | ';
// TODO: sort commands correctly
names = sortNames(names, (this instanceof Command));
return names.join(delimiter);
}
define(Argument.prototype, 'toString', toString, false);
/**
* Retrieve a standardized string to use when listing options.
*/
getOptionString = function(delimiter, assignment, names, extra) {
assignment = assignment || '=';
delimiter = delimiter || ', ';
extra = extra === undefined ? '' : extra;
var opt = typeof(this.extra) === 'function';
if(opt && extra !== false) {
// use extracted extra data assigned to the option
if(!extra) {
extra = this.extra() || '';
if(extra) {
// collapse whitespace so that [file ...] becomes [file...]
extra = extra.replace(/\s+(.+)/, "$1");
// use a consistent assignment style
extra = extra.replace(/^(.?)=(.*)$/, "$1$2");
}
}else if(extra === true){
extra = this.extra();
}
}
if(extra && typeof extra === 'string') {
// custom extra specified
extra = assignment + extra;
}
return this.toString(delimiter, names)
+ (typeof extra === 'string' ? extra : '');
}
define(Argument.prototype, 'getOptionString', getOptionString, false);
/**
* Determines whether an array consists solely of functions
* allowing for the special case JSON.
*
* @param arr The array candidate.
*/
Argument.prototype.isFunctionArray = function(arr) {
if(!Array.isArray(arr)) return false;
var i, item;
for(i = 0;i < arr.length;i++) {
item = arr[i];
if(item !== JSON && !(typeof item == 'function')) return false;
}
return true;
}
keys = Object.keys(mutators.arg);
keys.forEach(function(name) {
var read = function() {
return this['_' + name];
}
var write = function(value) {
var key = '_' + name;
if(!arguments.length) return this[key];
this[key] = value;
return this;
}
define(Argument.prototype, name, mutators.arg[name] ? write : read, false);
})
/**
* Represents an option argument.
*/
function Option() {
Argument.apply(this, arguments);
}
util.inherits(Option, Argument);
/**
* Represents a flag argument.
*/
function Flag() {
Argument.apply(this, arguments);
//console.log('Flag %s %s', this._name, this._description);
//this._value = false;
}
util.inherits(Flag, Argument);
/**
* Represents a command argument.
*/
function Command(name, description, options) {
//events.EventEmitter.call(this);
if(typeof name == 'object') options = name;
// private
define(this, '_parent', undefined, true);
define(this, '_commands', {}, true);
define(this, '_options', {}, true);
define(this, '_sections', {}, true);
define(this, '_name', name || '', true);
define(this, '_description', description || '', true);
define(this, '_detail', undefined, true);
define(this, '_key', '', true);
define(this, '_exec', {}, false);
define(this, '_action', undefined, true);
define(this, '_converter', undefined, true);
define(this, '_names', undefined, true);
define(this, '_last', undefined, true);
define(this, '_usage', undefined, true);
define(this, '_extra', undefined, true);
//define(this, '_package', undefined, true);
// helper functions for finding commands, options etc.
var finder = {};
for(var k in helpers) {
finder[k] = helpers[k].bind(this);
}
define(this, 'finder', finder, false);
// event emitter
define(this, '_emitter', new events.EventEmitter(), false);
define(this, '_events', undefined, true);
define(this, '_maxListeners', undefined, true);
define(this, 'domain', undefined, true);
if((typeof options == 'object')) {
initialize.call(this, options, Object.keys(mutators.cmd));
}
if(!this._name || typeof this._name !== 'string') {
throw new TypeError('Invalid command name \'' + this._name + '\'');
}
this._names = this._name.split(re.delimiter());
this._key = getKey.call(this);
}
define(Command.prototype, 'description', description, false);
define(Command.prototype, 'detail', detail, false);
define(Command.prototype, 'getOptionString', getOptionString, false);
define(Command.prototype, 'toString', toString, false);
define(Command.prototype, 'toObject', toObject, false);
var creator = {
createOption: function(name, description, options) {
return new Option(name, description, options);
},
createFlag: function(name, description, options) {
return new Flag(name, description, options);
},
createCommand: function(name, description, options) {
return new Command(name, description, options);
}
}
// create* functions
for(k in creator) {
define(Command.prototype, k, creator[k], false);
}
// is* functions
for(k in is) {
define(Command.prototype, k, is[k], false);
}
/**
* Set options as an object group.
*/
function options(value) {
if(!arguments.length) return this._options;
this._options = value;
// convert plain objects with *name*
// to Option or Flag instances
var k, v, clazz;
for(k in this._options) {
v = this._options[k];
if(v && typeof v === 'object' && !(v instanceof Argument) && v.name) {
if(v.key) k = v.key;
clazz = getClassByName(v.name);
this._options[k] = new clazz(v);
this._options[k].key(k);
//this._options[k].parent(this);
}
}
return this;
}
define(Command.prototype, 'options', options, true);
function hasOptions() {
for(var k in this._options) {
return true;
}
return false;
}
define(Command.prototype, 'hasOptions', hasOptions, true);
/**
* Set commands as an object group.
*/
function commands(value) {
if(!arguments.length) return this._commands;
this._commands = value;
// convert plain objects with *name*
// to Command instances
var k, v;
for(k in this._commands) {
v = this._commands[k];
if(v && typeof v === 'object' && !(v instanceof Command) && v.name) {
if(v.key) k = v.key;
this._commands[k] = new Command(v);
this._commands[k].parent(this);
this._commands[k].key(k);
}
}
return this;
}
define(Command.prototype, 'commands', commands, true);
function hasCommands() {
for(var k in this._commands) {
return true;
}
return false;
}
define(Command.prototype, 'hasCommands', hasCommands, true);
// define action so we can clear execs list
function action(value) {
if(!arguments.length) return this._action;
this._action = value;
if(this.parent() && this.parent()._exec) {
delete this.parent()._exec[this.key()];
}
return this;
}
define(Command.prototype, 'action', action, false);
function getLongName() {
return this._names.reduce(function(a, b) {
return a.length > b.length ? a : b;
});
}
define(Command.prototype, 'getLongName', getLongName, false);
function getShortName() {
return this._names.reduce(function(a, b) {
return a.length < b.length ? a : b;
});
}
define(Command.prototype, 'getShortName', getShortName, false);
function getParents(reverse, include, omit) {
var list = include ? [this] : [];
var p = this.parent();
while(p) {
list.push(p);
p = p.parent();
}
// omit root program part
if(omit && list.length > 1) list.pop();
if(reverse) list.reverse();
return list;
}
define(Command.prototype, 'getParents', getParents, false);
function getFullName(delimiter, reverse, include, omit) {
delimiter = delimiter || '-';
reverse = reverse !== undefined ? reverse : true;
include = include !== undefined ? include : true;
omit = omit !== undefined ? omit : false;
var list = this.getParents(reverse, include, omit);
list.forEach(function(cmd, index, arr) {
arr[index] = cmd.getLongName();
})
return list.join(delimiter);
}
define(Command.prototype, 'getFullName', getFullName, false);
/**
* Get or set the command usage.
*
* @param usage The command usage string.
*/
function usage(usage) {
if(!arguments.length) return this._usage;
this._usage = usage;
return this;
}
define(Command.prototype, 'usage', usage, false);
for(k in EventProxy) {
define(Command.prototype, k, EventProxy[k], false);
}
keys = Object.keys(mutators.cmd);
keys.forEach(function(name) {
if(Command.prototype[name]) return;
var read = function() {
return this['_' + name];
}
var write = function(value) {
var key = '_' + name;
if(value === undefined) return this[key];
this[key] = value;
return this;
}
define(Command.prototype, name, mutators.cmd[name] ? write : read, false);
})
/**
* Define a command argument.
*/
function command(name, description, options) {
var opt = (name instanceof Command) ? name
: new Command(name, description, options);
this._commands[opt.key()] = opt;
this._last = this._commands[opt.key()];
this._last.parent(this);
if(description) {
this._exec[opt.key()] = opt;
}
return description ? this : opt;
}
define(Command.prototype, 'command', command, false);
/**
* Inspect an argument name and determine the class (type)
* of argument: Option or Flag.
*/
function getClassByName(name) {
var clazz = Option;
if(typeof name === 'string') {
if(re.no().test(name) || !re.extra().test(name)) {
clazz = Flag;
}
}
return clazz;
}
/**
* Define an option argument.
*/
function option(name, description, options, coerce, value) {
var clazz = getClassByName(name);
var opt = (name instanceof Option) || (name instanceof Flag) ? name
: new clazz(name, description, options, coerce, value);
this._options[opt.key()] = opt;
this._last = this._options[opt.key()];
return this;
}
define(Command.prototype, 'option', option, false);
/**
* Define a flag argument explicitly.
*/
function flag(name, description, options, coerce, value) {
var opt = (name instanceof Flag) ? name
: new Flag(name, description, options, coerce, value);
this._options[opt.key()] = opt;
this._last = this._options[opt.key()];
return this;
}
define(Command.prototype, 'flag', flag, false);
/**
* Represents the program.
*/
function Program() {
Command.apply(this, arguments);
define(this, '_version', '0.0.1', true);
define(this, '_package', undefined, true);
define(this, '_configure', {}, true);
}
util.inherits(Program, Command);
keys = Object.keys(mutators.prg);
keys.forEach(function(name) {
var write = function(value) {
var key = '_' + name;
if(!arguments.length) return this[key];
this[key] = value;
return this;
}
define(Program.prototype, name, write, false);
})
/**
* Get and set the program version.
*
* @param semver A specific version number.
*/
function version(semver) {
if(!arguments.length) return this._version;
this._version = semver;
}
define(Program.prototype, 'version', version, false);
/**
* Assign a value to an option.
*
* @param arg The argument definition.
* @param key The option key.
* @param value The value for the option.
*/
function assign(arg, key, value) {
var receiver = this.configure().stash || this;
receiver[key] = value;
if(arg) arg.value(value);
}
define(Program.prototype, 'assign', assign, false);
/**
* Set the program package descriptor.
*
* @param path The path to the package descriptor or an existing
* package object.
*/
function package(path) {
if(!arguments.length) return this._package;
if(arguments.length === 1 && !path) {
this._package = path;
return this._package;
}
var pkg;
if(path && typeof(path) === 'string') {
try {
pkg = this._package = require(path);
}catch(e) {
throw new Error(util.format(
'package error %s (%s)', path, e.message.toLowerCase()));
}
}else if(path && typeof path === 'object') {
pkg = this._package = path;
}
if(!pkg) {
throw new Error(util.format(
'package descriptor %s does not exist', path));
}
this._version = pkg.version;
if(pkg.description) this._description = pkg.description;
return this;
}
define(Program.prototype, 'package', package, false);
/**
* Initialize the program from a package.json
* project descriptor, optionally overriding
* the name, description and class to instantiate.
*
* @param package The path to package.json.
* @param name A specific name for the root command (optional).
* @param description A specific description for the root command (optional).
* @param clazz A specific class to instantiate.
*/
function create(package, name, description, clazz) {
clazz = clazz || Program;
var program = new clazz(name || basename(process.argv[1]));
program.package(package);
if(name) program.name(name);
if(description) program.description(description);
return program;
}
module.exports = create;
module.exports.re = re;
module.exports.define = define;
module.exports.initialize = initialize;
module.exports.mutators = mutators;
module.exports.key = getKey;
module.exports.Program = Program;
module.exports.Command = Command;
module.exports.Option = Option;
module.exports.Flag = Flag;
module.exports.Argument = Argument;
module.exports.sortNames = sortNames;
module.exports.Description = Description;