From 26d697c45fa27a1d57e384c5bb2e0d53260f6893 Mon Sep 17 00:00:00 2001 From: Zach Carter Date: Wed, 1 Jun 2011 17:24:24 -0400 Subject: [PATCH] Add nomnom for option parsing. Allow indentation option" --- lib/cli.js | 27 +++++++++++++++++++++++++-- package.json | 6 ++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 74f2f5f..7f0f5ef 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,7 +1,30 @@ #!/usr/bin/env node var sys = require("sys"); +var fs = require("fs"); var parser = require("./jsonlint").parser; +var options = require("nomnom") + .scriptName("jsonlint") + .opts({ + file: { + position: 0, + help: "file to parse; otherwise uses stdin" + }, + version: { + string: '-v, --version', + help: 'print version and exit', + callback: function() { + return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version; + } + }, + indent : { + string: '-t CHAR, --indent CHAR', + default: " ", + help: 'character(s) to use for indentation' + } + }) + .parseArgs(); + function parse (source) { try { @@ -14,8 +37,8 @@ function parse (source) { function main (args) { var source = ''; - if (args[1]) { - source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8"); + if (options.file) { + source = fs.readFileSync(require('path').join(process.cwd(), options.file), "utf8"); parse(source); } else { var stdin = process.openStdin(); diff --git a/package.json b/package.json index c8cbdbe..c1739d8 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,9 @@ "engines": { "node": "0.4 || 0.5" }, - "dependencies": {}, + "dependencies": { + "nomnom": ">= 0.4.3" + }, "devDependencies": { "test": "*", "jison": "*", @@ -34,4 +36,4 @@ "test": "node test/all-tests.js" }, "homepage": "http://zaach.github.com/jsonlint/" -} \ No newline at end of file +}