Permalink
Browse files

Closes GH-1 Collisions between shorthands and abbrevs could cause fai…

…lure
  • Loading branch information...
1 parent 88f255e commit ec08a414784dc400c6cb2434bf02f3b7f5ce9e61 @isaacs isaacs committed Mar 30, 2011
Showing with 23 additions and 8 deletions.
  1. +2 −0 README.md
  2. +21 −8 lib/nopt.js
View
@@ -18,6 +18,7 @@ nice option parser.
## USAGE
+ // my-program.js
var nopt = require("nopt")
, Stream = require("stream").Stream
, path = require("path")
@@ -39,6 +40,7 @@ nice option parser.
// arg list defaults to process.argv
// slice defaults to 2
, parsed = nopt(knownOpts, shortHands, process.argv, 2)
+ console.log(parsed)
This would give you support for any of the following:
View
@@ -1,6 +1,6 @@
// info about each config option.
-var debug = process.env.DEBUG_OPTPARSE
+var debug = process.env.DEBUG_NOPT
? function () { console.error.apply(console, arguments) }
: function () {}
@@ -9,7 +9,7 @@ var url = require("url")
, Stream = require("stream").Stream
, abbrev = require("abbrev")
-module.exports = exports = optparse
+module.exports = exports = nopt
exports.clean = clean
exports.typeDefs =
@@ -21,12 +21,14 @@ exports.typeDefs =
, Stream : { type: Stream, validate: validateStream }
}
-function optparse (types, shorthands, args, slice) {
+function nopt (types, shorthands, args, slice) {
args = args || process.argv
types = types || {}
shorthands = shorthands || {}
if (typeof slice !== "number") slice = 2
+ debug(types, shorthands, args, slice)
+
args = args.slice(slice)
var data = {}
, key
@@ -145,12 +147,16 @@ function validate (data, k, val, type, typeDefs) {
}
function parse (args, data, remain, types, shorthands) {
+ debug("parse", args, data, remain)
+
var key = null
, abbrevs = abbrev(Object.keys(types))
, shortAbbr = abbrev(Object.keys(shorthands))
for (var i = 0; i < args.length; i ++) {
var arg = args[i]
+ debug("arg", arg)
+
if (arg.match(/^-{2,}$/)) {
// done with keys.
// the rest are args.
@@ -167,11 +173,15 @@ function parse (args, data, remain, types, shorthands) {
}
// see if it's a shorthand
// if so, splice and back up to re-parse it.
- var shRes = resolveShort(arg, shorthands, shortAbbr)
+ var shRes = resolveShort(arg, shorthands, shortAbbr, abbrevs)
+ debug("arg=%j shRes=%j", arg, shRes)
if (shRes) {
+ debug(arg, shRes)
args.splice.apply(args, [i, 1].concat(shRes))
- i --
- continue
+ if (arg !== shRes[0]) {
+ i --
+ continue
+ }
}
arg = arg.replace(/^-+/, "")
var no = false
@@ -207,12 +217,15 @@ function parse (args, data, remain, types, shorthands) {
}
}
-function resolveShort (arg, shorthands, shortAbbr) {
+function resolveShort (arg, shorthands, shortAbbr, abbrevs) {
// handle single-char shorthands glommed together, like
// npm ls -glp, but only if there is one dash, and only if
// all of the chars are single-char shorthands, and it's
// not a match to some other abbrev.
arg = arg.replace(/^-+/, '')
+ if (abbrevs[arg]) {
+ return null
+ }
if (shortAbbr[arg]) {
arg = shortAbbr[arg]
} else {
@@ -366,7 +379,7 @@ var assert = require("assert")
var argv = test[0].split(/\s+/)
, opts = test[1]
, rem = test[2]
- , actual = optparse(types, shorthands, argv, 0)
+ , actual = nopt(types, shorthands, argv, 0)
, parsed = actual.argv
delete actual.argv
console.log(parsed.remain)

0 comments on commit ec08a41

Please sign in to comment.