Comparing version 0.1.3 to 0.1.4
192
index.js
@@ -1,1 +0,191 @@ | ||
module.exports = require('./lib/optimist'); | ||
module.exports = Argv; | ||
var path = require('path'); | ||
// Hack to work when loaded with CoffeeScript | ||
if (process.argv.length < 2 || process.argv[0] != 'node') { | ||
process.argv = ['coffee', ''].concat(process.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, cwd) { | ||
var self = {}; | ||
if (!cwd) cwd = process.cwd(); | ||
self.$0 = process.argv | ||
.slice(0,2) | ||
.map(function (x) { | ||
var b = rebase(cwd, x); | ||
return x.match(/^\//) && b.length < x.length | ||
? b : x | ||
}) | ||
.join(' ') | ||
; | ||
if (process.argv[1] == process.env._) { | ||
self.$0 = process.env._.replace( | ||
path.dirname(process.execPath) + '/', '' | ||
); | ||
} | ||
self.argv = { _ : [], $0 : self.$0 }; | ||
function set (key, val) { | ||
var num = Number(val); | ||
var value = typeof val !== 'string' || isNaN(num) ? val : num; | ||
if (key in self.argv) { | ||
if (!Array.isArray(self.argv[key])) { | ||
self.argv[key] = [ self.argv[key] ]; | ||
} | ||
self.argv[key].push(value); | ||
} | ||
else { | ||
self.argv[key] = value; | ||
} | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var arg = args[i]; | ||
if (arg == '--') { | ||
self.argv._.push.apply(self.argv._, args.slice(i + 1)); | ||
break; | ||
} | ||
else if (arg.match(/^--.+=/)) { | ||
var m = arg.match(/^--([^=]+)=(.*)/); | ||
set(m[1], m[2]); | ||
} | ||
else if (arg.match(/^--no-.+/)) { | ||
var key = arg.match(/^--no-(.+)/)[1]; | ||
set(key, false); | ||
} | ||
else if (arg.match(/^--.+/)) { | ||
var key = arg.match(/^--(.+)/)[1]; | ||
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) { | ||
set(letter, true); | ||
}); | ||
var key = arg.slice(-1)[0]; | ||
if (args[i+1] && !args[i+1].match(/^-/)) { | ||
set(key, args[i+1]); | ||
i++; | ||
} | ||
else { | ||
set(key, true); | ||
} | ||
} | ||
else { | ||
var n = Number(arg); | ||
self.argv._.push(isNaN(n) ? arg : n); | ||
} | ||
} | ||
var usage; | ||
self.usage = function (msg) { | ||
usage = msg; | ||
return self; | ||
}; | ||
function fail (msg) { | ||
if (usage) console.error(usage.replace(/\$0/g, self.$0)) | ||
console.error(msg); | ||
process.exit(); | ||
} | ||
self.check = function (f) { | ||
try { | ||
if (f(self.argv) === false) fail( | ||
'Argument check failed: ' + f.toString() | ||
); | ||
} | ||
catch (err) { fail(err) } | ||
return self; | ||
}; | ||
self.demand = function (keys, cb) { | ||
if (typeof keys == 'number') { | ||
return self.demandCount(keys, cb); | ||
} | ||
var missing = []; | ||
keys.forEach(function (key) { | ||
if (!(key in self.argv)) missing.push(key); | ||
}); | ||
if (missing.length > 0) { | ||
if (cb) cb(missing); | ||
else fail('Missing arguments: ' + missing.join(' ')); | ||
} | ||
return self; | ||
}; | ||
self.demandCount = function (count, cb) { | ||
if (self.argv._.length < count) { | ||
if (cb) cb(self.argv._.length); | ||
else fail('Not enough arguments, expected ' | ||
+ count + ', but only found ' + self.argv._.length); | ||
} | ||
return self; | ||
}; | ||
self.default = function (key, value) { | ||
if (typeof key === 'object') { | ||
Object.keys(key).forEach(function (k) { | ||
self.default(k, key[k]); | ||
}); | ||
} | ||
else { | ||
if (self.argv[key] === undefined) { | ||
self.argv[key] = value; | ||
} | ||
} | ||
return self; | ||
}; | ||
self.parse = function (args) { | ||
return Argv(args).argv; | ||
}; | ||
return self; | ||
}; | ||
// rebase an absolute path to a relative one with respect to a base directory | ||
// exported for tests | ||
module.exports.rebase = rebase; | ||
function rebase (base, dir) { | ||
var ds = path.normalize(dir).split('/').slice(1); | ||
var bs = path.normalize(base).split('/').slice(1); | ||
for (var i = 0; ds[i] && ds[i] == bs[i]; i++); | ||
ds.splice(0, i); bs.splice(0, i); | ||
var p = path.normalize( | ||
bs.map(function () { return '..' }).concat(ds).join('/') | ||
).replace(/\/$/,'').replace(/^$/, '.'); | ||
return p.match(/^[.\/]/) ? p : './' + p; | ||
} |
{ | ||
"name" : "optimist", | ||
"version" : "0.1.3", | ||
"version" : "0.1.4", | ||
"description" : "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
"modules" : { | ||
"index" : "./lib/optimist.js", | ||
}, | ||
"main" : "./index.js", | ||
"repository" : { | ||
@@ -9,0 +7,0 @@ "type" : "git", |
@@ -7,3 +7,3 @@ // coffee script style arguments where process.argv is .splice(2)'d | ||
exports.coffee = function (assert) { | ||
fs.readFile(__dirname + '/../lib/optimist.js', function (err, src) { | ||
fs.readFile(__dirname + '/../index.js', function (err, src) { | ||
if (err) assert.fail(err); | ||
@@ -10,0 +10,0 @@ var context = { |
@@ -203,7 +203,8 @@ var Hash = require('traverse/hash'); | ||
var _console = console; | ||
console = Hash.copy(console); | ||
var errors = []; | ||
var logs = []; | ||
console._error = console.error; | ||
console.error = function (msg) { errors.push(msg) }; | ||
console._log = console.log; | ||
console.log = function (msg) { logs.push(msg) }; | ||
@@ -214,3 +215,4 @@ | ||
process = _process; | ||
console = _console; | ||
console.error = console._error; | ||
console.log = console._log; | ||
@@ -217,0 +219,0 @@ return { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Invalid manifest file
QualityPackage has an invalid manifest file and can cause installation problems if you try to use it.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
1
0
22727
16
579
2
2