Skip to content

Commit

Permalink
hack an instance for process.argv onto Argv so the export can be call…
Browse files Browse the repository at this point in the history
…ed to create an instance or used for argv, which is the most common case
  • Loading branch information
James Halliday committed Sep 10, 2010
1 parent a01caeb commit 102496a
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions optimist.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
module.exports = new Argv(process.argv.slice(2));

module.exports = Argv;

/* Hack an instance of Argv with process.argv into Argv
so people an do
require('optimist')(['--beeble=1','-z','zizzle']).argv
to parse a list of args and
require('optimist').argv
to get a parsed version of process.argv.
*/
var inst = Argv(process.argv.slice(2));
Object.keys(inst).forEach(function (key) {
Argv[key] = typeof inst[key] == 'function'
? inst[key].bind(inst)
: inst[key];
});

function Argv (args) {
if (!(this instanceof Argv)) return new Argv(args);
var self = this;
var self = {};
self.argv = { _ : [] };

function set (key, value) {
Expand Down Expand Up @@ -56,8 +69,6 @@ function Argv (args) {
}
}

self.parse = Argv;

var usage;
self.usage = function (msg) {
usage = msg;
Expand Down Expand Up @@ -98,4 +109,6 @@ function Argv (args) {
}
return self;
};

return self;
};

0 comments on commit 102496a

Please sign in to comment.