Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged narwhal-lib

  • Loading branch information...
commit 1296ea915a9630bc6135b00c61ff3811e5d009c7 2 parents 1129b4d + e920d6d
@kriskowal authored
Showing with 51 additions and 51 deletions.
  1. +48 −48 lib/narwhal/args.js
  2. +3 −3 lib/narwhal/promise-util.js
View
96 lib/narwhal/args.js
@@ -2,11 +2,11 @@
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
// -- tlrobinson Tom Robinson
-var os = require('os');
-var system = require('system');
-var file = require('file');
-var util = require('narwhal/util');
-var stream = require('narwhal/term').stream;
+var SYSTEM = require('system');
+var OS = require('./os');
+var FS = require('./fs');
+var UTIL = require('./util');
+var stream = require('./term').stream;
exports.UsageError = function (message) {
this.name = "UsageError";
@@ -52,8 +52,8 @@ exports.Parser.prototype.def = function (name, value) {
exports.Parser.prototype.reset = function (options) {
for (var name in this._def) {
- if (util.has(this._def, name))
- options[name] = util.copy(this._def[name]);
+ if (UTIL.has(this._def, name))
+ options[name] = UTIL.copy(this._def[name]);
}
this._options.forEach(function (option) {
options[option.getName()] = option._def;
@@ -132,7 +132,7 @@ exports.Parser.prototype.helpful = function () {
return self.printHelp(options);
})
.halt();
- if (util.len(this._commands))
+ if (UTIL.len(this._commands))
this.command('help', function (options) {
self.printHelp(options);
}).help('displays usage information');
@@ -154,12 +154,12 @@ exports.Parser.prototype.printHelp = function (options) {
if (args.length) {
// parse args for deep help
// TODO offer extended help for options
- if (!util.has(this._commands, args[0])) {
- this.error(options, util.repr(args[0]) + ' is not a command.');
+ if (!UTIL.has(this._commands, args[0])) {
+ this.error(options, UTIL.repr(args[0]) + ' is not a command.');
this.printCommands(options);
this.exit(options);
} else {
- util.put(args, 1, '--help');
+ UTIL.put(args, 1, '--help');
this._commands[args[0]]().act(args, options);
this.exit(options);
}
@@ -175,13 +175,13 @@ exports.Parser.prototype.printHelp = function (options) {
exports.Parser.prototype.printUsage = function (options) {
this.print(
- 'Usage: \0bold(\0blue(' + file.basename(options.command || '<unknown>') +
+ 'Usage: \0bold(\0blue(' + FS.base(options.command || '<unknown>') +
(!this._interleaved ? ' [OPTIONS]' : '' ) +
- (util.len(this._commands) ?
+ (UTIL.len(this._commands) ?
' COMMAND' :
''
) +
- (util.len(this._args) ?
+ (UTIL.len(this._args) ?
' ' + this._args.map(function (arg) {
if (arg._optional) {
return '[' + arg._name.toUpperCase() + ']';
@@ -205,8 +205,8 @@ exports.Parser.prototype.printUsage = function (options) {
exports.Parser.prototype.printCommands = function (options) {
var self = this;
- util.forEachApply(
- util.items(this._commands),
+ UTIL.forEachApply(
+ UTIL.items(this._commands),
function (name, command) {
var parser = command();
self.print(' \0bold(\0green(' + name + '\0)\0)' + (
@@ -227,7 +227,7 @@ exports.Parser.prototype.printCommands = function (options) {
exports.Parser.prototype.printOption = function (options, option, depth, parent) {
var self = this;
depth = depth || 0;
- var indent = util.mul(' ', depth);
+ var indent = UTIL.mul(' ', depth);
if (option._hidden)
return;
@@ -255,9 +255,9 @@ exports.Parser.prototype.printOption = function (options, option, depth, parent)
if (option._action && option._action.length > 2)
message.push(
' ' +
- util.range(option._action.length - 2)
+ UTIL.range(option._action.length - 2)
.map(function () {
- return '\0bold(\0green(' + util.upper(
+ return '\0bold(\0green(' + UTIL.upper(
option.getDisplayName()
) + '\0)\0)';
}).join(' ')
@@ -266,8 +266,8 @@ exports.Parser.prototype.printOption = function (options, option, depth, parent)
message.push(': ' + option._help + '');
if (option._choices) {
var choices = option._choices;
- if (!util.isArrayLike(choices))
- choices = util.keys(choices);
+ if (!UTIL.isArrayLike(choices))
+ choices = UTIL.keys(choices);
message.push(' \0bold(\0blue((' + choices.join(', ') + ')\0)\0)');
}
if (option._halt)
@@ -303,7 +303,7 @@ exports.Parser.prototype.exit = function (status) {
);
} else {
// FIXME: exit is sometimes called with the "options" object as the "status" argument. Why?
- os.exit(typeof status == "number" ? status : 1);
+ OS.exit(typeof status == "number" ? status : 1);
throw new Error("exit failed");
}
};
@@ -328,7 +328,7 @@ exports.Parser.prototype.check = function () {
if (!option._action)
throw new exports.ConfigurationError(
"No action associated with the option " +
- util.repr(option.getDisplayName())
+ UTIL.repr(option.getDisplayName())
);
});
};
@@ -341,7 +341,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
this.check();
if (!args)
- args = system.args;
+ args = SYSTEM.args;
if (!options)
options = {};
if (allowInterleaved === undefined)
@@ -355,7 +355,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
if (n > args.length) {
this.error(
options,
- 'Error: The ' + util.enquote(name) +
+ 'Error: The ' + UTIL.enquote(name) +
' option requires ' + n + ' arguments.'
);
}
@@ -378,13 +378,13 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
// initial values
for (var name in this._def) {
- if (util.has(this._def, name) && !util.has(options, name))
- options[name] = util.copy(this._def[name]);
+ if (UTIL.has(this._def, name) && !UTIL.has(options, name))
+ options[name] = UTIL.copy(this._def[name]);
}
this._options.forEach(function (option) {
if (!(option instanceof self.Option))
return;
- if (!util.has(options, option.getName()))
+ if (!UTIL.has(options, option.getName()))
options[option.getName()] = option._def;
});
@@ -406,7 +406,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
args.unshift(value);
}
- if (util.has(this._long, word)) {
+ if (UTIL.has(this._long, word)) {
var option = this._long[word];
if (!option._action) {
@@ -435,7 +435,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
break ARGS;
} else {
- this.error(options, 'Error: Unrecognized option: ' + util.enquote(word));
+ this.error(options, 'Error: Unrecognized option: ' + UTIL.enquote(word));
}
} else if (/^-/.test(arg)) {
@@ -443,7 +443,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
var letters = arg.match(/^-(.*)/)[1].split('');
while (letters.length) {
var letter = letters.shift();
- if (util.has(this._short, letter)) {
+ if (UTIL.has(this._short, letter)) {
var option = this._short[letter];
if (option._action.length > 2) {
@@ -474,7 +474,7 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
break ARGS;
} else {
- this.error(options, 'Error: unrecognized option: ' + util.enquote(letter));
+ this.error(options, 'Error: unrecognized option: ' + UTIL.enquote(letter));
}
}
@@ -489,9 +489,9 @@ exports.Parser.prototype.parse = function (args, options, noCommand, allowInterl
// add the interleaved arguments back in
args.unshift.apply(args, interleavedArgs)
- if (util.len(this._commands)) {
+ if (UTIL.len(this._commands)) {
if (args.length) {
- if (util.has(this._commands, args[0])) {
+ if (UTIL.has(this._commands, args[0])) {
var command = this._commands[args[0]];
command().act(args, options);
} else {
@@ -529,7 +529,7 @@ exports.Option = function (parser, args) {
};
this._long = [];
this._short = [];
- util.forEach(args, function (arg) {
+ UTIL.forEach(args, function (arg) {
if (typeof arg == "function") {
self.action(arg);
} else if (typeof arg !== "string") {
@@ -652,26 +652,26 @@ exports.Option.prototype.choices = function (choices) {
this.set();
this._choices = choices;
var self = this;
- if (util.isArrayLike(choices)) {
+ if (UTIL.isArrayLike(choices)) {
return this.validate(function (value) {
if (choices.indexOf(value) < 0)
throw new exports.UsageError(
- "choice for " + util.upper(self.getDisplayName()) +
- " is invalid: " + util.repr(value) + "\n" +
+ "choice for " + UTIL.upper(self.getDisplayName()) +
+ " is invalid: " + UTIL.repr(value) + "\n" +
"Use one of: " + choices.map(function (choice) {
- return util.enquote(choice);
+ return UTIL.enquote(choice);
}).join(', ')
);
return value;
})
} else {
return this.validate(function (value) {
- if (!util.has(choices, value))
+ if (!UTIL.has(choices, value))
throw new exports.UsageError(
- "choice for " + util.upper(self.getDisplayName()) +
- " is invalid: " + util.enquote(value) + "\n" +
- "Use one of: " + util.keys(choices).map(function (choice) {
- return util.enquote(choice);
+ "choice for " + UTIL.upper(self.getDisplayName()) +
+ " is invalid: " + UTIL.enquote(value) + "\n" +
+ "Use one of: " + UTIL.keys(choices).map(function (choice) {
+ return UTIL.enquote(choice);
}).join(', ')
);
return choices[value];
@@ -704,18 +704,18 @@ exports.Option.prototype.validate = function (validate) {
exports.Option.prototype.input = function () {
this.set().validate(function (value) {
if (value == "-")
- return system.stdin;
+ return SYSTEM.stdin;
else
- return file.open(value, 'r');
+ return FS.open(value, 'r');
});
};
exports.Option.prototype.output = function () {
this.set().validate(function (value) {
if (value == "-")
- return system.stdout;
+ return SYSTEM.stdout;
else
- return file.open(value, 'w');
+ return FS.open(value, 'w');
});
};
View
6 lib/narwhal/promise-util.js
@@ -5,9 +5,9 @@
*/
var QUTIL = exports;
-var UTIL = require("narwhal/util");
-var Q = require("narwhal/promise");
-var EL = require("narwhal/event-loop");
+var UTIL = require("./util");
+var Q = require("./promise");
+var EL = require("./event-loop");
UTIL.update(QUTIL, Q);
Please sign in to comment.
Something went wrong with that request. Please try again.