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,36 +236,52 @@ 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:
inputFile = args[0];
if (inputFile === "-") {
outputFile = "-";
} else {
outputFile = inputFile.substr(0, inputFile.length - path.extname(inputFile).length) + ".js";
}
break;
case 2: case 2:
var inputFile = args[0]; inputFile = args[0];
inputStream = fs.createReadStream(inputFile); outputFile = args[1];
break;
default:
abort("Too many arguments.");
}
if (inputFile === "-") {
process.stdin.resume();
inputStream = process.stdin;
inputStream.on("error", function() { inputStream.on("error", function() {
abort("Can't read from file \"" + inputFile + "\"."); abort("Can't read from file \"" + inputFile + "\".");
}); });
} else {
inputStream = fs.createReadStream(inputFile);
}
var outputFile = args.length === 1 if (outputFile === "-") {
? args[0].substr(0, args[0].length - path.extname(args[0]).length) + ".js" outputStream = process.stdout;
: args[1]; } else {
outputStream = fs.createWriteStream(outputFile); outputStream = fs.createWriteStream(outputFile);
outputStream.on("error", function() { outputStream.on("error", function() {
abort("Can't write to file \"" + outputFile + "\"."); abort("Can't write to file \"" + outputFile + "\".");
}); });
break;
default:
abort("Too many arguments.");
} }
readStream(inputStream, function(input) { readStream(inputStream, function(input) {

Loading…
Cancel
Save