This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
/
command.js
243 lines (198 loc) · 5.38 KB
/
command.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
/* lib/command.js */
var fs = require('fs'),
util = require('util'),
slice = Array.prototype.slice;
require('./extensions.js');
/**
Command Line class
@private
@constructor
@class CommandLine
@param {object} commands Commands object to pass
*/
function CommandLine(commands) {
this.keys = Object.keys(commands);
this.commands = commands;
this.args = process.argv.slice(2);
this.context = this.args[0] || null;
this.help = {
before: '',
after: ''
}
}
/**
Parses the command line arguments
@private
*/
CommandLine.prototype.parse = function() {
var count = this.args.length,
command = this.commands[this.context];
if (count < 2 && command && command.args > 0 && !command.noargs) {
// Print help
this.printHelp();
} else if (this.context in this.commands) {
var args = this.generateArgsObject(command);
if (command.validate instanceof Function) command.validate.call(this, args);
if (command.run instanceof Function) {
command.run.call(this, args);
} else {
console.exit('No run method specified for ' + this.context);
}
} else {
// Print help
this.printHelp();
}
}
/**
Prints the help message in the console
@private
*/
CommandLine.prototype.printHelp = function() {
console.log(this.help.before);
var commands = Object.keys(this.commands);
var sharedOptions = {};
commands.forEach(function(action) {
var sharedHelp = this.commands[action].sharedHelp || null;
if (sharedHelp) {
for (var key in sharedHelp) {
sharedHelp[key].forEach(function(option) {
var arr = sharedOptions[option] || (sharedOptions[option] = []);
if (arr.indexOf(action) === -1) arr.push(action);
if (arr.indexOf(key) === -1) arr.push(key);
arr.sort();
});
}
}
}, this);
commands.forEach(function(action) {
var data = this.commands[action].help || null;
if (data) {
var key, str;
for (key in data) {
var thisAction = sharedOptions[key] ? sharedOptions[key].join(',') : action;
str = padString(' --'+key, 18) + data[key] + ' (' + thisAction + ')';
console.log(str);
}
}
}, this);
console.log(this.help.after);
}
/**
Generates the arguments object
@private
@param {object} data
*/
CommandLine.prototype.generateArgsObject = function(data) {
var out = {args: []},
args = this.args.slice(1),
expect = data.args,
single = data.single || [],
multiple = data.multiple || [],
switches = data.switches || [];
var command = this.commands[this.context];
var noDuplicate = function(opt) {
opt = '--'+ opt;
if (args.countItem(opt) > 1) console.exit("Option can only be specified once: " + opt);
}
var skip = [];
// Single options
single.forEach(function(opt) { noDuplicate(opt);
var idx = args.indexOf('--'+opt);
if (idx >= 0) {
var val = args[idx+1],
next = args[idx+2];
if (!val || val.slice(0,2) == '--') {
console.exit(util.format("The --%s option requires one argument, none given.", opt));
} else if (next && next.slice(0, 2) != '--') {
console.exit(util.format("The --%s option requires one argument, multiple given", opt));
} else {
skip.push(idx);
skip.push(idx+1);
out[opt] = val;
}
} else {
out[opt] = null;
}
// console.exit(out);
}, this);
// Multiple options
multiple.forEach(function(opt) { noDuplicate(opt);
var last, idx = args.indexOf('--'+opt);
skip.push(idx);
if (idx >= 0) {
idx++;
for (var item,i=idx; i < args.length; i++) {
item = args[i];
skip.push(i);
if (item.slice(0,2) == '--') {
skip.pop();
last = i; break;
}
}
if (last === idx) {
out[opt] = [];
} else {
out[opt] = args.slice(idx, last);
}
} else {
out[opt] = [];
}
});
// Switches
switches.forEach(function(opt) { noDuplicate(opt);
var idx = args.indexOf('--'+opt);
if (idx >= 0) {
skip.push(idx);
out[opt] = true;
} else {
out[opt] = false;
}
});
// Get Arguments
for (var i=0; i < args.length; i++) {
if (skip.indexOf(i) >= 0) continue;
else if (args[i].slice(0,2) == '--') {
console.exit("Unknown option: " + args[i]);
} else {
out.args.push(args[i]);
}
}
// Remove duplicates
var key,val;
for (key in out) {
val = out[key];
if (val instanceof Array) {
var unique = [];
val.forEach(function(i) {
if (unique.indexOf(i) === -1) unique.push(i);
});
out[key] = unique;
}
}
// Check for too many args
if (out.args.length === 0) {
if (data.args > 0 && !command.noargs) {
console.exit("Not enough arguments provided.");
}
} else if (out.args.length > data.args) {
console.exit("Too many arguments provided.");
}
return out;
}
/**
Pads a string a certain amount of characters
*/
function padString(str, len) {
return str.split().concat(new Array(len-str.length)).join(' ');
}
/**
Counts the number of matches an item has in an array
*/
Array.prototype.countItem = function(item) {
var x,count = 0, len = this.length;
for (var i=0; i < len; i++) {
if (this[i] === item) count++;
}
return count;
}
module.exports = CommandLine;