forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptparse.js
More file actions
302 lines (281 loc) · 10.1 KB
/
Copy pathoptparse.js
File metadata and controls
302 lines (281 loc) · 10.1 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************
Authors : Joey Mazzarelli, Mike de Boer
Email : mazzarelli@gmail.com
Source : http://bitbucket.org/mazzarell/js-opts/
License : Simplified BSD License
Copyright 2010 Joey Mazzarelli. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY JOEY MAZZARELLI 'AS IS' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL JOEY MAZZARELLI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Joey Mazzarelli.
***************************************************************************/
var puts = console.log,
values = {},
args = {},
argv = [],
errors = [],
descriptors = {opts:[], args:[]};
/**
* Get some version info out of it
*/
exports.version = "1.2.1";
/**
* Add a set of option descriptors, not yet ready to be parsed.
* See exports.parse for description of options object
*
* Additionally, it takes a namespace as an argument, useful for
* building options for a library in addition to the main app.
*/
exports.add = function(options, namespace) {
for (var i = 0, l = options.length; i < l; ++i) {
options[i].namespace = namespace;
descriptors.opts.push(options[i]);
}
};
/**
* Parse the command line options
* @param array options Options to parse
* @param array args Arguments to parse
* @param bool help Automatically generate help message, default false
*
* ===== Options Docs =====
* Each option in the array can have the following fields. None are required,
* but you should at least provide a short or long name.
* {
* short : 'l',
* long : 'list',
* description : 'Show a list',
* value : false, // default false
* required : true, // default false
* callback : function (value) { ... },
* }
*
* You can add an automatically generated help message by passing
* a second parameter of <true> or by including the option;
* {
* long : 'help',
* description : 'Show this help message',
* callback : require('./opts').help,
* }
*
* ===== Arguments Docs =====
* Arguments are different than options, and simpler. They typically come
* after the options, but the library really doesn't care. Each argument
* can have the form of:
* {
* name : 'script',
* required : true, // default false
* callback : function (value) { ... },
* }
*/
exports.parse = function(options, params, help) {
if (params === true) {
help = true;
}
else if (!params) {
params = [];
}
else {
for (var i = 0, l = params.length; i < l; ++i)
descriptors.args.push(params[i]);
}
if (help) {
options.push({
long : "help",
description : "Show this help message",
callback : exports.help
});
}
for (var i = 0, l = options.length; i < l; ++i)
descriptors.opts.unshift(options[i]);
options = descriptors.opts;
var checkDup = function (opt, type) {
var prefix = (type == "short")? "-": "--";
var name = opt[type];
if (!opts[prefix + name]) {
opts[prefix + name] = opt;
}
else {
if (opt.namespace && !opts[prefix + opt.namespace + "." + name]) {
opts[prefix + opt.namespace + "." + name] = opt;
for (var i = 0, l = descriptors.opts.length; i < l; ++i) {
var desc = descriptors.opts[i];
if (desc.namespace == opt.namespace) {
if (type == "long" && desc.long == opt.long)
descriptors.opts[i].long = opt.namespace + "." + opt.long;
else if (type == "short")
delete descriptors.opts[i].short;
}
}
}
else {
puts("Conflicting flags: " + prefix + name + "\n");
puts(helpString());
process.exit(1);
}
}
};
var opts = {};
for (var i = 0, l = options.length; i < l; ++i) {
if (options[i].short)
checkDup(options[i], "short");
if (options[i].long)
checkDup(options[i], "long");
}
for (var i = 2, l = process.argv.length; i < l; ++i) {
var inp = process.argv[i];
if (opts[inp]) {
// found a match, process it.
var opt = opts[inp];
if (!opt.value) {
if (opt.callback)
opt.callback(true);
if (opt.short)
values[opt.short] = true;
if (opt.long)
values[opt.long] = true;
}
else {
var next = process.argv[i + 1];
if (!next || opts[next]) {
var flag = opt.short || opt.long;
errors.push("Missing value for option: " + flag);
if (opt.short)
values[opt.short] = true;
if (opt.long)
values[opt.long] = true;
}
else {
if (opt.callback)
opt.callback(next);
if (opt.short)
values[opt.short] = opt.parser ? opt.parser(next) : next;
if (opt.long)
values[opt.long] = opt.parser ? opt.parser(next) : next;
i++;
}
}
}
else {
// No match. If it starts with a dash, show an error. Otherwise
// add it to the extra params.
if (inp[0] == "-") {
puts("Unknown option: " + inp);
if (opts["--help"])
puts("Try --help");
process.exit(1);
}
else {
argv.push(inp);
var arg = params.shift ? params.shift() : params;
if (arg) {
args[arg.name] = arg.parser ? arg.parser(inp) : inp;
if (arg.callback)
arg.callback(inp);
}
}
}
}
for (var opt, i = 0, l = options.length; i < l; ++i) {
opt = options[i];
var flag = opt.short || opt.long;
if (typeof exports.get(flag) == "undefined" && opt.def) {
if (opt.short)
values[opt.short] = opt.parser ? opt.parser(opt.def) : opt.def;
if (opt.long)
values[opt.long] = opt.parser ? opt.parser(opt.def) : opt.def;
}
if (options[i].required && typeof exports.get(flag) == "undefined")
errors.push("Missing required option: " + flag);
}
for (var i = 0, l = params.length; i < l; ++i) {
if (params[i].required && !args[params[i].name])
errors.push("Missing required argument: " + params[i].name);
}
if (errors.length) {
for (var i = 0, l = errors.length; i < l; ++i)
puts(errors[i]);
puts("\n" + helpString());
process.exit(1);
}
return values;
};
/**
* Get the value of an option. Can be the short or long option
* @return string
*/
exports.get = function(opt) {
return values[opt] || values["-" + opt] || values["--" + opt];
};
/**
* Get unknown args. Could have special meaning to client
*/
exports.args = function() {
return argv;
};
/**
* Get an arg by name.
* This only works if arg names were passed into the parse function.
* @param string name Name of arg
* @return string Value of arg
*/
exports.arg = function(name) {
//puts(require("sys").inspect(arguments));
return args[name];
};
/**
* Print the help message and exit
*/
exports.help = function() {
puts(helpString());
process.exit(0);
};
// Create the help string
var helpString = function() {
var str = "Usage: " + process.argv[0] + " " + process.argv[1];
if (descriptors.opts.length)
str += " [options]";
if (descriptors.args.length) {
for (var i = 0, l = descriptors.args.length; i < l; i++) {
if (descriptors.args[i].required)
str += " " + descriptors.args[i].name;
else
str += " [" + descriptors.args[i].name + "]";
}
}
str += "\n";
for (var i = 0, l = descriptors.opts.length; i < l; ++i) {
var opt = descriptors.opts[i];
if (opt.description)
str += (opt.description) + (typeof opt.def != "undefined" ? " Default: <" + opt.def + ">" : "") + "\n";
var line = "";
if (opt.short && !opt.long)
line += "-" + opt.short;
else if (opt.long && !opt.short)
line += "--" + opt.long;
else
line += "-" + opt.short + ", --" + opt.long;
if (opt.value)
line += " <value>";
if (opt.required)
line += " (required)";
str += " " + line + "\n";
}
return str;
};