Permalink
Browse files

highlight 'abbr', 'full', and 'metavar' in readme instead of 'string'…

…, fix a couple related bugs
  • Loading branch information...
1 parent 561de3c commit 04ddc2a4ff02b18b1b403a4a2382edc602bc5a18 @harthur committed Jul 18, 2011
Showing with 79 additions and 42 deletions.
  1. +49 −26 README.md
  2. +15 −13 nomnom.js
  3. +1 −1 package.json
  4. +14 −2 test/nostring.js
View
@@ -4,19 +4,18 @@ nomnom is an option parser for node and CommonJS. It noms your args and gives th
```javascript
var options = require("nomnom")
.opts({
+ debug : {
+ abbr: 'd',
+ help: 'Print debugging info'
+ },
version: {
- string: '--version',
help: 'print version and exit',
callback: function() {
return "version 1.2.4";
}
},
- debug : {
- string: '-d, --debug',
- help: 'Print debugging info'
- },
config: {
- string: '-c PATH, --config=PATH',
+ abbr: 'c',
default: 'config.json',
help: 'JSON file with tests to run'
}
@@ -73,7 +72,8 @@ parser.command('sanity')
help: 'test file to run'
},
config: {
- string: '-c FILE, --config=FILE',
+ abbr: 'c',
+ metavar: 'FILE',
default: 'config.json',
help: 'json file with tests to run'
}
@@ -101,11 +101,12 @@ var options = require("nomnom")
list: true
},
config: {
- string: '-c FILE, --config=FILE',
- help: "Config file with tests to run",
+ abbr: 'c',
+ metavar: 'FILE',
+ help: "Config file with tests to run"
},
debug: {
- string: '-d, --debug',
+ abbr: 'd',
help: "Print debugging info"
}
}).parseArgs();
@@ -124,17 +125,40 @@ var options = require("nomnom")
# Options hash
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
+#### abbr, full, and metavar
+`abbr` is the single character string to match to this option, `full` is the full-length string (defaults to the name of the option). `metavar` is used in the usage printout and specifies that the option expects a value, `expectsValue` can also be set to `true` for this purpose (default is `false`).
-specifies what command line arguments to match on and wether the option takes an argument.
+This option matches `-d` and `--debug` on the command line:
-To attach an option to `--version` use `"--version"`
-
-To attach to `-v` or `--version` use `"-v, --version"`
-
-To attach to `-c` and `--config` and require an argument use `"-c FILE, --config=FILE"`
-
-The metavar (e.g. `"FILE"`) is just a guide and can be any string. Note that `string` will be used when printing the usage for this option.
+```javascript
+debug: {
+ abbr: 'd'
+}
+```
+
+This option matches `-n 3`, `--num-lines 12` on the command line:
+
+```javascript
+numLines: {
+ abbr: 'n',
+ full: 'num-lines',
+ expectsValue: true
+}
+```
+
+as does:
+
+```javascript
+numLines: {
+ abbr: 'n',
+ full: 'num-lines',
+ metavar: "NUM"
+}
+```
+
+#### string
+
+A shorthand for `abbr`, `full`, and `metavar`. For example, to attach an options to `-c` and `--config` and require an argument use a `string: "-c FILE, --config=FILE"`
#### help
@@ -149,13 +173,12 @@ The value to give the option if it's not specified in the arguments.
A callback that will be executed as soon as the option is encountered. If the callback returns a string it will print the string and exit:
```javascript
-var opts = {
- count: {
- string: '-n COUNT',
- callback: function(count) {
- if(count != parseInt(count))
- return "count must be an integer";
- }
+
+count: {
+ expectsValue: true,
+ callback: function(count) {
+ if(count != parseInt(count))
+ return "count must be an integer";
}
}
```
View
@@ -347,21 +347,23 @@ Opt = function(opt) {
metavar = opt.metavar || metavar; // e.g. PATH from '--config=PATH'
expectsValue = opt.expectsValue || metavar || opt.default;
- var string= "";
- if(abbr) {
- string += "-" + abbr;
- if(metavar)
- string += " " + metavar
- string += ", ";
+ var string;
+ if(opt.string) {
+ string = opt.string;
}
- if(full) {
- string += "--" + full;
+ else if (opt.position === undefined) {
+ string= "";
+ if(abbr) {
+ string += "-" + abbr;
+ if(metavar)
+ string += " " + metavar
+ string += ", ";
+ }
+ string += "--" + (full || opt.name);
if(metavar)
- string += "=" + metavar;
+ string += " " + metavar;
}
- if(opt.string)
- string = opt.string;
-
+
opt = _(opt).extend({
name: opt.name || full || abbr,
string: string,
@@ -370,7 +372,7 @@ Opt = function(opt) {
metavar: metavar,
matches: function(arg) {
return opt.full == arg || opt.abbr == arg || opt.position == arg
- || (opt.list && arg >= opt.position);
+ || opt.name == arg || (opt.list && arg >= opt.position);
},
expectsValue: expectsValue
});
View
@@ -1,7 +1,7 @@
{
"name": "nomnom",
"description": "Option parser with generated usage and commands",
- "version": "0.6.0",
+ "version": "0.6.1",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
"type": "git",
View
@@ -10,6 +10,9 @@ var opts = {
},
config: {
string: '-c FILE, --config=FILE'
+ },
+ numLines: {
+ string: '--num-lines=NUM, -n NUM'
}
};
@@ -20,17 +23,26 @@ var opts2 = {
},
version: {
abbr: 'v',
- full: 'version'
},
config: {
abbr: 'c',
full: 'config',
metavar: 'FILE'
+ },
+ numLines: {
+ abbr: 'n',
+ full: 'num-lines',
+ expectsValue: true
}
};
-var argv = ["-l", "log.txt", "-v", "--config=test.js"];
+var argv = ["--logfile", "log.txt", "--num-lines=4", "-v", "--config=test.js"];
var options = nomnom().opts(opts).parseArgs(argv);
var options2 = nomnom().opts(opts2).parseArgs(argv);
+assert.equal(options.logfile, "log.txt");
+assert.equal(options.numLines, 4);
+assert.equal(options.config, "test.js");
+assert.ok(options.version);
+
assert.deepEqual(options, options2);

0 comments on commit 04ddc2a

Please sign in to comment.