Permalink
Browse files

Use arguments-helper.

  • Loading branch information...
1 parent f03d775 commit c94dc3daa05f5ee1e6c7da45bc78ed8ad399db11 Miki Leskinen committed Jan 13, 2011
Showing with 26 additions and 20 deletions.
  1. +1 −20 application/foo-twitter-client.js
  2. +25 −0 application/support/arguments-helper.js
@@ -1,24 +1,5 @@
var express = require('express')
-
-var options = (function() {
- var _options = {
- port: '8002'
- }
-
- function resolveArgument(name, arg) {
- var fullPrefix = name + '='
- if (arg.substring(0, fullPrefix.length) === fullPrefix) {
- _options[name] = arg.substring(fullPrefix.length)
- }
- }
-
- process.argv.forEach(function(arg) {
- resolveArgument('port', arg)
- })
-
- return _options
-}())
-
+var options = require(__dirname + '/support/arguments-helper').argumentsHelper.toOptions({port: 8002}, process.argv)
var app = express.createServer()
app.set('view engine', 'jade')
@@ -0,0 +1,25 @@
+exports.argumentsHelper = (function() {
+ var undefined
+ var conversions = {
+ 'true': true,
+ 'false': false,
+ 'null': null
+ }
+
+ function toOptions(defaultOptions, arguments) {
+ var options = Object.create(defaultOptions)
+ arguments.forEach(function(argument) {
+ var separatorIndex = argument.indexOf('=')
+ var name = argument.substring(0, separatorIndex)
+ if (defaultOptions[name] !== undefined) {
+ var rawValue = argument.substring(separatorIndex + 1)
+ options[name] = conversions[rawValue] !== undefined ? conversions[rawValue] : +rawValue || rawValue
+ }
+ })
+ return options
+ }
+
+ return {
+ toOptions: toOptions
+ }
+}())

0 comments on commit c94dc3d

Please sign in to comment.