bin/pegjs: Use the -o/--output option to specify the output file

This is more traditional compiler interface. Its main advantage against
specifying the output file as a second argument (which is what bin/pegjs
used until now) is that input and output files can't be mixed up.

Part of #370.
redux
David Majda 8 years ago
parent 9bf7c0c5ff
commit a57431955e

@ -78,7 +78,7 @@ This writes parser source code into a file with the same name as the grammar
file but with “.js” extension. You can also specify the output file explicitly: file but with “.js” extension. You can also specify the output file explicitly:
```console ```console
$ pegjs arithmetics.pegjs arithmetics-parser.js $ pegjs -o arithmetics-parser.js arithmetics.pegjs
``` ```
If you omit both input and output file, standard input and output are used. If you omit both input and output file, standard input and output are used.

@ -13,9 +13,10 @@ function printVersion() {
} }
function printHelp() { function printHelp() {
console.log("Usage: pegjs [options] [--] [<input_file>] [<output_file>]"); console.log("Usage: pegjs [options] [--] [<input_file>]");
console.log(""); console.log("");
console.log("Options:"); console.log("Options:");
console.log(" -o, --output <file> output file");
console.log(" -e, --export-var <variable> name of a global variable into which the"); console.log(" -e, --export-var <variable> name of a global variable into which the");
console.log(" parser object is assigned to when no module"); console.log(" parser object is assigned to when no module");
console.log(" loader is detected (default: \"\")"); console.log(" loader is detected (default: \"\")");
@ -103,6 +104,9 @@ function readStream(inputStream, callback) {
/* Main */ /* Main */
var inputFile = null;
var outputFile = null;
var options = { var options = {
cache: false, cache: false,
output: "source", output: "source",
@ -116,6 +120,15 @@ var options = {
while (args.length > 0 && isOption(args[0])) { while (args.length > 0 && isOption(args[0])) {
switch (args[0]) { switch (args[0]) {
case "-o":
case "--output":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the -o/--output option.");
}
outputFile = args[0];
break;
case "-e": case "-e":
case "--export-var": case "--export-var":
nextArg(); nextArg();
@ -229,35 +242,30 @@ 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:
inputFile = "-"; inputFile = "-";
outputFile = "-";
break; break;
case 1: case 1:
inputFile = args[0]; inputFile = args[0];
if (inputFile === "-") {
outputFile = "-";
} else {
outputFile = inputFile.substr(0, inputFile.length - path.extname(inputFile).length) + ".js";
}
break;
case 2:
inputFile = args[0];
outputFile = args[1];
break; break;
default: default:
abort("Too many arguments."); abort("Too many arguments.");
} }
if (outputFile === null) {
if (inputFile === "-") {
outputFile = "-";
} else {
outputFile = inputFile.substr(0, inputFile.length - path.extname(inputFile).length) + ".js";
}
}
if (inputFile === "-") { if (inputFile === "-") {
process.stdin.resume(); process.stdin.resume();
inputStream = process.stdin; inputStream = process.stdin;

Loading…
Cancel
Save