1 |
|
2 |
|
3 |
|
4 |
|
5 | var EventEmitter = require('events').EventEmitter;
|
6 | var spawn = require('child_process').spawn;
|
7 | var path = require('path');
|
8 | var dirname = path.dirname;
|
9 | var basename = path.basename;
|
10 | var fs = require('fs');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | exports = module.exports = new Command();
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.Command = Command;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | exports.Option = Option;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function Option(flags, description) {
|
39 | this.flags = flags;
|
40 | this.required = ~flags.indexOf('<');
|
41 | this.optional = ~flags.indexOf('[');
|
42 | this.bool = !~flags.indexOf('-no-');
|
43 | flags = flags.split(/[ ,|]+/);
|
44 | if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
|
45 | this.long = flags.shift();
|
46 | this.description = description || '';
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | Option.prototype.name = function() {
|
57 | return this.long
|
58 | .replace('--', '')
|
59 | .replace('no-', '');
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | Option.prototype.is = function(arg) {
|
71 | return arg == this.short || arg == this.long;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function Command(name) {
|
82 | this.commands = [];
|
83 | this.options = [];
|
84 | this._execs = {};
|
85 | this._allowUnknownOption = false;
|
86 | this._args = [];
|
87 | this._name = name || '';
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | Command.prototype.__proto__ = EventEmitter.prototype;
|
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 | Command.prototype.command = function(name, desc, opts) {
|
158 | opts = opts || {};
|
159 | var args = name.split(/ +/);
|
160 | var cmd = new Command(args.shift());
|
161 |
|
162 | if (desc) {
|
163 | cmd.description(desc);
|
164 | this.executables = true;
|
165 | this._execs[cmd._name] = true;
|
166 | if (opts.isDefault) this.defaultExecutable = cmd._name;
|
167 | }
|
168 |
|
169 | cmd._noHelp = !!opts.noHelp;
|
170 | this.commands.push(cmd);
|
171 | cmd.parseExpectedArgs(args);
|
172 | cmd.parent = this;
|
173 |
|
174 | if (desc) return this;
|
175 | return cmd;
|
176 | };
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | Command.prototype.arguments = function (desc) {
|
185 | return this.parseExpectedArgs(desc.split(/ +/));
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | Command.prototype.addImplicitHelpCommand = function() {
|
196 | this.command('help [cmd]', 'display help for [cmd]');
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | Command.prototype.parseExpectedArgs = function(args) {
|
210 | if (!args.length) return;
|
211 | var self = this;
|
212 | args.forEach(function(arg) {
|
213 | var argDetails = {
|
214 | required: false,
|
215 | name: '',
|
216 | variadic: false
|
217 | };
|
218 |
|
219 | switch (arg[0]) {
|
220 | case '<':
|
221 | argDetails.required = true;
|
222 | argDetails.name = arg.slice(1, -1);
|
223 | break;
|
224 | case '[':
|
225 | argDetails.name = arg.slice(1, -1);
|
226 | break;
|
227 | }
|
228 |
|
229 | if (argDetails.name.length > 3 && argDetails.name.slice(-3) === '...') {
|
230 | argDetails.variadic = true;
|
231 | argDetails.name = argDetails.name.slice(0, -3);
|
232 | }
|
233 | if (argDetails.name) {
|
234 | self._args.push(argDetails);
|
235 | }
|
236 | });
|
237 | return this;
|
238 | };
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | Command.prototype.action = function(fn) {
|
258 | var self = this;
|
259 | var listener = function(args, unknown) {
|
260 |
|
261 | args = args || [];
|
262 | unknown = unknown || [];
|
263 |
|
264 | var parsed = self.parseOptions(unknown);
|
265 |
|
266 |
|
267 | outputHelpIfNecessary(self, parsed.unknown);
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | if (parsed.unknown.length > 0) {
|
273 | self.unknownOption(parsed.unknown[0]);
|
274 | }
|
275 |
|
276 |
|
277 | if (parsed.args.length) args = parsed.args.concat(args);
|
278 |
|
279 | self._args.forEach(function(arg, i) {
|
280 | if (arg.required && null == args[i]) {
|
281 | self.missingArgument(arg.name);
|
282 | } else if (arg.variadic) {
|
283 | if (i !== self._args.length - 1) {
|
284 | self.variadicArgNotLast(arg.name);
|
285 | }
|
286 |
|
287 | args[i] = args.splice(i);
|
288 | }
|
289 | });
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | if (self._args.length) {
|
295 | args[self._args.length] = self;
|
296 | } else {
|
297 | args.push(self);
|
298 | }
|
299 |
|
300 | fn.apply(self, args);
|
301 | };
|
302 | var parent = this.parent || this;
|
303 | var name = parent === this ? '*' : this._name;
|
304 | parent.on('command:' + name, listener);
|
305 | if (this._alias) parent.on('command:' + this._alias, listener);
|
306 | return this;
|
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 | Command.prototype.option = function(flags, description, fn, defaultValue) {
|
359 | var self = this
|
360 | , option = new Option(flags, description)
|
361 | , oname = option.name()
|
362 | , name = camelcase(oname);
|
363 |
|
364 |
|
365 | if (typeof fn != 'function') {
|
366 | if (fn instanceof RegExp) {
|
367 | var regex = fn;
|
368 | fn = function(val, def) {
|
369 | var m = regex.exec(val);
|
370 | return m ? m[0] : def;
|
371 | }
|
372 | }
|
373 | else {
|
374 | defaultValue = fn;
|
375 | fn = null;
|
376 | }
|
377 | }
|
378 |
|
379 |
|
380 | if (false == option.bool || option.optional || option.required) {
|
381 |
|
382 | if (false == option.bool) defaultValue = true;
|
383 |
|
384 | if (undefined !== defaultValue) self[name] = defaultValue;
|
385 | }
|
386 |
|
387 |
|
388 | this.options.push(option);
|
389 |
|
390 |
|
391 |
|
392 | this.on('option:' + oname, function(val) {
|
393 |
|
394 | if (null !== val && fn) val = fn(val, undefined === self[name]
|
395 | ? defaultValue
|
396 | : self[name]);
|
397 |
|
398 |
|
399 | if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) {
|
400 |
|
401 | if (null == val) {
|
402 | self[name] = option.bool
|
403 | ? defaultValue || true
|
404 | : false;
|
405 | } else {
|
406 | self[name] = val;
|
407 | }
|
408 | } else if (null !== val) {
|
409 |
|
410 | self[name] = val;
|
411 | }
|
412 | });
|
413 |
|
414 | return this;
|
415 | };
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | Command.prototype.allowUnknownOption = function(arg) {
|
425 | this._allowUnknownOption = arguments.length === 0 || arg;
|
426 | return this;
|
427 | };
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | Command.prototype.parse = function(argv) {
|
438 |
|
439 | if (this.executables) this.addImplicitHelpCommand();
|
440 |
|
441 |
|
442 | this.rawArgs = argv;
|
443 |
|
444 |
|
445 | this._name = this._name || basename(argv[1], '.js');
|
446 |
|
447 |
|
448 | if (this.executables && argv.length < 3 && !this.defaultExecutable) {
|
449 |
|
450 | argv.push('--help');
|
451 | }
|
452 |
|
453 |
|
454 | var parsed = this.parseOptions(this.normalize(argv.slice(2)));
|
455 | var args = this.args = parsed.args;
|
456 |
|
457 | var result = this.parseArgs(this.args, parsed.unknown);
|
458 |
|
459 |
|
460 | var name = result.args[0];
|
461 |
|
462 | var aliasCommand = null;
|
463 |
|
464 | if (name) {
|
465 | aliasCommand = this.commands.filter(function(command) {
|
466 | return command.alias() === name;
|
467 | })[0];
|
468 | }
|
469 |
|
470 | if (this._execs[name] && typeof this._execs[name] != "function") {
|
471 | return this.executeSubCommand(argv, args, parsed.unknown);
|
472 | } else if (aliasCommand) {
|
473 |
|
474 | args[0] = aliasCommand._name;
|
475 | return this.executeSubCommand(argv, args, parsed.unknown);
|
476 | } else if (this.defaultExecutable) {
|
477 |
|
478 | args.unshift(this.defaultExecutable);
|
479 | return this.executeSubCommand(argv, args, parsed.unknown);
|
480 | }
|
481 |
|
482 | return result;
|
483 | };
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 | Command.prototype.executeSubCommand = function(argv, args, unknown) {
|
495 | args = args.concat(unknown);
|
496 |
|
497 | if (!args.length) this.help();
|
498 | if ('help' == args[0] && 1 == args.length) this.help();
|
499 |
|
500 |
|
501 | if ('help' == args[0]) {
|
502 | args[0] = args[1];
|
503 | args[1] = '--help';
|
504 | }
|
505 |
|
506 |
|
507 | var f = argv[1];
|
508 |
|
509 | var bin = basename(f, '.js') + '-' + args[0];
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | var baseDir
|
515 | , link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f;
|
516 |
|
517 |
|
518 | if (link !== f && link.charAt(0) !== '/') {
|
519 | link = path.join(dirname(f), link)
|
520 | }
|
521 | baseDir = dirname(link);
|
522 |
|
523 |
|
524 | var localBin = path.join(baseDir, bin);
|
525 |
|
526 |
|
527 | var isExplicitJS = false;
|
528 | if (exists(localBin + '.js')) {
|
529 | bin = localBin + '.js';
|
530 | isExplicitJS = true;
|
531 | } else if (exists(localBin)) {
|
532 | bin = localBin;
|
533 | }
|
534 |
|
535 | args = args.slice(1);
|
536 |
|
537 | var proc;
|
538 | if (process.platform !== 'win32') {
|
539 | if (isExplicitJS) {
|
540 | args.unshift(bin);
|
541 |
|
542 | args = (process.execArgv || []).concat(args);
|
543 |
|
544 | proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
|
545 | } else {
|
546 | proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
|
547 | }
|
548 | } else {
|
549 | args.unshift(bin);
|
550 | proc = spawn(process.execPath, args, { stdio: 'inherit'});
|
551 | }
|
552 |
|
553 | var signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
|
554 | signals.forEach(function(signal) {
|
555 | process.on(signal, function(){
|
556 | if ((proc.killed === false) && (proc.exitCode === null)){
|
557 | proc.kill(signal);
|
558 | }
|
559 | });
|
560 | });
|
561 | proc.on('close', process.exit.bind(process));
|
562 | proc.on('error', function(err) {
|
563 | if (err.code == "ENOENT") {
|
564 | console.error('\n %s(1) does not exist, try --help\n', bin);
|
565 | } else if (err.code == "EACCES") {
|
566 | console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
|
567 | }
|
568 | process.exit(1);
|
569 | });
|
570 |
|
571 |
|
572 | this.runningCommand = proc;
|
573 | };
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 | Command.prototype.normalize = function(args) {
|
586 | var ret = []
|
587 | , arg
|
588 | , lastOpt
|
589 | , index;
|
590 |
|
591 | for (var i = 0, len = args.length; i < len; ++i) {
|
592 | arg = args[i];
|
593 | if (i > 0) {
|
594 | lastOpt = this.optionFor(args[i-1]);
|
595 | }
|
596 |
|
597 | if (arg === '--') {
|
598 |
|
599 | ret = ret.concat(args.slice(i));
|
600 | break;
|
601 | } else if (lastOpt && lastOpt.required) {
|
602 | ret.push(arg);
|
603 | } else if (arg.length > 1 && '-' == arg[0] && '-' != arg[1]) {
|
604 | arg.slice(1).split('').forEach(function(c) {
|
605 | ret.push('-' + c);
|
606 | });
|
607 | } else if (/^--/.test(arg) && ~(index = arg.indexOf('='))) {
|
608 | ret.push(arg.slice(0, index), arg.slice(index + 1));
|
609 | } else {
|
610 | ret.push(arg);
|
611 | }
|
612 | }
|
613 |
|
614 | return ret;
|
615 | };
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 | Command.prototype.parseArgs = function(args, unknown) {
|
630 | var name;
|
631 |
|
632 | if (args.length) {
|
633 | name = args[0];
|
634 | if (this.listeners('command:' + name).length) {
|
635 | this.emit('command:' + args.shift(), args, unknown);
|
636 | } else {
|
637 | this.emit('command:*', args);
|
638 | }
|
639 | } else {
|
640 | outputHelpIfNecessary(this, unknown);
|
641 |
|
642 |
|
643 |
|
644 | if (unknown.length > 0) {
|
645 | this.unknownOption(unknown[0]);
|
646 | }
|
647 | }
|
648 |
|
649 | return this;
|
650 | };
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 | Command.prototype.optionFor = function(arg) {
|
661 | for (var i = 0, len = this.options.length; i < len; ++i) {
|
662 | if (this.options[i].is(arg)) {
|
663 | return this.options[i];
|
664 | }
|
665 | }
|
666 | };
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 | Command.prototype.parseOptions = function(argv) {
|
678 | var args = []
|
679 | , len = argv.length
|
680 | , literal
|
681 | , option
|
682 | , arg;
|
683 |
|
684 | var unknownOptions = [];
|
685 |
|
686 |
|
687 | for (var i = 0; i < len; ++i) {
|
688 | arg = argv[i];
|
689 |
|
690 |
|
691 | if (literal) {
|
692 | args.push(arg);
|
693 | continue;
|
694 | }
|
695 |
|
696 | if ('--' == arg) {
|
697 | literal = true;
|
698 | continue;
|
699 | }
|
700 |
|
701 |
|
702 | option = this.optionFor(arg);
|
703 |
|
704 |
|
705 | if (option) {
|
706 |
|
707 | if (option.required) {
|
708 | arg = argv[++i];
|
709 | if (null == arg) return this.optionMissingArgument(option);
|
710 | this.emit('option:' + option.name(), arg);
|
711 |
|
712 | } else if (option.optional) {
|
713 | arg = argv[i+1];
|
714 | if (null == arg || ('-' == arg[0] && '-' != arg)) {
|
715 | arg = null;
|
716 | } else {
|
717 | ++i;
|
718 | }
|
719 | this.emit('option:' + option.name(), arg);
|
720 |
|
721 | } else {
|
722 | this.emit('option:' + option.name());
|
723 | }
|
724 | continue;
|
725 | }
|
726 |
|
727 |
|
728 | if (arg.length > 1 && '-' == arg[0]) {
|
729 | unknownOptions.push(arg);
|
730 |
|
731 |
|
732 |
|
733 |
|
734 | if (argv[i+1] && '-' != argv[i+1][0]) {
|
735 | unknownOptions.push(argv[++i]);
|
736 | }
|
737 | continue;
|
738 | }
|
739 |
|
740 |
|
741 | args.push(arg);
|
742 | }
|
743 |
|
744 | return { args: args, unknown: unknownOptions };
|
745 | };
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 | Command.prototype.opts = function() {
|
754 | var result = {}
|
755 | , len = this.options.length;
|
756 |
|
757 | for (var i = 0 ; i < len; i++) {
|
758 | var key = camelcase(this.options[i].name());
|
759 | result[key] = key === 'version' ? this._version : this[key];
|
760 | }
|
761 | return result;
|
762 | };
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 | Command.prototype.missingArgument = function(name) {
|
772 | console.error();
|
773 | console.error(" error: missing required argument `%s'", name);
|
774 | console.error();
|
775 | process.exit(1);
|
776 | };
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | Command.prototype.optionMissingArgument = function(option, flag) {
|
787 | console.error();
|
788 | if (flag) {
|
789 | console.error(" error: option `%s' argument missing, got `%s'", option.flags, flag);
|
790 | } else {
|
791 | console.error(" error: option `%s' argument missing", option.flags);
|
792 | }
|
793 | console.error();
|
794 | process.exit(1);
|
795 | };
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 | Command.prototype.unknownOption = function(flag) {
|
805 | if (this._allowUnknownOption) return;
|
806 | console.error();
|
807 | console.error(" error: unknown option `%s'", flag);
|
808 | console.error();
|
809 | process.exit(1);
|
810 | };
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | Command.prototype.variadicArgNotLast = function(name) {
|
820 | console.error();
|
821 | console.error(" error: variadic arguments must be last `%s'", name);
|
822 | console.error();
|
823 | process.exit(1);
|
824 | };
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 | Command.prototype.version = function(str, flags) {
|
839 | if (0 == arguments.length) return this._version;
|
840 | this._version = str;
|
841 | flags = flags || '-V, --version';
|
842 | this.option(flags, 'output the version number');
|
843 | this.on('option:version', function() {
|
844 | process.stdout.write(str + '\n');
|
845 | process.exit(0);
|
846 | });
|
847 | return this;
|
848 | };
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 | Command.prototype.description = function(str) {
|
859 | if (0 === arguments.length) return this._description;
|
860 | this._description = str;
|
861 | return this;
|
862 | };
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 | Command.prototype.alias = function(alias) {
|
873 | var command = this;
|
874 | if(this.commands.length !== 0) {
|
875 | command = this.commands[this.commands.length - 1]
|
876 | }
|
877 |
|
878 | if (arguments.length === 0) return command._alias;
|
879 |
|
880 | command._alias = alias;
|
881 | return this;
|
882 | };
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 | Command.prototype.usage = function(str) {
|
893 | var args = this._args.map(function(arg) {
|
894 | return humanReadableArgName(arg);
|
895 | });
|
896 |
|
897 | var usage = '[options]'
|
898 | + (this.commands.length ? ' [command]' : '')
|
899 | + (this._args.length ? ' ' + args.join(' ') : '');
|
900 |
|
901 | if (0 == arguments.length) return this._usage || usage;
|
902 | this._usage = str;
|
903 |
|
904 | return this;
|
905 | };
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 | Command.prototype.name = function(str) {
|
916 | if (0 === arguments.length) return this._name;
|
917 | this._name = str;
|
918 | return this;
|
919 | };
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 | Command.prototype.largestOptionLength = function() {
|
929 | return this.options.reduce(function(max, option) {
|
930 | return Math.max(max, option.flags.length);
|
931 | }, 0);
|
932 | };
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 | Command.prototype.optionHelp = function() {
|
942 | var width = this.largestOptionLength();
|
943 |
|
944 |
|
945 | return this.options.map(function(option) {
|
946 | return pad(option.flags, width) + ' ' + option.description;
|
947 | }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
|
948 | .join('\n');
|
949 | };
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 | Command.prototype.commandHelp = function() {
|
959 | if (!this.commands.length) return '';
|
960 |
|
961 | var commands = this.commands.filter(function(cmd) {
|
962 | return !cmd._noHelp;
|
963 | }).map(function(cmd) {
|
964 | var args = cmd._args.map(function(arg) {
|
965 | return humanReadableArgName(arg);
|
966 | }).join(' ');
|
967 |
|
968 | return [
|
969 | cmd._name
|
970 | + (cmd._alias ? '|' + cmd._alias : '')
|
971 | + (cmd.options.length ? ' [options]' : '')
|
972 | + ' ' + args
|
973 | , cmd._description
|
974 | ];
|
975 | });
|
976 |
|
977 | var width = commands.reduce(function(max, command) {
|
978 | return Math.max(max, command[0].length);
|
979 | }, 0);
|
980 |
|
981 | return [
|
982 | ''
|
983 | , ' Commands:'
|
984 | , ''
|
985 | , commands.map(function(cmd) {
|
986 | var desc = cmd[1] ? ' ' + cmd[1] : '';
|
987 | return pad(cmd[0], width) + desc;
|
988 | }).join('\n').replace(/^/gm, ' ')
|
989 | , ''
|
990 | ].join('\n');
|
991 | };
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 | Command.prototype.helpInformation = function() {
|
1001 | var desc = [];
|
1002 | if (this._description) {
|
1003 | desc = [
|
1004 | ' ' + this._description
|
1005 | , ''
|
1006 | ];
|
1007 | }
|
1008 |
|
1009 | var cmdName = this._name;
|
1010 | if (this._alias) {
|
1011 | cmdName = cmdName + '|' + this._alias;
|
1012 | }
|
1013 | var usage = [
|
1014 | ''
|
1015 | ,' Usage: ' + cmdName + ' ' + this.usage()
|
1016 | , ''
|
1017 | ];
|
1018 |
|
1019 | var cmds = [];
|
1020 | var commandHelp = this.commandHelp();
|
1021 | if (commandHelp) cmds = [commandHelp];
|
1022 |
|
1023 | var options = [
|
1024 | ''
|
1025 | , ' Options:'
|
1026 | , ''
|
1027 | , '' + this.optionHelp().replace(/^/gm, ' ')
|
1028 | , ''
|
1029 | ];
|
1030 |
|
1031 | return usage
|
1032 | .concat(desc)
|
1033 | .concat(options)
|
1034 | .concat(cmds)
|
1035 | .join('\n');
|
1036 | };
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 | Command.prototype.outputHelp = function(cb) {
|
1045 | if (!cb) {
|
1046 | cb = function(passthru) {
|
1047 | return passthru;
|
1048 | }
|
1049 | }
|
1050 | process.stdout.write(cb(this.helpInformation()));
|
1051 | this.emit('--help');
|
1052 | };
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 | Command.prototype.help = function(cb) {
|
1061 | this.outputHelp(cb);
|
1062 | process.exit();
|
1063 | };
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 | function camelcase(flag) {
|
1074 | return flag.split('-').reduce(function(str, word) {
|
1075 | return str + word[0].toUpperCase() + word.slice(1);
|
1076 | });
|
1077 | }
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 | function pad(str, width) {
|
1089 | var len = Math.max(0, width - str.length);
|
1090 | return str + Array(len + 1).join(' ');
|
1091 | }
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 | function outputHelpIfNecessary(cmd, options) {
|
1102 | options = options || [];
|
1103 | for (var i = 0; i < options.length; i++) {
|
1104 | if (options[i] == '--help' || options[i] == '-h') {
|
1105 | cmd.outputHelp();
|
1106 | process.exit(0);
|
1107 | }
|
1108 | }
|
1109 | }
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 | function humanReadableArgName(arg) {
|
1120 | var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
|
1121 |
|
1122 | return arg.required
|
1123 | ? '<' + nameOutput + '>'
|
1124 | : '[' + nameOutput + ']'
|
1125 | }
|
1126 |
|
1127 |
|
1128 | function exists(file) {
|
1129 | try {
|
1130 | if (fs.statSync(file).isFile()) {
|
1131 | return true;
|
1132 | }
|
1133 | } catch (e) {
|
1134 | return false;
|
1135 | }
|
1136 | }
|
1137 |
|