Permalink
Browse files

replace args parser with cli

  • Loading branch information...
1 parent eb95da3 commit c65dc2f74d96eac751f16cabe3d6a66a54fd72ec @halkeye halkeye committed May 5, 2012
Showing with 108 additions and 74 deletions.
  1. +11 −9 HELP
  2. +29 −43 lib/cli.js
  3. +1 −1 package.json
  4. +67 −21 test/unit/cli.js
View
20 HELP
@@ -1,10 +1,12 @@
-Usage: jshint path path2 [options]
+Usage:
+ hint [OPTIONS] [ARGS]
-Options:
-
- --version display package version
- --config custom config file
- --reporter custom reporter
- --jslint-reporter use a jslint compatible xml reporter
- --show-non-errors show additional data generated by jshint
- --extra-ext comma-separated list of file extensions to use (.js is default)
+Options:
+ -v, --version BOOLEAN display package version
+ --config STRING custom config file
+ --reporter STRING custom reporter
+ --jslint-reporter use a jslint compatible xml reporter
+ --show-non-errors show additional data generated by jshint
+ --extra-ext STRING comma-separated list of file extensions to use (.js
+ is default)
+ -h, --help Display help and usage details
View
72 lib/cli.js
@@ -1,23 +1,14 @@
var fs = require('fs'),
path = require('path'),
- argsparser = require('argsparser'),
+ cli = require('cli').enable('glob', 'help'),
hint = require('./hint');
var rootPath = path.resolve("/");
-
function existsSync() {
var obj = fs.existsSync ? fs : path;
return obj.existsSync.apply(obj, arguments);
}
-function _help() {
- process.stdout.write(fs.readFileSync(__dirname + "/../HELP", "utf-8"));
-}
-
-function _version() {
- process.stdout.write(JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf-8")).version + "\n");
-}
-
function _removeJsComments(str) {
str = str || '';
str = str.replace(/\/\*[\s\S]*(?:\*\/)/g, ''); //everything between "/* */"
@@ -88,47 +79,42 @@ function _print(results) {
module.exports = {
interpret: function (args) {
- var config, reporter,
- options = argsparser.parse(args),
- customConfig = options["--config"],
- customReporter = options["--reporter"] ? path.resolve(process.cwd(), options["--reporter"]) : null,
- targets = options.node,
- extraExtensionList = options["--extra-ext"],
- ignoreFile, ignores;
-
- //could be on Windows which we are looking for an attribute ending in 'node.exe'
- if (targets === undefined) {
- (function () {
- var arg;
-
- for (arg in options) {
- if (path.basename(arg) === 'node.exe') {
- targets = options[arg];
- break;
- }
- }
- }());
- }
-
- targets = typeof targets === "string" ? null : targets.slice(1);
-
- extraExtensionList = typeof extraExtensionList === "string" ? extraExtensionList : "";
-
- if (options["--version"]) {
- _version();
+ var config, reporter, options,
+ customConfig, customReporter,
+ ignoreFile, ignores, extraExtensionList;
+
+ cli.setArgv(args);
+ cli.options = {};
+ options = cli.parse({
+ 'version': ['v', 'display package version', 'boolean', false],
+ 'config': ['config', 'custom config file', 'string', false],
+ 'reporter': ['reporter', 'custom reporter', 'string', undefined],
+ 'jslint-reporter': ['jslint-reporter', 'use a jslint compatible xml reporter'],
+ 'show-non-errors': ['show-non-errors', 'show additional data generated by jshint'],
+ 'extra-ext': ['extra-ext', 'comma-separated list of file extensions to use (.js is default)', 'string', '']
+ });
+
+ customConfig = options.config;
+ customReporter = options.reporter ? path.resolve(process.cwd(), options.reporter) : null;
+ extraExtensionList = options["extra-ext"];
+
+ if (options.version) {
+ cli.setApp(path.resolve(__dirname + "/../package.json"));
+ process.stdout.write(cli.version + "\n");
return;
}
- if (!targets || options["--help"]) {
- _help();
+ if (options.help || !cli.args.length) {
+ cli.getUsage();
+ process.exit();
return;
}
- if (options["--jslint-reporter"]) {
+ if (options['jslint-reporter']) {
customReporter = "./reporters/jslint_xml.js";
}
- if (options["--show-non-errors"]) {
+ if (options['show-non-errors']) {
customReporter = "./reporters/non_error.js";
}
@@ -156,6 +142,6 @@ module.exports = {
});
}
- _print(hint.hint(targets, config, reporter, ignores, extraExtensionList));
+ _print(hint.hint(cli.args, config, reporter, ignores, extraExtensionList));
}
};
View
2 package.json
@@ -24,7 +24,7 @@
"lib"
],
"dependencies": {
- "argsparser": "0.0.x",
+ "cli": "0.4.3",
"minimatch": "0.0.x"
},
"devDependencies": {
View
88 test/unit/cli.js
@@ -8,22 +8,44 @@ describe("cli", function () {
if (!process.stdout.flush) {
process.stdout.flush = function () {};
}
-
- spyOn(process, "exit");
+ spyOn(process, "exit").andCallFake(function () {
+ throw "ProcessExit";
+ });
spyOn(hint, "hint").andReturn([]);
spyOn(process.stdout, "write");
});
it("interprets --help with no args", function () {
- var txt = require('fs').readFileSync(__dirname + "/../../HELP", "utf-8");
- cli.interpret(["node", "hint"]);
- expect(process.stdout.write.mostRecentCall.args[0]).toEqual(txt);
+ var txt = require('fs').readFileSync(__dirname + "/../../HELP", "utf-8"),
+ got = [],
+ i = 0;
+
+ spyOn(console, "error");
+ try {
+ cli.interpret(["node", "hint"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
+
+ for (i = 0; i < console.error.calls.length; i++)
+ got.push(console.error.calls[i].args[0]);
+
+ //require('fs').writeFileSync(__dirname + "/../../HELP", got.join("\n"), "utf-8");
+ expect(got.join("\n")).toEqual(txt);
});
it("interprets --help", function () {
- var txt = require('fs').readFileSync(__dirname + "/../../HELP", "utf-8");
- cli.interpret(["node", "hint", "file.js", "--help"]);
- expect(process.stdout.write.mostRecentCall.args[0]).toEqual(txt);
+ var txt = require('fs').readFileSync(__dirname + "/../../HELP", "utf-8"),
+ got = [],
+ i = 0;
+
+ spyOn(console, "error");
+ try {
+ cli.interpret(["node", "hint", "file.js", "--help"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
+
+ for (i = 0; i < console.error.calls.length; i++)
+ got.push(console.error.calls[i].args[0]);
+
+ expect(got.join("\n")).toEqual(txt);
});
it("interprets --config", function () {
@@ -35,7 +57,9 @@ describe("cli", function () {
});
spyOn(JSON, "parse").andReturn(config);
- cli.interpret(["node", "hint", "file2.js", "file.js", "--config", "file.json"]);
+ try {
+ cli.interpret(["node", "hint", "file2.js", "file.js", "--config", "file.json"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(fs.readFileSync).toHaveBeenCalledWith("file.json", "utf-8");
expect(JSON.parse).toHaveBeenCalledWith("data");
@@ -47,7 +71,9 @@ describe("cli", function () {
it("interprets --reporter", function () {
var reporter = require("./../../example/reporter").reporter;
spyOn(process, "cwd").andReturn(__dirname + "/../");
- cli.interpret(["node", "hint", "file.js", "file.js", "--reporter", "../example/reporter.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js", "file.js", "--reporter", "../example/reporter.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});
@@ -63,7 +89,9 @@ describe("cli", function () {
return path.match(/\.jshintrc/) ? true : false;
});
- cli.interpret(["node", "hint", "file.js", "file2.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js", "file2.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(fs.readFileSync.argsForCall[0]).toEqual([path.join(process.cwd(), '.jshintrc'), "utf-8"]);
});
@@ -85,27 +113,35 @@ describe("cli", function () {
}
});
- cli.interpret(["node", "hint", "file.js", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(fs.readFileSync.argsForCall[0]).toEqual([home, "utf-8"]);
});
});
it("interprets --version and logs the current package version", function () {
- var data = {version: 1};
+ var data = {"name": "jshint", "version": "0.6.4"};
spyOn(fs, "readFileSync").andReturn(JSON.stringify(data));
- cli.interpret(["node", "file.js", "--version"]);
+ try {
+ cli.interpret(["node", "file.js", "--version"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(process.stdout.write.mostRecentCall.args[0]).toEqual(data.version + "\n");
});
it("interprets --jslint-reporter and uses the jslint xml reporter", function () {
var reporter = require("./../../lib/reporters/jslint_xml").reporter;
- cli.interpret(["node", "file.js", "file.js", "--jslint-reporter"]);
+ try {
+ cli.interpret(["node", "file.js", "file.js", "--jslint-reporter"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});
it("interprets --show-non-errors and uses the non error reporter", function () {
var reporter = require("./../../lib/reporters/non_error.js").reporter;
- cli.interpret(["node", "file.js", "file.js", "--show-non-errors"]);
+ try {
+ cli.interpret(["node", "file.js", "file.js", "--show-non-errors"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});
@@ -135,7 +171,9 @@ describe("cli", function () {
}
});
- cli.interpret(["node", "hint", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(hint.hint.mostRecentCall.args[3])
.toEqual([path.join(process.cwd(), "dir"),
@@ -157,7 +195,9 @@ describe("cli", function () {
}
});
- cli.interpret(["node", "hint", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(hint.hint.mostRecentCall.args[3])
.toEqual([path.join(process.env.HOME, "dir"),
@@ -172,7 +212,9 @@ describe("cli", function () {
hint.hint.andReturn(results);
spyOn(process.stdout, "flush").andReturn(true);
- cli.interpret(["node", "hint", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(process.exit).toHaveBeenCalledWith(0);
});
@@ -184,7 +226,9 @@ describe("cli", function () {
hint.hint.andReturn(results);
spyOn(process.stdout, "flush").andReturn(true);
- cli.interpret(["node", "hint", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(process.exit).toHaveBeenCalledWith(1);
});
@@ -199,7 +243,9 @@ describe("cli", function () {
func();
});
- cli.interpret(["node", "hint", "file.js"]);
+ try {
+ cli.interpret(["node", "hint", "file.js"]);
+ } catch (err) { if (err !== "ProcessExit") throw err; }
expect(process.stdout.on.argsForCall[0][0]).toBe("drain");
expect(process.exit).toHaveBeenCalledWith(1);

0 comments on commit c65dc2f

Please sign in to comment.