Skip to content

Commit

Permalink
Use optparse instead of parse-args.js
Browse files Browse the repository at this point in the history
Also, move default-config.js and the optparse hashes into
config-defs.js
  • Loading branch information
isaacs committed Mar 25, 2011
1 parent 85fbe18 commit 2d7027d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 371 deletions.
10 changes: 6 additions & 4 deletions bin/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ var fs = require("../lib/utils/graceful-fs")
, rm = require("../lib/utils/rm-rf")
, errorHandler = require("../lib/utils/error-handler")

, argv = process.argv.slice(2)
, parseArgs = require("../lib/utils/parse-args")
, configDefs = require("../lib/utils/config-defs")
, shorthands = configDefs.shorthands
, types = configDefs.types
, optparse = require("optparse")

log.verbose(argv, "cli")
log.verbose(process.argv, "cli")

var conf = parseArgs(argv)
var conf = optparse(types, shorthands)
npm.argv = conf.argv.remain
if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()
else conf.usage = true
Expand Down
2 changes: 1 addition & 1 deletion doc/npm.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ npm is extremely configurable. It reads its configuration options from
then that file is parsed instead.
* Defaults:
npm's default configuration options are defined in
lib/utils/default-config.js. These should not be changed.
lib/utils/config-defs.js. These must not be changed.

See `npm help config` for much much more information.

Expand Down
10 changes: 6 additions & 4 deletions lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module.exports = completion
completion.usage = "npm completion >> ~/.bashrc"

