Permalink
Browse files

replace globalOpts() and fallback callback() with nocommand()

  • Loading branch information...
1 parent 08e9eb8 commit 02b62a501e95578932b74e3c1b2ae545e1d0744e @harthur committed Nov 2, 2011
Showing with 87 additions and 64 deletions.
  1. +2 −6 README.md
  2. +27 −19 nomnom.js
  3. +58 −39 test/commands.js
View
@@ -245,13 +245,9 @@ Overrides the usage printing function.
Takes a command name and gives you a command object on which you can chain command options.
-#### callback
-
-A callback that will be called with the parsed options. If a command is expected, this is the fallback callback when no command is specified.
-
-#### globalOpts
+#### nocommand
-The global options when commands are specified. Any options in here will be included in the usage string for any command.
+Gives a command object that will be used when no command is called.
#### parseArgs
View
@@ -9,10 +9,18 @@ function ArgParser() {
ArgParser.prototype = {
// add a command to the expected commands
command : function(name) {
- var command = this.commands[name] = {
- name: name,
- specs: {}
- };
+ var command;
+ if (name) {
+ command = this.commands[name] = {
+ name: name,
+ specs: {}
+ };
+ }
+ else {
+ command = this.fallback = {
+ specs: {}
+ };
+ }
// facilitates command('name').opts().cb().help()
var chain = {
@@ -36,21 +44,15 @@ ArgParser.prototype = {
return chain;
},
- globalOpts : function(specs) {
- this.globalSpecs = specs;
- return this;
+ nocommand : function() {
+ return this.command();
},
opts : function(specs) {
this.specs = specs;
return this;
},
-
- callback : function(cb) {
- this.cb = cb;
- return this;
- },
-
+
usage : function(usageString) {
this.usageStr = usageString;
return this;
@@ -89,7 +91,7 @@ ArgParser.prototype = {
if (commandExpected) {
if (command) {
- this.specs = _(command.specs).extend(this.globalSpecs);
+ _(this.specs).extend(command.specs);
this.script += " " + command.name;
if (command.help) {
this.helpStr = command.help;
@@ -105,7 +107,10 @@ ArgParser.prototype = {
position: 0,
help: 'one of: ' + _(this.commands).keys().join(", ")
}
- _(this.specs).extend(this.globalSpecs);
+ if (this.fallback) {
+ _(this.specs).extend(this.fallback.specs);
+ this.helpStr = this.fallback.help;
+ }
}
}
@@ -208,12 +213,12 @@ ArgParser.prototype = {
this.print(opt.name + " argument is required\n\n" + this.getUsage());
}
}, this);
-
+
if (command && command.cb) {
command.cb(options);
}
- else if (this.cb) {
- this.cb(options);
+ else if (this.fallback && this.fallback.cb) {
+ this.fallback.cb(options);
}
return options;
@@ -223,6 +228,9 @@ ArgParser.prototype = {
if (this.command && this.command.usageStr) {
return this.command.usageStr;
}
+ else if (this.fallback && this.fallback.usageStr) {
+ return this.fallback.usageStr;
+ }
if (this.usageStr) {
return this.usageStr;
}
@@ -235,7 +243,7 @@ ArgParser.prototype = {
})
positionals = _(positionals).sortBy(function(opt) {
return opt.position;
- });
+ });
var options = _(this.specs).select(function(opt) {
return opt.position === undefined;
});
View
@@ -37,15 +37,19 @@ exports.testMissingCommand = function(test) {
exports.testNoCommand = function(test) {
test.expect(2);
- var parser = nomnom().opts({
- version: {
- flag: true
- }
- })
- .callback(function(options) {
- test.strictEqual(options.version, true);
- });
-
+ var parser = nomnom();
+
+ parser.nocommand()
+ .opts({
+ version: {
+ flag: true
+ }
+ })
+ .callback(function(options) {
+ test.strictEqual(options.version, true);
+ })
+ .usage("fallback usage");
+
parser.command('run');
var options = parser.parseArgs(["--version"]);
@@ -54,48 +58,63 @@ exports.testNoCommand = function(test) {
test.done();
}
-exports.testUsage = function(test) {
- test.expect(3);
-
- var parser = nomnom().scriptName("test")
- .globalOpts({
- debug: {
- flag: true
- }
- })
- .opts({
- verbose: {
- flag: true
- }
- });
-
- parser.command('run')
+function createParser() {
+ var parser = nomnom().scriptName("test")
.opts({
- file: {
- help: 'file to run'
+ debug: {
+ flag: true
}
- })
- .help("run all");
-
- parser.command('test').usage("test usage");
+ });
+
+ parser.command('run')
+ .opts({
+ file: {
+ help: 'file to run'
+ }
+ })
+ .help("run all");
+
+ parser.command('test').usage("test usage");
+
+ parser.nocommand()
+ .opts({
+ verbose: {
+ flag: true
+ }
+ })
+ .help("nocommand");
+
+ return parser;
+}
+exports.testUsage = function(test) {
+ test.expect(4);
+
+ var parser = createParser();
parser.printFunc(function(string) {
- test.equal(strip(string), "usage:test<command>[options]commandoneof:run,testoptions:--verbose--debug");
+ test.equal(strip(string), "testusage");
});
+ parser.parseArgs(["test", "-h"]);
- parser.parseArgs(["-h"]);
-
+ parser = createParser();
parser.printFunc(function(string) {
- test.equal(strip(string), "usage:testrun[options]options:--filefiletorun--debugrunall");
+ test.equal(strip(string), "usage:testrun[options]options:--debug--filefiletorunrunall");
});
-
parser.parseArgs(["run", "-h"]);
+ parser = createParser();
parser.printFunc(function(string) {
- test.equal(strip(string), "testusage");
+ test.equal(strip(string), "usage:test<command>[options]commandoneof:run,testoptions:--debug--verbosenocommand");
});
-
- parser.parseArgs(["test", "-h"]);
+ parser.parseArgs(["-h"]);
+ parser = createParser();
+ parser.nocommand()
+ .usage("fallback");
+ parser.printFunc(function(string) {
+ test.equal(strip(string), "fallback");
+ });
+ parser.parseArgs(["-h"]);
+
test.done();
}

0 comments on commit 02b62a5

Please sign in to comment.