Implement the --extra-options and --extra-options-file options

These are mainly useful to pass additional options to plugins.
redux
David Majda 11 years ago
parent d013016717
commit 851681d663

@ -79,6 +79,10 @@ You can tweak the generated parser with several options:
allowed to start parsing from (default: the first rule in the grammar)
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple
times)
* `--extra-options` — additional options (in JSON format) to pass to
`PEG.buildParser`
* `--extra-options-file` — file with additional options (in JSON format) to
pass to `PEG.buildParser`
### JavaScript API

@ -34,6 +34,10 @@ function printHelp() {
util.puts(" (default: speed)");
util.puts(" --plugin <plugin> use a specified plugin (can be specified");
util.puts(" multiple times)");
util.puts(" --extra-options <options> additional options (in JSON format) to pass");
util.puts(" to PEG.buildParser");
util.puts(" --extra-options-file <file> file with additional options (in JSON");
util.puts(" format) to pass to PEG.buildParser");
util.puts(" -v, --version print version information and exit");
util.puts(" -h, --help print help and exit");
}
@ -51,6 +55,23 @@ function abort(message) {
exitFailure();
}
function addExtraOptions(options, json) {
try {
var extraOptions = JSON.parse(json);
} catch (e) {
if (!(e instanceof SyntaxError)) { throw e; }
abort("Error parsing JSON: " + e.message);
}
if (typeof extraOptions !== "object") {
abort("The JSON with extra options has to represent an object.");
}
for (var key in extraOptions) {
options[key] = extraOptions[key];
}
}
/* Arguments */
var args = process.argv.slice(2); // Trim "node" and the script path.
@ -135,6 +156,27 @@ while (args.length > 0 && isOption(args[0])) {
options.plugins.push(module);
break;
case "--extra-options":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the --extra-options option.");
}
addExtraOptions(options, args[0]);
break;
case "--extra-options-file":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the --extra-options-file option.");
}
try {
var json = fs.readFileSync(args[0]);
} catch(e) {
abort("Can't read from file \"" + args[0] + "\".");
}
addExtraOptions(options, json);
break;
case "-v":
case "--version":
printVersion();

Loading…
Cancel
Save