Skip to content

Commit

Permalink
nothing to see here yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytri Kleiner committed Dec 17, 2010
0 parents commit b0af03b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
49 changes: 49 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

require.paths.push('.');

var Operetta = require('operetta').Operetta;

var operetta = new Operetta(['test', 'hello',
'-t', '-tclosetest', '-xtcloser', '-xt', 'another', 'test', '--test', 'long', 'test',
'-t', 'short', 'test', '--test=equals_test','--flag','/path/to/some/file']);

operetta.banner = " _____ _\n"
operetta.banner += "|_ _|__ ___| |_\n"
operetta.banner += " | |/ _ \\/ __| __|\n"
operetta.banner += " | | __/\\__ \\ |_\n"
operetta.banner += " |_|\\___||___/\\__|\n"

// Operetta is an Event Emitter
// Values that are not preceeded by an opion
// Are passed to the 'positional' event;
operetta.on('positional', function(value) {
console.log('positional:', value);
});

// Operetta has a special event binder called 'bind'
// bind takes one or more options, a description and listener
operetta.parameters(["-t", "--test"], "A Test Argument", function(value) {
if (value == undefined) {
// if no value follows options value is undefined
console.log("Test Nothing");
} else {
console.log("Test", value);
}
});

// An optional forth argument sets the option as a flag.
// it does not take a value and any arguments following it
// should be treated as positional arguments
operetta.options("--flag", "A Test Option", function() {
console.log("Flagged!");
});
operetta.options("-x", "x!", function() {
console.log("x!");
});

operetta.start(function(values) {
console.log(values);
});

operetta.usage();

133 changes: 133 additions & 0 deletions operetta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/***************************************************
*
* Operetta: A Node Option Parser That Sings!
* Dmytri Kleiner <dk@trick.ca>
*
* This program is free software.
* It comes without any warranty, to the extent permitted by
* applicable law. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License v2.
* See http://sam.zoy.org/wtfpl/COPYING for more details.
*
***********************************/

var events = require('events'),
sys = require('sys');

var Operetta = function(args) {
this.args = args || process.argv;
this.params = {};
this.opts = {};
this.values = [];
this.banner = '';
this.help = '';
this.re = /^(-[^-])([A-Za-z0-9_\-]+)?$|^(--[A-Za-z0-9_\-]+)[=]?(.+)?$/;
};
sys.inherits(Operetta, events.EventEmitter);

Operetta.prototype.start = function(listener) {
var operetta = this, option, value;
var trigger = function() {
if (option || value) {
option = option || 'positional';
operetta.values[option] = operetta.values[option] || [];
operetta.values[option].push(value);
operetta.emit(option, value);
option = undefined
value = undefined
}
}
while (operetta.args.length > 0) {
var arg = operetta.args.shift(),
m = operetta.re.exec(arg);
if (m) {
trigger();
if (m[2]) {
m[2] = m[1][1] + m[2];
for (c in m[2]) {
var o = '-' + m[2][c];
if (o in operetta.opts) {
option = operetta.opts[o];
trigger();
} else {
if (o in operetta.params) {
option = operetta.params[o];
if (m[2].length > c) {
value = m[2].slice(parseInt(c) + 1);
trigger();
}
} else {
option = undefined;
}
break;
}
}
} else {
var o = m[1] || m[3];
if (o in operetta.params) {
value = m[2] || m[4];
option = operetta.params[o];
if (value) {
trigger();
}
} else {
option = operetta.opts[o];
trigger();
}
}
} else {
if (value) {
value += ' ' + arg;
} else {
value = arg;
}
}
}
trigger();
if (listener) {
listener(operetta.values);
}
};

Operetta.prototype.bind = function(args, description, listener, takes_arguments) {
if (args) {
var operetta = this;
if (!(args != null && typeof args === "object" && 'join' in args)) {
args = [args];
}
var key = args[0];
sargs = args.join(",");
operetta.help += "\n" + args + Array(16 - sargs.length).join(" ") + description;
args.forEach(function(option){
if (takes_arguments) {
operetta.params[option] = key;
} else {
operetta.opts[option] = key;
}
});
if (listener) {
operetta.on(key, listener);
}
}
};

Operetta.prototype.parameters = function(args, description, listener) {
this.bind(args, description, listener, true);
};

Operetta.prototype.options = function(args, description, listener) {
this.bind(args, description, listener, false);
};

Operetta.prototype.usage = function(values, listener) {
var operetta = this;
var usage = [operetta.banner, operetta.help].join("\n");
if (listener) {
listener(usage);
} else {
console.log(usage);
}
};

exports.Operetta = Operetta;

0 comments on commit b0af03b

Please sign in to comment.