Permalink
Browse files

initial argparse code plus README and package.json

  • Loading branch information...
0 parents commit 0a310cc4f9278a7dd24ed981791445c85b390591 @harthur committed Aug 20, 2010
Showing with 142 additions and 0 deletions.
  1. +26 −0 README.md
  2. +99 −0 lib/argparse.js
  3. +17 −0 package.json
@@ -0,0 +1,26 @@
+=argparse=
+argparse is a mainly-correct options parser for [node.js](http://nodejs.org/). It parses `process.argv`. Currently it does not print out 'help'.
+
+= Install =
+using [npm](http://github.com/isaacs/npm):
+ git clone http://github.com/harthur/argparse
+ cd argparse
+ npm install .
+
+= Usage =
+ var argparse = require("argparse");
+
+ var options = [
+ { name: 'config',
+ flag: '-c',
+ full: '--config=PATH TO CONFIG',
+ default: 'config.json'},
+
+ { name: 'debug',
+ flag: '-d'}];
+
+ var parser = new argparse.ArgParser(options);
+ var options = parser.parse();
+
+ if(options.debug)
+ // do stuff
@@ -0,0 +1,99 @@
+var wu = require("wu").wu;
+
+ArgParser = function(options) {
+ this.options = options || [];
+}
+
+ArgParser.prototype = {
+ getOption : function(arg) {
+ var match = {};
+ wu(this.options).each(function(option) {
+ var fullOpt = new Arg(option.full);
+ if(fullOpt.isFull() && fullOpt.getKey() == arg)
+ match = option;
+
+ var flagOpt = new Arg(option.flag);
+ if(flagOpt.isFlag() && flagOpt.getFlags() == arg)
+ match = option;
+ });
+ return match;
+ },
+
+ getOptName : function(arg) {
+ return this.getOption(arg).name || arg;
+ },
+
+ getDefault : function(arg) {
+ return this.getOption(arg).default || true;
+ },
+
+ parse : function() {
+ var ret = {};
+ wu(process.argv.concat([""])).map(function(arg) {
+ return new Arg(arg);
+ })
+ .reduce(function(arg, val) {
+ /* -cfv */
+ if(arg.isFlag()) {
+ wu(arg.getFlags()).each(function(flag) {
+ ret[this.getOptName(flag)] = this.getDefault(flag);
+ }, this);
+ /* -c 4 */
+ if(val.isValue()) {
+ var value = val.val;
+ try {
+ value = JSON.parse(value)
+ } catch(e) {}
+
+ ret[this.getOptName(arg.lastFlag())] = value;
+ }
+ }
+ /* --config=tests.json */
+ if(arg.isFull()) {
+ ret[this.getOptName(arg.getKey())] = arg.getValue();
+ }
+ return val;
+ }, undefined, this);
+ return ret;
+ }
+}
+
+Arg = function(arg) {
+ this.val = arg;
+}
+
+Arg.prototype = {
+ flagRegex : /^\-(\w+)/,
+
+ fullRegex : /^\-\-(.+)=(.*)/,
+
+ isFlag : function() {
+ return this.flagRegex.test(this.val);
+ },
+
+ isValue : function() {
+ return this.val && !this.isFlag() && !this.isFull();
+ },
+
+ isFull : function() {
+ return this.fullRegex.test(this.val);
+ },
+
+ getFlags : function() {
+ return this.flagRegex.exec(this.val)[1];
+ },
+
+ lastFlag : function() {
+ return this.val[this.val.length - 1];
+ },
+
+ getKey : function() {
+ return this.fullRegex.exec(this.val)[1];
+ },
+
+ getValue : function() {
+ return this.fullRegex.exec(this.val)[2];
+ },
+}
+
+exports.ArgParser = ArgParser;
@@ -0,0 +1,17 @@
+{
+ "name": "argparse",
+ "description": "small option parser for node",
+ "version": "0.1",
+ "author": "Heather Arthur <fayearthur@gmail.com>",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/harthur/argparse.git"
+ },
+ "directories": {
+ "lib": "./lib"
+ },
+ "dependencies": {
+ "wu": "*"
+ },
+ "main": "./lib/argparse"
+}

0 comments on commit 0a310cc

Please sign in to comment.