/
cmd-conf.js
283 lines (246 loc) · 6.63 KB
/
cmd-conf.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
var logLevel = 1;
/*
* This file is part of CMD-Conf.
*
* CMD-Conf is distributed under the terms of the M.I.T License.
*
* Copyright © Vincent Peybernes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability, fitness
* for a particular purpose and noninfringement. In no event shall the authors or
* copyright holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or in
* connection with the software or the use or other dealings in the Software.
*/
/**
* Node CMD-Conf.
* Command line analyser for Node.JS.
* @author Vincent Peybernes [Techniv] <vpeybernes.pro@gmail.com>
* @version 0.3.0
*/
function cmdConf(cmd){
var logger = new LightLogger(logLevel);
var _path = require('path');
var that = this;
var command = {
itemList: cmd.slice(0),
cmdStr: '',
args: cmd.slice(2)
};
command.cmdStr = command.itemList.join(' ');
var conf = {
appPath: _path.dirname(global.process.mainModule.filename),
defaultConfigFile: './config.json',
processed: false,
configured: false,
regexp: /^(-{1,2})([a-zA-Z]+)$/,
key: {},
shortKey: {}
};
var parameters = {
parameters: [],
arguments: command.args,
commandStr: command.cmdStr
};
/**
* Configure the command analyser.
* @param object config The config object. Can be the path to
* the config JSON file.
*/
that.configure = function(config){
conf.processed = false;
parameters = {
arguments: [],
cmdArguments: command.args,
cmdStr: command.cmdStr
};
if(typeof config == 'undefined') config = conf.defaultConfigFile;
if(typeof config == "string"){
config = getConfigFromFile(config);
}
if(config._options){
options = config.options;
delete config._options;
}
for(var name in config){
var item = config[name];
processConfItem(name, item);
}
conf.configured = true;
return that;
};
/**
* Get the parameters. If the module are not configured, it try to
* load the default JSON config file.
* @return an object whith the catched parameter assotiate whith their key
*/
that.getParameters = function(){
if(!conf.processed) process();
return parameters;
};
function processConfItem(name, item){
if(typeof item.action == 'string' && typeof item.key == 'string'){
switch(item.action){
case 'get':
if(item.number == undefined){
logger.error('The number of get action is\' defined for \''+item.name+'\'.');
return false;
}
break;
case 'set':
if(item.value == undefined){
logger.warn('The set value of \''+item.name+' is not defined. Use true.');
item.value = true;
}
break;
default:
logger.error('The config property '+item.name+' has no action');
return false;
break;
}
item.name = name;
conf.key[item.key] = item;
if(item.shortKey) conf.shortKey[item.shortKey] = item;
if(item.defaultValue !== undefined) setParam(name, item.defaultValue);
} else {
setParam(name, item);
}
return true;
}
/**
* Fire the command line analyse
*/
function process(){
if (!conf.configured) that.configure();
var args = command.args.slice(0);
for(var i in args){
var arg = args[i];
if(conf.regexp.test(arg)){
var catchWord = RegExp.$2;
switch(RegExp.$1){
case '-':
processShortKey(catchWord, i, args);
break;
case '--':
processKey(catchWord, i, args);
break;
}
} else {
addArgument(arg);
}
}
conf.processed = true
}
function processKey(word, position, args){
var option = conf.key[word];
if(!option) return;
option.position = parseInt(position);
processOption(option, args);
}
function processShortKey(word, position, args){
var option = conf.shortKey[word];
if(!option) return;
option.position = parseInt(position);
processOption(option, args);
}
function processOption(option, args){
var name = option.name;
var action = option.action;
switch(action){
case 'get':
var params = getCmdParam(option.position+1, option.number, args);
setParam(name,params);
break;
case 'set':
var value = (option.value) ? option.value : undefined;
setParam(name,value);
break;
default:
break;
}
}
function getCmdParam(start, num, args){
var params = args.slice(start,start+num);
var assign = [];
for(var i in params){
var param = params[i];
if(/^-{1,2}/.test(param)) break;
else{
if(/^[0-9]+(?:(\.)[0-9]+)?$/.test(param)){
params[i] = (RegExp.$1 == '.')? parseFloat(param) : parseInt(param);
}
assign.push(params[i]);
}
}
args.splice(start,assign.length);
return num == 1 ? assign[0] : assign;
}
function setParam(key, value){
parameters[key] = value;
}
function addArgument(value){
parameters.arguments.push(value);
}
function getConfigFromFile(filePath){
logger.info('Read cmd-conf configurtion from '+filePath);
var fs = require('fs');
var path = _path;
filePath = path.resolve(conf.appPath,filePath);
if(!fs.existsSync(filePath)){
logger.error('Can\'t find '+filePath);
return;
}
try{
var content = fs.readFileSync(filePath).toString();
} catch(err){
logger.error(err.name+': Can\'t read file \''+filePath+'\'');
return;
}
try{
content = JSON.parse(content);
} catch (err){
logger.error(err.name+': The JSON file is\'nt correctly formed');
return;
}
return content;
}
}
/**
* LightLogger using basic console log.
* LogLevel :
* 0 Nothing
* 1 Error
* 2 Warning
* 3 Info
* 4 Debug
*/
function LightLogger(logLevel){
logger = global.console;
this.log = function(msg){
if(logLevel >= 4) logger.log(msg);
}
this.debbug = function(msg){
if(logLevel >= 4) logger.log(msg);
}
this.info = function(msg){
if(logLevel >= 3) logger.info(msg);
}
this.warn = function(msg){
if(logLevel >= 2) logger.warn(msg);
}
this.error = function(msg){
if(logLevel >= 1) logger.error(msg);
}
}
module.exports = new cmdConf(process.argv);