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

Commit

Permalink
'opts' -> 'options'
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Nov 2, 2011
1 parent 034e3f3 commit 3f85d03
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
nomnom is an option parser for node. It noms your args and gives them back to you in a hash.

```javascript
var options = require("nomnom")
.opts({
var opts = require("nomnom")
.options({
debug : {
abbr: 'd',
flag: true,
Expand All @@ -24,19 +24,19 @@ var options = require("nomnom")
})
.parse();

if (options.debug)
if (opts.debug)
// do stuff
```

You don't have to specify anything if you don't want to:

```javascript
var options = require("nomnom").parse();
var opts = require("nomnom").parse();

var url = options[0]; // get the first positional arg
var file = options.file // see if --file was specified
var verbose = options.v // see if -v was specified
var extras = options._ // get an array of the unmatched, positional args
var url = opts[0]; // get the first positional arg
var file = opts.file // see if --file was specified
var verbose = opts.v // see if -v was specified
var extras = opts._ // get an array of the unmatched, positional args
```

# Install
Expand Down Expand Up @@ -68,7 +68,7 @@ parser.command('browser')
.help("run browser tests");

parser.command('sanity')
.opts({
.options({
outfile: {
abbr: 'o',
help: 'file to write results to'
Expand All @@ -79,8 +79,8 @@ parser.command('sanity')
help: 'json manifest of tests to run'
}
})
.callback(function(options) {
runSanity(options.filename);
.callback(function(opts) {
runSanity(opts.filename);
})
.help("run the sanity tests")

Expand All @@ -93,9 +93,9 @@ Each command generates its own usage message when `-h` or `--help` is specified
Nomnom prints out a usage message if `--help` or `-h` is an argument. Usage for these options in `test.js`:

```javascript
var options = require("nomnom")
var opts = require("nomnom")
.script("runtests")
.opts({
.options({
path: {
position: 0,
help: "Test file to run",
Expand Down Expand Up @@ -125,7 +125,7 @@ var options = require("nomnom")
-d, --debug Print debugging info

# Options hash
The options hash passed to `nomnom.opts()` is a hash keyed on option name. Each option specification can have the following fields:
The options hash passed to `nomnom.options()` is a hash keyed on option name. Each option specification can have the following fields:

#### abbr and full
`abbr` is the single character string to match to this option, `full` is the full-length string (defaults to the name of the option).
Expand Down Expand Up @@ -183,8 +183,9 @@ A callback that will be executed as soon as the option is encountered. If the ca

count: {
callback: function(count) {
if (count != parseInt(count))
if (count != parseInt(count)) {
return "count must be an integer";
}
}
}
```
Expand Down Expand Up @@ -221,7 +222,7 @@ Option won't be printed in the usage
# Parser interface
`require("nomnom")` will give you the option parser. You can also make an instance of a parser with `require("nomnom")()`. You can chain any of these functions off of a parser:

#### opts
#### options

The options hash.

Expand Down Expand Up @@ -254,13 +255,13 @@ Gives a command object that will be used when no command is called.
Parses node's `process.argv` and returns the parsed options hash. You can also provide argv:

```javascript
var options = nomnom.parse(["-xvf", "--atomic=true"])
var opts = nomnom.parse(["-xvf", "--atomic=true"])
```

# Command interface
A command is specified with `nomnom.command('name')`. All these functions can be chained on a command:

#### opts
#### options

The options for this command.

Expand Down
15 changes: 12 additions & 3 deletions nomnom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ ArgParser.prototype = {
};
}

// facilitates command('name').opts().cb().help()
// facilitates command('name').options().cb().help()
var chain = {
opts : function(specs) {
options : function(specs) {
command.specs = specs;
return chain;
},
opts : function(specs) {
// old API
return this.options(specs);
},
callback : function(cb) {
command.cb = cb;
return chain;
Expand All @@ -48,11 +52,16 @@ ArgParser.prototype = {
return this.command();
},

opts : function(specs) {
options : function(specs) {
this.specs = specs;
return this;
},

opts : function(specs) {
// old API
return this.options(specs);
},

usage : function(usage) {
this._usage = usage;
return this;
Expand Down
4 changes: 2 additions & 2 deletions test/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var nomnom = require("../nomnom");
exports.testVersion = function(test) {
test.expect(1);

nomnom().opts({
nomnom().options({
date: {
callback: function(date) {
test.equal(date, "2010-02-03", "date should match value")
Expand All @@ -17,7 +17,7 @@ exports.testVersion = function(test) {
exports.testReturnString = function(test) {
test.expect(1);

nomnom().opts({
nomnom().options({
version: {
flag: true,
callback: function() {
Expand Down
8 changes: 4 additions & 4 deletions test/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.testNoCommand = function(test) {
var parser = nomnom();

parser.nocommand()
.opts({
.options({
version: {
flag: true
}
Expand All @@ -60,14 +60,14 @@ exports.testNoCommand = function(test) {

function createParser() {
var parser = nomnom().scriptName("test")
.opts({
.options({
debug: {
flag: true
}
});

parser.command('run')
.opts({
.options({
file: {
help: 'file to run'
}
Expand All @@ -77,7 +77,7 @@ function createParser() {
parser.command('test').usage("test usage");

parser.nocommand()
.opts({
.options({
verbose: {
flag: true
}
Expand Down
8 changes: 4 additions & 4 deletions test/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ var opts = {
}
}

var parser = nomnom().opts(opts);
var parser = nomnom().options(opts);

exports.testFlag = function(test) {
test.expect(1);

nomnom().opts({
nomnom().options({
file: {
position: 0,
}
Expand All @@ -27,7 +27,7 @@ exports.testFlag = function(test) {
exports.testRequired = function(test) {
test.expect(1);

nomnom().opts({
nomnom().options({
file: {
required: true
}
Expand All @@ -42,7 +42,7 @@ exports.testRequired = function(test) {
exports.testChoices = function(test) {
test.expect(2);

var parser = nomnom().opts({
var parser = nomnom().options({
color: {
choices: ['green', 'blue']
}
Expand Down
2 changes: 1 addition & 1 deletion test/matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var opts = {
}
}

var parser = nomnom().opts(opts);
var parser = nomnom().options(opts);

exports.testPositional = function(test) {
var options = parser.parse(["--flag1", "val1", "--config", "file", "val2"]);
Expand Down
4 changes: 2 additions & 2 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var opts = {
}
}

var parser = nomnom().opts(opts).help("all the best foods").scriptName("test");
var parser = nomnom().options(opts).help("all the best foods").scriptName("test");

var expected = "usage:test<egg>[options]eggrobinoptions:-a,--applehowmanyapples--b-nana-cNUM,--carrots=NUM--dillPICKLEallthebestfoods"

Expand Down Expand Up @@ -79,7 +79,7 @@ exports.testUsage = function(test) {
exports.testHidden = function(test) {
test.expect(1);

nomnom().opts({
nomnom().options({
file: {
hidden: true
}
Expand Down
2 changes: 1 addition & 1 deletion test/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var opts = {
}
}

var parser = nomnom().opts(opts);
var parser = nomnom().options(opts);

exports.testFlag = function(test) {
var options = parser.parse(["--debug", "pos0", "--no-verbose"]);
Expand Down

0 comments on commit 3f85d03

Please sign in to comment.