add compact option to CLI and update flags to work with latest nomnom api. see #10
This commit is contained in:
25
lib/cli.js
25
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");
|
||||
|
||||
Reference in New Issue
Block a user