Skip to content
Browse files

add support for appending args and matching multiple positionals

  • Loading branch information...
1 parent 0ab76da commit 4950b107126055c5bf52b578d99afe771e779252 @harthur committed May 31, 2011
Showing with 56 additions and 15 deletions.
  1. +11 −3 README.md
  2. +18 −8 nomnom.js
  3. +1 −1 package.json
  4. +1 −1 test/argv.js
  5. +1 −1 test/chain.js
  6. +23 −0 test/list.js
  7. +1 −1 test/positional.js
View
14 README.md
@@ -41,7 +41,7 @@ for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
# Commands
Nomnom supports command-based interfaces (e.g. with git: `git add -p` and `git rebase -i` where `add` and `rebase` are the commands):
- var parser = require("nomnom")();
+ var parser = require("nomnom");
parser.command('browser')
.callback(runBrowser)
@@ -61,7 +61,7 @@ Nomnom supports command-based interfaces (e.g. with git: `git add -p` and `git r
})
.callback(function(options) {
runSanity(options.filename);
- })
+ })
.help("run the sanity tests")
parser.parseArgs();
@@ -109,7 +109,7 @@ Nomnom prints out a usage message if `--help` or `-h` is an argument. Usage for
# Options hash
-The options hash that is passed in to `nomnom.opts()` is a hash keyed on option name. Each option specification can have the following fields:
+The options hash that is passed to `nomnom.opts()` is a hash keyed on option name. Each option specification can have the following fields:
#### string
@@ -155,6 +155,14 @@ A callback that will be executed as soon as the option is encountered. If the ca
The position of the option if it's a positional argument. If the option should be matched to the first positional arg use position `0`
+#### list
+
+Specifies that the option is a list. Appending can be achieved by specifying the arg more than once on the command line:
+
+ node test.js --file=test1.js --file=test2.js
+
+If the option has a `position` and `list` is true, all positional args including and after `position` will be appended to the array.
+
#### required
If this is set to `true` and the option isn't in the args, a message will be printed and the program will exit.
View
26 nomnom.js
@@ -25,11 +25,19 @@ function ArgParser() {
var option = opt(arg);
if(option.callback) {
var message = option.callback(value);
- if(typeof message == "string"){
+ if(typeof message == "string") {
parser.print(message);
}
}
- options[option.name || arg] = value;
+ var name = option.name || arg;
+ if(option.list) {
+ if(!options[name])
+ options[name] = [value];
+ else
+ options[name].push(value);
+ }
+ else
+ options[name] = value;
};
var parser = {
@@ -125,7 +133,7 @@ function ArgParser() {
commandName = argv[0];
if(!commandName) {
- // no command but command expected e.g. 'git --version'
+ // no command but command expected e.g. 'git -h'
parser.specs.command = {
position: 0,
help: 'one of: ' + _(parser.commands).keys().join(", ")
@@ -161,10 +169,6 @@ function ArgParser() {
parser.print(parser.getUsage());
var options = {};
- parser.specs.forEach(function(opt) {
- options[opt.name] = opt.default;
- });
-
args = argv.concat([""]).map(function(arg) {
return Arg(arg);
});
@@ -207,6 +211,11 @@ function ArgParser() {
positionals.forEach(function(pos, index) {
setOption(options, index, pos);
});
+
+ parser.specs.forEach(function(opt) {
+ if(typeof options[opt.name] == "undefined")
+ options[opt.name] = opt.default;
+ });
// exit if required arg isn't present
parser.specs.forEach(function(opt) {
@@ -278,7 +287,8 @@ Opt = function(opt) {
lg: lg,
metavar: metavar,
matches: function(arg) {
- return opt.lg == arg || opt.sh == arg || opt.position == arg;
+ return opt.lg == arg || opt.sh == arg || opt.position == arg
+ || (opt.list && arg >= opt.position);
},
expectsValue: function() {
return opt.metavar || opt.default;
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "nomnom",
"description": "Option parser with support for usage and commands",
- "version": "0.4.3",
+ "version": "0.4.5",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
"type": "git",
View
2 test/argv.js
@@ -10,7 +10,7 @@ var opts = [
{ name: 'logfile',
string: '-l LOG'}
];
-nomnom.parseArgs(opts, {
+nomnom().parseArgs(opts, {
printHelp: true,
printFunc : function(msg) {
sys.puts("heeeeeeeeeeeelp" + msg);
View
2 test/chain.js
@@ -18,7 +18,7 @@ var options = nomnom()
assert.ok(options.debug);
assert.equal(options.config, "test.json");
-var options = nomnom.parseArgs(["-xvf", "--file=test.js"]);
+var options = nomnom().parseArgs(["-xvf", "--file=test.js"]);
assert.ok(options.x);
assert.equal(options.file, "test.js");
View
23 test/list.js
@@ -0,0 +1,23 @@
+var nomnom = require("../nomnom"),
+ assert = require("assert");
+
+
+var options = nomnom().opts({
+ file: {
+ string: "--file=FILE",
+ list: true
+ }
+ })
+ .parseArgs(["--file=f1", "--file=f2"]);
+
+assert.deepEqual(options.file, ["f1", "f2"]);
+
+var options = nomnom().opts({
+ files : {
+ position: 1,
+ list: true
+ }
+ })
+ .parseArgs(["f1", "-d", "f2", "f3"]);
+
+assert.deepEqual(options.files, ["f2", "f3"]);
View
2 test/positional.js
@@ -56,7 +56,7 @@ assert.equal(options.test1, "def1");
assert.ok(!options.test2);
// positionals that weren't specified in opts
-options = nomnom.parseArgs({}, { argv: ["pos1", "pos2", "pos3"] });
+options = nomnom().parseArgs({}, { argv: ["pos1", "pos2", "pos3"] });
assert.equal(options[0], "pos1");
assert.equal(options[1], "pos2");
assert.equal(options[2], "pos3");

0 comments on commit 4950b10

Please sign in to comment.
Something went wrong with that request. Please try again.