bin/pegjs: Allow using "-" to mean standard input and output

Part of #370.
redux
David Majda 8 years ago
parent 6eb42ddae7
commit 1c14a2c8f2

@ -93,7 +93,7 @@ function trim(s) {
var args = process.argv.slice(2); // Trim "node" and the script path. var args = process.argv.slice(2); // Trim "node" and the script path.
function isOption(arg) { function isOption(arg) {
return (/^-/).test(arg); return (/^-.+/).test(arg);
} }
function nextArg() { function nextArg() {
@ -236,38 +236,54 @@ while (args.length > 0 && isOption(args[0])) {
nextArg(); nextArg();
} }
var inputFile;
var outputFile;
var inputStream; var inputStream;
var outputStream; var outputStream;
switch (args.length) { switch (args.length) {
case 0: case 0:
process.stdin.resume(); inputFile = "-";
inputStream = process.stdin; outputFile = "-";
outputStream = process.stdout;
break; break;
case 1: case 1:
case 2: inputFile = args[0];
var inputFile = args[0]; if (inputFile === "-") {
inputStream = fs.createReadStream(inputFile); outputFile = "-";
inputStream.on("error", function() { } else {
abort("Can't read from file \"" + inputFile + "\"."); outputFile = inputFile.substr(0, inputFile.length - path.extname(inputFile).length) + ".js";
}); }
break;
var outputFile = args.length === 1
? args[0].substr(0, args[0].length - path.extname(args[0]).length) + ".js"
: args[1];
outputStream = fs.createWriteStream(outputFile);
outputStream.on("error", function() {
abort("Can't write to file \"" + outputFile + "\".");
});
case 2:
inputFile = args[0];
outputFile = args[1];
break; break;
default: default:
abort("Too many arguments."); abort("Too many arguments.");
} }
if (inputFile === "-") {
process.stdin.resume();
inputStream = process.stdin;
inputStream.on("error", function() {
abort("Can't read from file \"" + inputFile + "\".");
});
} else {
inputStream = fs.createReadStream(inputFile);
}
if (outputFile === "-") {
outputStream = process.stdout;
} else {
outputStream = fs.createWriteStream(outputFile);
outputStream.on("error", function() {
abort("Can't write to file \"" + outputFile + "\".");
});
}
readStream(inputStream, function(input) { readStream(inputStream, function(input) {
var source; var source;

Loading…
Cancel
Save