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:
```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.

@ -13,9 +13,10 @@ function printVersion() {
}
function printHelp() {
console.log("Usage: pegjs [options] [--] [<input_file>] [<output_file>]");
console.log("Usage: pegjs [options] [--] [<input_file>]");
console.log("");
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(" parser object is assigned to when no module");
console.log(" loader is detected (default: \"\")");
@ -103,6 +104,9 @@ function readStream(inputStream, callback) {
/* Main */
var inputFile = null;
var outputFile = null;
var options = {
cache: false,
output: "source",
@ -116,6 +120,15 @@ var options = {
while (args.length > 0 && isOption(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 "--export-var":
nextArg();
@ -229,33 +242,28 @@ while (args.length > 0 && isOption(args[0])) {
nextArg();
}
var inputFile;
var outputFile;
var inputStream;
var outputStream;
switch (args.length) {
case 0:
inputFile = "-";
outputFile = "-";
break;
case 1:
inputFile = args[0];
break;
default:
abort("Too many arguments.");
}
if (outputFile === null) {
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;
default:
abort("Too many arguments.");
}
if (inputFile === "-") {

Loading…
Cancel
Save