var output = require("./utils/output")
, parseArgs = require("./utils/parse-args")
, configTypes = parseArgs.types
, shorthands = parseArgs.shorthands
, configDefs = require("./utils/config-defs")
, configTypes = configDefs.types
, shorthands = configDefs.shorthands
, optparse = require("optparse")
, configNames = Object.keys(configTypes).filter(function (e) {
return e.charAt(0) !== "_"
})
Expand Down Expand Up @@ -114,7 +115,8 @@ function completion (args, cb) {
// take a little shortcut and use npm's arg parsing logic.
// don't have to worry about the last arg being implicitly
// boolean'ed, since the last block will catch that.
var parsed = opts.conf = parseArgs(partialWords.slice(0, -1))
var parsed = opts.conf =
optparse(configTypes, shorthands, partialWords.slice(0, -1), 0)
// check if there's a command already.
console.error(parsed)
var cmd = parsed.argv.remain[1]
Expand Down
8 changes: 4 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var ini = require("./utils/ini")
, fs = require("./utils/graceful-fs")
, dc
, output = require("./utils/output")
, parseArgs = require("./utils/parse-args")
, types = require("./utils/config-defs").types

config.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
Expand All @@ -34,7 +34,7 @@ config.completion = function (opts, cb) {
case "get":
case "delete":
case "rm":
return cb(null, Object.keys(parseArgs.types))
return cb(null, Object.keys(types))
case "edit":
case "list": case "ls":
return cb(null, [])
Expand Down Expand Up @@ -65,7 +65,7 @@ function edit (cb) {
if (er) return cb(er)
fs.readFile(f, "utf8", function (er, data) {
if (er) data = ""
dc = dc || require("./utils/default-config")
dc = dc || require("./utils/config-defs").defaults
data = [ ";;;;"
, "; npm "+(ini.get("global") ? "globalconfig" : "userconfig")+" file"
, "; this is a simple ini-formatted file"
Expand Down Expand Up @@ -139,7 +139,7 @@ function list (cb) {
var msg = ""
ini.keys.sort(function (a,b) { return a > b ? 1 : -1 })
.forEach(function (i) {
if (parseArgs.types[i] !== parseArgs.types[i]) {
if (types[i] !== types[i]) {
return
}
var val = (i.charAt(0) === "_")
Expand Down
86 changes: 75 additions & 11 deletions lib/utils/default-config.js → lib/utils/config-defs.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// defaults, types, and shorthands.

var log = require("./log")
, path = require("path")
, hasSSL = false
, sslWorks = false
, semver = require("semver")
, stdio = process.binding("stdio")

try {
hasSSL = !!(process.binding("crypto") && require("crypto"))
} catch (ex) {}

var path = require("path")
, stdio = process.binding("stdio")
, url = require("url")
, Stream = require("stream").Stream

module.exports =
exports.defaults =
{ argv : []
// are there others?
, browser : process.platform === "darwin" ? "open" : "google-chrome"
Expand Down Expand Up @@ -63,3 +58,72 @@ module.exports =
, viewer: "man"
, _exit : true
}

exports.types =
{ argv : NaN
, browser : String
, cache : path
, color : ["always", Boolean]
, depth : Number
, description : Boolean
, dev : Boolean
, editor : path
, force : Boolean
, global : Boolean
, globalconfig : path
, group : [String, Number]
, gzipbin : String
, logfd : [Number, Stream]
, loglevel : ["silent","win","error","warn","info","verbose","silly"]
, long : Boolean
, "node-version" : [false, String]
, npaturl : url
, npat : Boolean
, "onload-script" : [false, String]
, outfd : [Number, Stream]
, parseable : Boolean
, pre: Boolean
, prefix: path
, proxy : url
, "rebuild-bundle" : Boolean
, registry : url
, searchopts : String
, searchexclude: [null, String]
, shell : path
, tag : String
, tar : String
, tmp : path
, "unsafe-perm" : Boolean
, usage : Boolean
, user : String
, username : String
, userconfig : path
, version : Boolean
, viewer: path
, _exit : Boolean
}

exports.shorthands =
{ s : ["--loglevel", "silent"]
, d : ["--loglevel", "info"]
, dd : ["--loglevel", "verbose"]
, ddd : ["--loglevel", "silly"]
, noreg : ["--no-registry"]
, reg : ["--registry"]
, "no-reg" : ["--no-registry"]
, silent : ["--loglevel", "silent"]
, verbose : ["--loglevel", "verbose"]
, h : ["--usage"]
, H : ["--usage"]
, "?" : ["--usage"]
, help : ["--usage"]
, v : ["--version"]
, f : ["--force"]
, desc : ["--description"]
, "no-desc" : ["--no-description"]
, "local" : ["--no-global"]
, l : ["--long"]
, p : ["--parseable"]
, porcelain : ["--parseable"]
, g : ["--global"]
}
8 changes: 4 additions & 4 deletions lib/utils/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var fs = require("./graceful-fs")
, ProtoList = require("./proto-list")
, defaultConfig
, configList = new ProtoList()
, parseArgs = require("./parse-args")
, types = require("./config-defs").types
, TRANS =
{ "default" : 4
, "global" : 3
Expand All @@ -53,7 +53,7 @@ var fs = require("./graceful-fs")
exports.configList = configList
configList.push({loglevel:"info"})
function resolveConfigs (cli, cb) {
defaultConfig = defaultConfig || require("./default-config")
defaultConfig = defaultConfig || require("./config-defs").defaults
exports.defaultConfig = defaultConfig
configList.pop()
configList.push(defaultConfig)
Expand Down Expand Up @@ -93,7 +93,7 @@ function parseEnv (env) {

function unParseField (f, k) {
// type can be an array or single thing.
var isPath = -1 !== [].concat(parseArgs.types[k]).indexOf(path)
var isPath = -1 !== [].concat(types[k]).indexOf(path)
if (isPath) {
if (typeof process.env.HOME !== 'undefined') {
if (process.env.HOME.substr(-1) === "/") {
Expand All @@ -110,7 +110,7 @@ function unParseField (f, k) {
function parseField (f, k) {
if (typeof f !== "string" && !(f instanceof String)) return f
// type can be an array or single thing.
var isPath = -1 !== [].concat(parseArgs.types[k]).indexOf(path)
var isPath = -1 !== [].concat(types[k]).indexOf(path)
f = (""+f).trim()
if (f === "") return f = true
switch (f) {
Expand Down
Loading

0 comments on commit 2d7027d

Please sign in to comment.