Skip to content

Commit

Permalink
Adding command line helper (cli.js) and path helper (pathExt.js)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmcl committed Jun 12, 2012
1 parent 826fa36 commit 2679626
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/cli.js
@@ -0,0 +1,16 @@
var nopt = require('nopt');

/**
* Parse command line arguments
* @param {Array} args the command line arguments array
*/
exports.parseCommandLineArgs = function( args ) {
var options = {
'port': Number
},
shorthand = {
'p': '--port'
};

return nopt(options, shorthand, args);
};
32 changes: 32 additions & 0 deletions lib/pathExt.js
@@ -0,0 +1,32 @@
var path = require('path');

/**
* Resolves a pathname to an absolute pathname.
* Handles ~/ references and environmental variables.
*
* Example:
* Say you have the $DATA environmental variable set to 'app/config'
*
* You call: resolve('~/$DATA/file.log')
* Result would be something like: '/home/username/app/config/file.log'
*/
exports.resolve = function(pathname) {
if(typeof pathname !== 'string') {
pathname = '';
}

// resolve ~/
pathname = pathname.replace('~/', process.env.HOME + '/');

// resolve env variables
pathname = pathname.replace(/\$(\w*)/g, function() {
var env = process.env[arguments[1]];
if(env) {
return env;
} else {
return '';
}
});

return path.resolve(pathname);
};

0 comments on commit 2679626

Please sign in to comment.