Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding command support

  • Loading branch information...
commit c90dfcde7d12d2b310ba2818c130b57e84060126 1 parent a95d275
Dmytri Kleiner authored
Showing with 73 additions and 30 deletions.
  1. +22 −0 example_commands.js
  2. +51 −30 operetta.js
View
22 example_commands.js
@@ -0,0 +1,22 @@
+
+require.paths.push('.');
+
+var Operetta = require('operetta').Operetta;
+
+var args = ['test', 'hello',
+ '-t', '-tclosetest', '-xtcloser', '-xt', 'another', 'test', '--test', 'long', 'test',
+ '-t', 'short', 'test', '--test=equals_test','--flag','/path/to/some/file'];
+
+console.log("[", args.join(" "), "]\n");
+
+var operetta = new Operetta(args);
+operetta.command('say', function(command) {
+ command.start(function(values) {
+ console.log(values);
+ });
+});
+
+operetta.start();
+
+operetta.usage();
+
View
81 operetta.js
@@ -50,46 +50,62 @@ var Operetta = function(args) {
this.params = {};
// options which are not paramaters
this.opts = {};
+ this.commands = {};
this.values = [];
this.banner = '';
this.help = '';
+ this.parent = null;
// universal option detector
this.re = /^(-[^-])([A-Za-z0-9_\-]+)?$|^(--[A-Za-z0-9_\-]+)[=]?(.+)?$/;
+ this.parse = function(listener) {
+ var operetta = this, parameter;
+ var sing = function(argument, data) {
+ operetta.values[argument] = operetta.values[argument] || [];
+ operetta.values[argument].push(data);
+ operetta.emit(argument, data);
+ if (operetta.parent) operetta.parent.emit(argument, data);
+ parameter = undefined
+ }
+ var process = function(option, data) {
+ parameter = operetta.params[option];
+ if (data || !parameter) sing(parameter || operetta.opts[option], data || true);
+ }
+ while (operetta.args.length > 0) {
+ var arg = operetta.args.shift(),
+ m = operetta.re.exec(arg);
+ if (m) {
+ if (parameter) sing(parameter, null);
+ if (m[2]) {
+ var options = m[1][1] + m[2];
+ for (i in options) {
+ var option = operetta.opts["-" + options[i]];
+ if (option) process(option);
+ else {
+ process("-" + options[i], options.slice(parseInt(i) + 1));
+ break;
+ }
+ }
+ } else process(m[1] || m[3], m[4]);
+ } else if (parameter) sing(parameter, arg);
+ else sing("positional", arg);
+ }
+ if (listener) listener(operetta.values);
+ };
};
sys.inherits(Operetta, events.EventEmitter);
Operetta.prototype.start = function(listener) {
- var operetta = this, parameter;
- var sing = function(argument, data) {
- operetta.values[argument] = operetta.values[argument] || [];
- operetta.values[argument].push(data);
- operetta.emit(argument, data);
- parameter = undefined
- }
- var parse = function(option, data) {
- parameter = operetta.params[option];
- if (data || !parameter) sing(parameter || operetta.opts[option], data || true);
- }
- while (operetta.args.length > 0) {
- var arg = operetta.args.shift(),
- m = operetta.re.exec(arg);
- if (m) {
- if (parameter) sing(parameter, null);
- if (m[2]) {
- var options = m[1][1] + m[2];
- for (i in options) {
- var option = operetta.opts["-" + options[i]];
- if (option) parse(option);
- else {
- parse("-" + options[i], options.slice(parseInt(i) + 1));
- break;
- }
- }
- } else parse(m[1] || m[3], m[4]);
- } else if (parameter) sing(parameter, arg);
- else sing("positional", arg);
+ if (this.commands.length > 0) {
+ var command = operetta.args[0],
+ callback = this.commands(command);
+ if (listener) {
+ operetta.args.shift(),
+ operetta = new Operetta(operetta.args);
+ operetta.parent = this;
+ callback(operetta);
+ }
}
- if (listener) listener(operetta.values);
+ this.parse(listener);
};
Operetta.prototype.bind = function(args, description, listener, takes_arguments) {
@@ -115,6 +131,11 @@ Operetta.prototype.options = function(args, description, listener) {
this.bind(args, description, listener, false);
};
+Operetta.prototype.command = function(command, description, listener) {
+ this.help += "\n" + command + Array(16 - command.length).join(" ") + description;
+ this.commands[command] = listener;
+};
+
Operetta.prototype.usage = function(values, listener) {
var operetta = this;
var usage = [operetta.banner, operetta.help].join("\n");
Please sign in to comment.
Something went wrong with that request. Please try again.