-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (103 loc) · 3.07 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
/**
* Module to parse commandline-args and options.
* @author Muhammad Dadu
* @module arghandle
*/
const _ = require('underscore');
(function (){
/**
* @Constructor
* @param {object} options - defines the options for the argument parser
* @param {number} options.startIndex - index to start parsing arguments
* @param {array} options.argv - command line arguments from the system
* @param {array} options.definedArgs - an array of custom argument handlers
*/
function ArgHandle(options) {
options = options || {};
// create a index to store the itteration
this.startIndex = options.startIndex || 2;
// argv
this.argv = options.argv || [];
// create an array of args permitted
this.definedArgs = options.definedArgs || [];
// add default help args
this.definedArgs.push({
keys: ['--help', '-h'],
method: function (self) {
// log all the commands to the terminal [this.args]
console.log("\nUsage: script <command> \n");
console.log("Available commands:");
for (var argObjectIndex in self.definedArgs) {
var argObject = self.definedArgs[argObjectIndex];
console.log(" -", argObject.description);
console.log(" Usage: ", JSON.stringify(argObject.keys), "\n")
};
},
description: 'This argument shows the list of arguments available'
});
}
/**
* Concat custom argument handlers
* @param {array} argObjectArray - an array of custom argument handlers
*/
ArgHandle.prototype.concat = function (argObjectArray) {
// append customArgs to array
this.definedArgs = this.definedArgs.concat(argObjectArray);
}
/**
* Push custom argument handler
* @param {object} argObject - custom argument handler
*/
ArgHandle.prototype.push = function (argObject) {
// append customArgs to array
this.definedArgs.push(argObject);
}
/**
* Gets the next argument from argv
* @return {string} argument
*/
ArgHandle.prototype.getNext = function() {
this.index++;
return this.argv[this.index];
}
/**
* Starts to process the arguments and sent to the custom even handlers
* @param {system.argv} argv - command line arguments from the system
* @return {bool} status of processing arguments [false == failure]
*/
ArgHandle.prototype.process = function (argv) {
// store the arguments
this.argv = argv || this.argv;
// reset index
this.index = this.startIndex;
// Process enviromental variables
for (var argl = this.argv.length; this.index < argl; this.index++) {
var arg = this.argv[this.index],
argObject = _.find(this.definedArgs, function (object) {
if (object.keys.indexOf(arg) > -1) {
return object;
}
});
if (!argObject) {
console.error('Undefined argument', arg);
return false;
}
argObject.method.call(this, this);
}
return true;
}
// Node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = ArgHandle;
}
// AMD / RequireJS
else if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return ArgHandle;
});
}
// included directly via <script> tag
else {
root.arghandle = ArgHandle;
}
}());