Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
highlight 'abbr', 'full', and 'metavar' in readme instead of 'string'…
Browse files Browse the repository at this point in the history
…, fix a couple related bugs
  • Loading branch information
harthur committed Jul 18, 2011
1 parent 561de3c commit 04ddc2a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 42 deletions.
75 changes: 49 additions & 26 deletions README.md
Expand Up @@ -4,19 +4,18 @@ nomnom is an option parser for node and CommonJS. It noms your args and gives th
```javascript ```javascript
var options = require("nomnom") var options = require("nomnom")
.opts({ .opts({
debug : {
abbr: 'd',
help: 'Print debugging info'
},
version: { version: {
string: '--version',
help: 'print version and exit', help: 'print version and exit',
callback: function() { callback: function() {
return "version 1.2.4"; return "version 1.2.4";
} }
}, },
debug : {
string: '-d, --debug',
help: 'Print debugging info'
},
config: { config: {
string: '-c PATH, --config=PATH', abbr: 'c',
default: 'config.json', default: 'config.json',
help: 'JSON file with tests to run' help: 'JSON file with tests to run'
} }
Expand Down Expand Up @@ -73,7 +72,8 @@ parser.command('sanity')
help: 'test file to run' help: 'test file to run'
}, },
config: { config: {
string: '-c FILE, --config=FILE', abbr: 'c',
metavar: 'FILE',
default: 'config.json', default: 'config.json',
help: 'json file with tests to run' help: 'json file with tests to run'
} }
Expand Down Expand Up @@ -101,11 +101,12 @@ var options = require("nomnom")
list: true list: true
}, },
config: { config: {
string: '-c FILE, --config=FILE', abbr: 'c',
help: "Config file with tests to run", metavar: 'FILE',
help: "Config file with tests to run"
}, },
debug: { debug: {
string: '-d, --debug', abbr: 'd',
help: "Print debugging info" help: "Print debugging info"
} }
}).parseArgs(); }).parseArgs();
Expand All @@ -124,17 +125,40 @@ var options = require("nomnom")
# Options hash # 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: 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"` ```javascript

debug: {
To attach to `-v` or `--version` use `"-v, --version"` abbr: 'd'

}
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. 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 #### help


Expand All @@ -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: 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 ```javascript
var opts = {
count: { count: {
string: '-n COUNT', expectsValue: true,
callback: function(count) { callback: function(count) {
if(count != parseInt(count)) if(count != parseInt(count))
return "count must be an integer"; return "count must be an integer";
}
} }
} }
``` ```
Expand Down
28 changes: 15 additions & 13 deletions nomnom.js
Expand Up @@ -347,21 +347,23 @@ Opt = function(opt) {
metavar = opt.metavar || metavar; // e.g. PATH from '--config=PATH' metavar = opt.metavar || metavar; // e.g. PATH from '--config=PATH'
expectsValue = opt.expectsValue || metavar || opt.default; expectsValue = opt.expectsValue || metavar || opt.default;


var string= ""; var string;
if(abbr) { if(opt.string) {
string += "-" + abbr; string = opt.string;
if(metavar)
string += " " + metavar
string += ", ";
} }
if(full) { else if (opt.position === undefined) {
string += "--" + full; string= "";
if(abbr) {
string += "-" + abbr;
if(metavar)
string += " " + metavar
string += ", ";
}
string += "--" + (full || opt.name);
if(metavar) if(metavar)
string += "=" + metavar; string += " " + metavar;
} }
if(opt.string)
string = opt.string;

opt = _(opt).extend({ opt = _(opt).extend({
name: opt.name || full || abbr, name: opt.name || full || abbr,
string: string, string: string,
Expand All @@ -370,7 +372,7 @@ Opt = function(opt) {
metavar: metavar, metavar: metavar,
matches: function(arg) { matches: function(arg) {
return opt.full == arg || opt.abbr == arg || opt.position == 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 expectsValue: expectsValue
}); });
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{ {
"name": "nomnom", "name": "nomnom",
"description": "Option parser with generated usage and commands", "description": "Option parser with generated usage and commands",
"version": "0.6.0", "version": "0.6.1",
"author": "Heather Arthur <fayearthur@gmail.com>", "author": "Heather Arthur <fayearthur@gmail.com>",
"repository": { "repository": {
"type": "git", "type": "git",
Expand Down
16 changes: 14 additions & 2 deletions test/nostring.js
Expand Up @@ -10,6 +10,9 @@ var opts = {
}, },
config: { config: {
string: '-c FILE, --config=FILE' string: '-c FILE, --config=FILE'
},
numLines: {
string: '--num-lines=NUM, -n NUM'
} }
}; };


Expand All @@ -20,17 +23,26 @@ var opts2 = {
}, },
version: { version: {
abbr: 'v', abbr: 'v',
full: 'version'
}, },
config: { config: {
abbr: 'c', abbr: 'c',
full: 'config', full: 'config',
metavar: 'FILE' 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 options = nomnom().opts(opts).parseArgs(argv);
var options2 = nomnom().opts(opts2).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); assert.deepEqual(options, options2);

0 comments on commit 04ddc2a

Please sign in to comment.