-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.js
executable file
·152 lines (136 loc) · 5.17 KB
/
args.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
//used to more easily access and document the arguments supported for a node script run from command line
const _ = require('lodash');
const log = require('./log');
const path = require('path');
const currentScript = path.basename(__filename);
var args = { };
var altNames = { };
if(!process.argv) {
//Not called via command line.
args.ignore = true;
return;
}
var waitOnErrors = null;
var hasDefaults = false;
var errors = [];
var exampleCommands = {
longDashed: ['node ' + process.argv[1].split('/').pop()],
shortDashed: ['node ' + process.argv[1].split('/').pop()],
longEquals: ['node ' + process.argv[1].split('/').pop()],
shortEquals: ['node ' + process.argv[1].split('/').pop()],
all: ['node ' + process.argv[1].split('/').pop()],
};
for(var ai = 0; ai < process.argv.length; ai++) {
var arg = process.argv[ai];
var key = arg.split('=')[0];
if(key.charAt(0) == '-') {
key = key.substring(1);
}
if(key.charAt(0) == '-') {
key = key.substring(1);
}
var val = true;
if(arg.indexOf('=') > -1) {
val = arg.split('=').pop();
if(val.charAt(0) == '"') {
val = val.split('"').join('');
}
if(val.charAt(0) == "'") {
val = val.split("'").join('');
}
} else if(ai < process.argv.length - 1) {
var nextArg = process.argv[ai + 1];
if(nextArg.charAt(0) !== '-' && nextArg.indexOf('=') < 0) {
val = nextArg;
ai++;
}
}
args[key] = val;
}
args.extend = function(object) {
_.forOwn(object, function(v, k) {
args[k] = v;
});
}
args.waitOnErrors = function() {
waitOnErrors = true;
}
//argName can be an array, if so the first argument becomes the value, but the others are alternatives. E.g. ["unit", "u"]
args.declare = function(argName, argType, argDefault, argExplain) {
var that = this;
if(Array.isArray(argName)) {
var useArgName = argName[0];
argName.forEach(function(a) {
if(typeof that[a] !== 'undefined') {
that[useArgName] = that[a];
}
});
altNames[useArgName] = argName;
argName = useArgName;
}
if(typeof this[argName] == 'undefined') {
if(typeof argDefault == 'undefined' || argDefault === null) {
var err = 'arg "' + argName + ( altNames[argName] ? '" / "' + altNames[argName].slice(1).join('" / "') : '') + '" is required' + (argExplain ? '. ' + argExplain : '') + (argType ? ', type ' + argType : '');
if(waitOnErrors) {
errors.push(err);
exampleCommands.longEquals.push(argName + '=?');
exampleCommands.shortEquals.push((altNames[argName] ? altNames[argName][1] : argName) + '=?');
exampleCommands.longDashed.push('--' + argName + ' ?');
exampleCommands.shortDashed.push('-' + (altNames[argName] ? altNames[argName][1] : argName) + ' ?');
exampleCommands.all.push('--' + argName + ' ?');
} else {
throw new Error(err);
}
} else {
this[argName] = argDefault;
console.log('defaulting arg "' + argName + '" to ' + argDefault + (argExplain ? '. ' + argExplain : ''));
exampleCommands.all.push('--' + argName + ' ?');
hasDefaults = true;
}
} else {
if(typeof argType == 'string') {
if(argType == 'number') {
this[argName] = Number(this[argName]);
if(isNaN(this[argName])) {
throw new Error('arg "' + argName + '" must be of type ' + argType);
}
} else if(argType == 'boolean') {
if(this[argName] !== 'false' && this[argName] !== 'true' && this[argName] !== '0' && this[argName] !== '1') {
var err = 'arg "' + argName + '" must be of type ' + argType;
if(waitOnErrors) {
errors.push(err);
} else {
throw new Error(err);
}
}
this[argName] = (this[argName] == '1' || this[argName] == 'true');
} else if(argType == 'array') {
this[argName] = this[argName].split(',');
} else if(argType == 'object') {
this[argName] = JSON.parse(this[argName]);
} else if(argType !== 'string') {
throw new Error('Unknown arg type ' + argType);
}
}
}
}
args.throwIfErrors = function() {
if(waitOnErrors && errors.length) {
if(args.help) {
log('');
log(exampleCommands.longDashed.join(' '));
log(exampleCommands.longEquals.join(' '));
log(exampleCommands.shortDashed.join(' '));
log(exampleCommands.shortEquals.join(' '));
if(hasDefaults) { log(exampleCommands.all.join(' ')); }
log('');
}
errors.forEach(function(err) { log(err); });
if(args.help) {
process.exit(0);
} else {
throw new Error('Cannot proceed due to bad/missing args');
}
}
}
module.exports = args;