1 | var Parameter, REGEX_MULTIWORD, REGEX_OPTIONAL, REGEX_REQUIRED, REGEX_STDIN, REGEX_VARIADIC, STDIN_CHARACTER, _, parse;
|
2 |
|
3 | _ = require('lodash');
|
4 |
|
5 | _.str = require('underscore.string');
|
6 |
|
7 | parse = require('./parse');
|
8 |
|
9 | REGEX_REQUIRED = /^<(.*)>$/;
|
10 |
|
11 | REGEX_OPTIONAL = /^\[(.*)\]$/;
|
12 |
|
13 | REGEX_VARIADIC = /^[<\[](.*)[\.]{3}[>\]]$/;
|
14 |
|
15 | REGEX_MULTIWORD = /\s/;
|
16 |
|
17 | REGEX_STDIN = /^[<\[]\|(.*)[\]>]$/;
|
18 |
|
19 | STDIN_CHARACTER = '|';
|
20 |
|
21 | module.exports = Parameter = (function() {
|
22 | function Parameter(parameter) {
|
23 | if (_.isNumber(parameter)) {
|
24 | parameter = String(parameter);
|
25 | }
|
26 | if ((parameter == null) || !_.isString(parameter)) {
|
27 | throw new Error("Missing or invalid parameter: " + parameter);
|
28 | }
|
29 | this.parameter = parameter;
|
30 | if (this.isVariadic() && this.allowsStdin()) {
|
31 | throw new Error('Parameter can\'t be variadic and allow stdin');
|
32 | }
|
33 | }
|
34 |
|
35 | Parameter.prototype._testRegex = function(regex) {
|
36 | return regex.test(this.parameter);
|
37 | };
|
38 |
|
39 | Parameter.prototype.isRequired = function() {
|
40 | return this._testRegex(REGEX_REQUIRED);
|
41 | };
|
42 |
|
43 | Parameter.prototype.isOptional = function() {
|
44 | return this._testRegex(REGEX_OPTIONAL);
|
45 | };
|
46 |
|
47 | Parameter.prototype.isVariadic = function() {
|
48 | return this._testRegex(REGEX_VARIADIC);
|
49 | };
|
50 |
|
51 | Parameter.prototype.isWord = function() {
|
52 | return !_.any([this.isRequired(), this.isOptional()]);
|
53 | };
|
54 |
|
55 | Parameter.prototype.isMultiWord = function() {
|
56 | return this._testRegex(REGEX_MULTIWORD);
|
57 | };
|
58 |
|
59 | Parameter.prototype.allowsStdin = function() {
|
60 | return this.parameter[1] === STDIN_CHARACTER;
|
61 | };
|
62 |
|
63 | Parameter.prototype.getValue = function() {
|
64 | var regex, result;
|
65 | if (this.isWord()) {
|
66 | return this.parameter;
|
67 | }
|
68 | if (this.isRequired()) {
|
69 | regex = REGEX_REQUIRED;
|
70 | }
|
71 | if (this.isOptional()) {
|
72 | regex = REGEX_OPTIONAL;
|
73 | }
|
74 | if (this.isVariadic()) {
|
75 | regex = REGEX_VARIADIC;
|
76 | }
|
77 | if (this.allowsStdin()) {
|
78 | regex = REGEX_STDIN;
|
79 | }
|
80 | result = this.parameter.match(regex);
|
81 | return result[1];
|
82 | };
|
83 |
|
84 | Parameter.prototype.getType = function() {
|
85 | if (this.isWord()) {
|
86 | return 'word';
|
87 | }
|
88 | return 'parameter';
|
89 | };
|
90 |
|
91 | Parameter.prototype.matches = function(parameter) {
|
92 | var parameterWordsLength;
|
93 | if (this.isWord()) {
|
94 | return parameter === this.parameter;
|
95 | }
|
96 | parameterWordsLength = parse.split(parameter).length;
|
97 | if (this.isVariadic()) {
|
98 | if (this.isOptional()) {
|
99 | return true;
|
100 | }
|
101 | if (parameterWordsLength < 1) {
|
102 | return false;
|
103 | }
|
104 | } else {
|
105 | if (this.isRequired()) {
|
106 | if (parameterWordsLength < 1) {
|
107 | return false;
|
108 | }
|
109 | }
|
110 | }
|
111 | return true;
|
112 | };
|
113 |
|
114 | Parameter.prototype.toString = function() {
|
115 | if (this.isMultiWord() && this.isWord()) {
|
116 | return _.str.quote(this.parameter);
|
117 | }
|
118 | return this.parameter;
|
119 | };
|
120 |
|
121 | return Parameter;
|
122 |
|
123 | })();
|