Skip to content

Commit

Permalink
new --key value style with passing tests. NOTE: changes existing beha…
Browse files Browse the repository at this point in the history
…vior
  • Loading branch information
James Halliday committed Dec 1, 2010
1 parent ed2a2d5 commit d54ffcc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/optimist.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ function Argv (args, cwd) {
}
else if (arg.match(/^--.+/)) {
var key = arg.match(/^--(.+)/)[1];
set(key, true);
var next = args[i + 1];
if (next !== undefined && !next.match(/^-/)) {
set(key, next);
i++;
}
else {
set(key, true);
}
}
else if (arg.match(/^-[^-]+/)) {
arg.slice(1,-1).split('').forEach(function (letter) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "optimist",
"version" : "0.0.7",
"version" : "0.1.0",
"description" : "Light-weight option parsing with an argv hash. No optstrings attached.",
"modules" : {
"index" : "./lib/optimist.js",
Expand Down
19 changes: 17 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,28 @@ exports['short captures'] = function (assert) {
);
};

exports['long capture sp'] = function (assert) {
assert.deepEqual(
optimist.parse([ '--pow', 'xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : 'expresso' }
);
};

exports['long capture'] = function (assert) {
exports['long capture eq'] = function (assert) {
assert.deepEqual(
optimist.parse([ '--pow=xixxle' ]),
{ pow : 'xixxle', _ : [], $0 : 'expresso' }
);
};

exports['long captures'] = function (assert) {
exports['long captures sp'] = function (assert) {
assert.deepEqual(
optimist.parse([ '--host', 'localhost', '--port', '555' ]),
{ host : 'localhost', port : '555', _ : [], $0 : 'expresso' }
);
};

exports['long captures eq'] = function (assert) {
assert.deepEqual(
optimist.parse([ '--host=localhost', '--port=555' ]),
{ host : 'localhost', port : '555', _ : [], $0 : 'expresso' }
Expand Down Expand Up @@ -103,6 +116,7 @@ exports.comprehensive = function (assert) {
optimist.parse([
'--name=meowmers', 'bare', '-cats', 'woo',
'-h', 'awesome', '--multi=quux',
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', 'eek'
]),
Expand All @@ -114,6 +128,7 @@ exports.comprehensive = function (assert) {
h : 'awesome',
b : true,
bool : true,
key : 'value',
multi : [ 'quux', 'baz' ],
meep : false,
name : 'meowmers',
Expand Down

0 comments on commit d54ffcc

Please sign in to comment.