From 1956caa5a40b973524e0aeae4734905916435784 Mon Sep 17 00:00:00 2001 From: millermedeiros Date: Fri, 16 Dec 2011 13:45:52 -0200 Subject: [PATCH] add compact option to CLI and update flags to work with latest nomnom api. see #10 --- lib/cli.js | 25 +++++++++++++++++++++---- package.json | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index b791de4..eeb6a20 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -11,6 +11,7 @@ var options = require("nomnom") help: "file to parse; otherwise uses stdin" }, version: { + flag : true, string: '-v, --version', help: 'print version and exit', callback: function() { @@ -18,30 +19,46 @@ var options = require("nomnom") } }, sort : { + flag : true, string: '-s, --sort-keys', help: 'sort object keys' }, inplace : { + flag : true, string: '-i, --in-place', help: 'overwrite the file' }, indent : { string: '-t CHAR, --indent CHAR', - default: " ", + "default": " ", help: 'character(s) to use for indentation' + }, + compact : { + flag : true, + string: '-c, --compact', + help : 'compact error display' } }) .parseArgs(); +if (options.compact) { + var fileName = options.file? options.file + ': ' : ''; + parser.parseError = parser.lexer.parseError = function(str, hash) { + util.puts(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.'); + throw new Error(str); + }; +} function parse (source) { try { var parsed = options.sort ? sortObject(parser.parse(source)) : - parser.parse(source); + parser.parse(source); return JSON.stringify(parsed,null,options.indent); } catch (e) { - util.puts(e); + if (! options.compact) { + util.puts(e); + } process.exit(1); } } @@ -49,7 +66,7 @@ function parse (source) { function main (args) { var source = ''; if (options.file) { - var path = require('path').join(process.cwd(), options.file); + var path = require('path').join(process.cwd(), options.file); source = parse(fs.readFileSync(path, "utf8")); if (options.inplace) { fs.writeSync(fs.openSync(path,'w+'), source, 0, "utf8"); diff --git a/package.json b/package.json index 027f3d4..0cd0785 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lint", "jsonlint" ], - "version": "1.2.1", + "version": "1.2.1+", "preferGlobal": true, "repository": { "type": "git",